Java 字符串索引超出范围:0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18364648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java string index out of range: 0
提问by user2704743
I have this problem where as soon as I enter my first input the program crashes and I get
我有这个问题,一旦我输入我的第一个输入,程序就会崩溃,我得到
String index out of range: 0
字符串索引超出范围:0
I've looked elsewhere and tried to find my mistakes but I found different problems which aren't what I had. Could someone please tell me where have I gone wrong?.
我在别处寻找并试图找出我的错误,但我发现了不同的问题,这些问题不是我所拥有的。有人可以告诉我我哪里出错了吗?
Thanks for your help, here is the code:
感谢您的帮助,代码如下:
import java.util.Scanner;
public class Assignment1Q2 {
public static void main(String[] args) {
System.out.println("Thank you for your call,\nPlease take some time to answer a few questions");
collectData();
}//end of main
public static void collectData() {
Scanner userInput = new Scanner(System.in);
int age;
char gender;
char show;
int over30MY = 0, over30FY = 0, under30MY = 0, under30FY = 0;
int over30MN = 0, over30FN = 0, under30MN = 0, under30FN = 0;
System.out.println("\nWhat is your age?\n");
age = userInput.nextInt();
System.out.println("Male or Female (Enter M or Y)");
gender = userInput.nextLine().charAt(0);
gender = Character.toLowerCase(gender);
System.out.println("Do you watch the show regularly? (Enter Y or N)");
show = userInput.nextLine().charAt(0);
show = Character.toLowerCase(show);
if((age > 30) && (gender == 'm') && (show == 'y')) {
over30MY++;
}
else if((age > 30) && (gender == 'f') && (show == 'y')) {
over30FY++;
}
else if((age < 30) && (gender == 'm') && (show == 'y')) {
under30MY++;
}
else if((age < 30) && (gender == 'f') && (show == 'y')) {
under30FY++;
}
else if((age > 30) && (gender == 'm') && (show == 'n')) {
over30MN++;
}
else if((age > 30) && (gender == 'f') && (show == 'n')) {
over30FN++;
}
else if((age < 30) && (gender == 'm') && (show == 'n')) {
under30MN++;
}
else if((age < 30) && (gender == 'f') && (show == 'n')) {
under30FN++;
}//end of if else
}//end of collectData
}// end of class
采纳答案by Andrew Martin
Your problem is in this line:
你的问题在这一行:
userInput.nextLine().charAt(0);
The nextLine() method scans everything on the current line and then advances the pointer pastthat 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.
nextLine() 方法扫描当前行上的所有内容,然后将指针移过该行。所以当你调用 charAt() 方法时,你是在下一行调用它,这是空格,因此发生错误。
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.
正要编辑我的解决方案,但@Marc-Andre 添加了他的答案,涵盖了它,所以也只需将目光投向它。
回答by Marc-Andre
The problem when you're doing age = userInput.nextInt();
is that you've probably enter a number say 4 and then press Enter.
执行时的问题age = userInput.nextInt();
是您可能输入了一个数字,例如 4,然后按 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.
因此,当您打电话时,扫描仪会读取 4,nextInt
但不会消耗新行。这意味着当您这样做时:userInput.nextLine().charAt(0);
您正在使用新行,因此nextLine()
将返回一个空字符串。由于您正在chartAt
处理一个空字符串,因此它会给您一个异常。
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.
这将消耗新行,因此流应该为空。所以你不会有例外,你可以要求下一个输入。