Your problem is in this line:
userInput.nextLine().charAt(0);
The nextLine() method scans everything on the current line and then advances the pointer past that line. So when you call the charAt() method, you are calling it on the next line, which is blank space, and thus an error is occuring.
Instead, change this line to:
userInput.next().charAt(0)
Note, this means other parts of your code will need changed too.
Edit:
Was about to edit my solution, but @Marc-Andre added his answer which covers it, so just cast your eyes over it too.
Answer from Andrew Martin on Stack OverflowI'm writing a program that needs to iterate through a string and print the first letter of every word. I have the program working but I can't submit it because of a "String index out of range" error.
An example of what needs to happen is this:
Please type in a sentence: Humpty Dumpty sat on a wall
Output:
H
D
s
o
a
w
Here's my code:
sent = input("Please type in a sentence: ")
index = 0
sent_length = len(sent)
space = " "
print(sent[index])
while index <= sent_length:
index += 1
if sent[index] == space:
print(sent[index + 1])I'm getting the correct output but it says I have a "string index out of range" error on line 8. I can't figure out what's wrong with it
java - StringIndexOutOfBoundsException String index out of range: 0 - Stack Overflow
String index out of range - Oracle Forums
Found a bug : java.lang.StringIndexOutOfBoundsException: String index out of range: 0 in Http2ServerRequestImpl
Unexpected error: String index out of range: -5 cryptic messages
Does StringIndexOutOfBoundsException indicate index corruption?
Does StringIndexOutOfBoundsException cause data loss?
What's the fastest way to diagnose StringIndexOutOfBoundsException in production?
Videos
Your problem is in this line:
userInput.nextLine().charAt(0);
The nextLine() method scans everything on the current line and then advances the pointer past that line. So when you call the charAt() method, you are calling it on the next line, which is blank space, and thus an error is occuring.
Instead, change this line to:
userInput.next().charAt(0)
Note, this means other parts of your code will need changed too.
Edit:
Was about to edit my solution, but @Marc-Andre added his answer which covers it, so just cast your eyes over it too.
The problem when you're doing age = userInput.nextInt(); is that you've probably enter a number say 4 and then press Enter.
So the scanner read 4 when you're calling nextInt but the new line is not consume. That means that when you do : userInput.nextLine().charAt(0); you're consuming the new line, so the the nextLine() will return an empty String. Since you're doing chartAt on an empty String, it give you an Exception.
You could do:
age = userInput.nextInt();
userInput.nextLine();
This will consume the new line, so the stream should be empty. So you won't have the exception and you can ask for the next input.
The problem occurs when line is empty (e.g. ""). Then it doesn't have a character at index 0, hence your error.
To fix, you can check the length of line before you use charAt:
System.out.println(line);
char x;
if (line.length() < 1) {
System.out.println("next line is empty");
} else {
x = line.charAt(0);
while((line.charAt(0)!='/')&&(Character.isWhitespace(x)==false))
{
line = inputFile.nextLine();
if (line.length() < 1) {
System.out.println("next line is empty");
break;
}
x = line.charAt(0);
System.out.println(line);
System.out.println(x);
}
}
Seems like line=="" , first check if the line String has some content and then try to invoke charAt() .
if(line!=null && line.length()>0) // perform this check to be safe.
Look at the documentation of String#charAt().
Throws:
IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.