How can I save just one input from the user to two distinct variables (such as int and string) in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I save just one input from the user to two distinct variables (such as int and string) in java?

20th Nov 2019, 7:36 PM
Mutiu Olagoke
Mutiu Olagoke - avatar
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.
20th Nov 2019, 9:21 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
You can take in a string and perform some string operations on it.
20th Nov 2019, 8:24 PM
Seb TheS
Seb TheS - avatar
+ 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();
20th Nov 2019, 8:45 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Thanks to everyone for the replies. Indeed, I have gotten what I need. Cheers!
21st Nov 2019, 5:55 AM
Mutiu Olagoke
Mutiu Olagoke - avatar