0
How can I save just one input from the user to two distinct variables (such as int and string) in java?
5 Answers
+ 2
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String s = Integer.toString(n);
But if you need different input you can do this:
int n = scan.nextInt();
String s = scan.nextLine() or scan.next();
double d = scan.nextDouble();
Maybe you can give an example of what you need.
+ 1
You can take in a string and perform some string operations on it.
+ 1
sample input would be useful:
i.e
word, int
word, word, int
word, word, int, int
word, int, int, int.
etc.
but for a single word and an int as one input then:-
Scanner input = new Scanner(System.in);
System.out.println("Type in a single word, a space, then an integer");
System.out.println("on a single line:-");
Sring[] mystring = input.nextLine().split(" ");
String myWord = mystring[0];
int myInt = Integer.parseInt(mystring[1]);
System.out.println(myWord + " " + myInt);
input.close();
+ 1
Thanks to everyone for the replies. Indeed, I have gotten what I need.
Cheers!