+ 2
Can someone explain how to use charAt.
5 Answers
+ 6
@Karl
https://code.sololearn.com/cmLXU0UfYQhi/#java
Scanner scnr = new Scanner(System.in);
String myInput = "";
System.out.println("Please enter your input: ");
myInput = scnr.nextLine();
System.out.println("You entered: " + myInput);
System.out.println("Char at (1) is: " + myInput.charAt(1));
+ 9
String.charAt(index) function is typically used when you need to get a character present in a String at any given index (first index is 0, same as in an array).
For example, if you have a String named "Karl" and you want to get the letter 'a' from it, you'd use the charAt() function.
String str = "Karl";
char ch = str.charAt(1);
// Stores 'a' in ch as it is present at the 1-th index of the String str.
+ 4
SYNTAX: charAt(index);
Think of a String as an array with each element an individual character of the string. charAt(index) allows you to get the specific character that's stored at the index specified.
::::EXAMPLE::::
https://code.sololearn.com/cIHg4067MFrk/#java
String myStr = "Karl Jude";
System.out.println(myStr.charAt(0)); // prints K
System.out.println(myStr.charAt(1)); // prints A
System.out.println(myStr.charAt(2)); // prints R
System.out.println(myStr.charAt(3)); // prints L
System.out.println(myStr.charAt(4)); // prints " "
System.out.println(myStr.charAt(5)); // prints J
System.out.println(myStr.charAt(6)); // prints U
System.out.println(myStr.charAt(7)); // prints D
System.out.println(myStr.charAt(8)); // prints E
System.out.println("");
// HOW YOU CAN ITERATE THROUGH ENTIRE STRING
for(int i = 0; i < myStr.length(); ++i){
System.out.print(myStr.charAt(i));
}
+ 3
@Karl, no. The scanner object needs it methods to get an input first.
char ch = word.nextLine().charAt(1);
+ 2
Is this line correct if I use it with a scanner?
Scanner word = new scanner(system.in);
System.outprintln("input word:");
Ch = word.charAt(1);