Can you input string and number in one input in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Can you input string and number in one input in java?

I'm making a caeser cipher program with encrypt and decrypt!

13th Jun 2019, 4:42 PM
CodeFu
CodeFu - avatar
7 Answers
+ 5
Codinger 🖥️🇲🇰 my older post means input like this: "hello123" As ~ swim ~ posted you can also keep it separate: Scanner scan = new Scanner(System.in); int number = scan.nextInt(); String text = scan.nextLine();
13th Jun 2019, 5:26 PM
Denise Roßberg
Denise Roßberg - avatar
+ 9
This question was for this code: https://code.sololearn.com/cgtwNMddInXm/?ref=app
13th Jun 2019, 7:29 PM
CodeFu
CodeFu - avatar
+ 8
Thanks,Denise!
13th Jun 2019, 7:22 PM
CodeFu
CodeFu - avatar
+ 4
You have to use a String. Scanner scan = new Scanner(System.in); String input = scan.nextLine() or scan.next() Then you can work with the characters of your String. A char can be everything: letter, number, special character.
13th Jun 2019, 4:58 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
Anhjje 🐿 It depends on what you want to do. If you want to manipulate the chars, you should use an char array. String str = "hello"; char[] c = str.toCharArray(); If you don't manipulate the chars (e.g. if you count numbers or vowels) then you can use a loop. To get a char of a String -> .charAt(int index) Here is an simple example: String str = "hello"; for(int i = 0; i < str.length(); i++){ System.out.println(str.charAt(i)); }
13th Jun 2019, 5:18 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
If you want to accept both a string and a number input then string is the best to use, just something like this Scanner s = new Scanner(System.in); String input = s.nextLine()
20th Jun 2019, 2:05 PM
Mansur Ibrahim Nok
Mansur Ibrahim Nok - avatar
0
So basically, turn a string into an array of characters? Am I getting this right Denise Roßberg 😁
13th Jun 2019, 5:08 PM
HNNX 🐿
HNNX 🐿 - avatar