+ 1

Scanner characters?

Does Java.util.Scanner have input for characters? (Char) Like nextChar or similar. Not string. I would want to make an Array of characters to be able to index Char arrays like you could index Strings in Python.

8th Oct 2018, 6:32 AM
Seb TheS
Seb TheS - avatar
3 Answers
+ 2
From what I've seen how people accept character here I've learned that String input is taken first, then use <String object>.charAt() method passing the desired character index to get the character. Alternatively you can convert a String object into a char array by using <String object>.toCharArray() method which returns a character array containing the String object contents. import java.util.Scanner; public class StringToCharArray { public static void main(String[] args) { Scanner reader = new Scanner(System.in); String s = reader.nextLine(); reader.close(); System.out.println("The string:\n" + s); char[] carr = s.toCharArray(); System.out.println("\nThe character array:"); for(int i = 0; i < carr.length; ++i) System.out.print(carr[i] + " "); } } Hth, cmiiw
8th Oct 2018, 7:00 AM
Ipang
+ 1
Ipang That's a lot what I've searched for, thanks!
8th Oct 2018, 7:07 AM
Seb TheS
Seb TheS - avatar
+ 1
You're welcome Seb TheS, happy coding : )
8th Oct 2018, 7:11 AM
Ipang