Java字符串索引超出范围:0。

时间:2022-11-20 16:41:57

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

2 个解决方案

#1


12  

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.

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添加了他的答案,所以你也可以把目光放在上面。

#2


7  

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然后按回车键。

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.

所以当你调用nextInt时,扫描器读取4,但新行不使用。这意味着当您执行: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.

这将消耗新行,所以流应该是空的。所以你不会有例外,你可以要求下一个输入。

#1


12  

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.

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添加了他的答案,所以你也可以把目光放在上面。

#2


7  

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然后按回车键。

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.

所以当你调用nextInt时,扫描器读取4,但新行不使用。这意味着当您执行: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.

这将消耗新行,所以流应该是空的。所以你不会有例外,你可以要求下一个输入。