+ 6
Can you input string and number in one input in java?
I'm making a caeser cipher program with encrypt and decrypt!
7 ответов
+ 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();
+ 9
This question was for this code:
https://code.sololearn.com/cgtwNMddInXm/?ref=app
+ 8
Thanks,Denise!
+ 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.
+ 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));
}
+ 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()
0
So basically, turn a string into an array of characters? Am I getting this right Denise Roßberg 😁