[JAVA]Request to enter a variable
Solved
Onde2Choc
-
Artcas2 -
Artcas2 -
Hello everyone.
I recently finished the beginner part of my book on programming that talked about Java. It’s all nice, I’m thinking, I know the basics of Java. While waiting to buy a more complex and specialized book, I’m going to make some cool little programs. I start a class, I open the braces, I declare a few variables and then I wonder... how do you ask the user to enter a variable (Input in BASIC)??? I re-open my book... Nothing about that... :-O
So I would like to know how to ask the user to enter an integer, decimal, or string (String) variable. A simple example will be enough for me.
Thank you in advance.
I recently finished the beginner part of my book on programming that talked about Java. It’s all nice, I’m thinking, I know the basics of Java. While waiting to buy a more complex and specialized book, I’m going to make some cool little programs. I start a class, I open the braces, I declare a few variables and then I wonder... how do you ask the user to enter a variable (Input in BASIC)??? I re-open my book... Nothing about that... :-O
So I would like to know how to ask the user to enter an integer, decimal, or string (String) variable. A simple example will be enough for me.
Thank you in advance.
9 réponses
The simplest way to ask the user to enter something is to use Scanner.
So, you import it:
import java.util.Scanner;
then, you use it:
Scanner userInput = new Scanner(System.in);
Now, you need to know what you want the user to enter as a value (int, float, double, string, char,...) depending on the type:
System.out.println("Please enter a word:");
String str = userInput.next();
System.out.println("Please enter an integer:");
int ent = userInput.nextInt();
... Etc,...
So, you import it:
import java.util.Scanner;
then, you use it:
Scanner userInput = new Scanner(System.in);
Now, you need to know what you want the user to enter as a value (int, float, double, string, char,...) depending on the type:
System.out.println("Please enter a word:");
String str = userInput.next();
System.out.println("Please enter an integer:");
int ent = userInput.nextInt();
... Etc,...
cpojdoe
thank you
Artcas2
Thank you, I was also looking for how to do that.