使用for循环时字符串超出界限[重复]

时间:2021-05-27 04:18:00

This question already has an answer here:

这个问题已经有了答案:

My code below has a exception error for:

我下面的代码有一个异常错误:

YN = input.nextLine().charAt(0); //line 13

when it is run. The assignment is to make an array that is assigned 10 numbers from console, when I put in 10 numbers the run completes automatically and gives an error for line 13. Is there something wrong with line 13 or is the issue with something else?

当它运行。分配的任务是创建一个从控制台分配10个数字的数组,当我输入10个数字时,运行将自动完成,并为第13行提供一个错误。第13行有什么问题吗?还是有别的问题?

(The array must be a normal array and not an arrayList)

(数组必须是普通数组,而不是arrayList)

import java.util.*;
import java.util.Arrays;

public class CountOccurrences {
    static Scanner input = new Scanner(System.in);
    static int[][] temp = new int[10][1];

    public static void main(String[] args) {
        char YN = 'y';

        while (YN == 'y') {
            run();
            System.out.print("Continue? (y or n)\t");
            YN = input.nextLine().charAt(0); // Line 13
        }
    }

    public static void run() {
        System.out.print("Enter the integers between 1 and 100: ");

        int[] numbersArray = new int[10];

        for (int i = 0; i < numbersArray.length; i++) {
            numbersArray[i] = input.nextInt();
        }

        for (int i = 0; i < numbersArray.length; i++) {
            Arrays.sort(numbersArray);
            System.out.println(numbersArray[i]);
        }
    }
}

1 个解决方案

#1


4  

input.nextLine() could be an empty String, in which case input.nextLine().charAt(0) will give you that exception. You should check the length() of the String before calling charAt.

inpuot . nextline()可以是一个空字符串,在这种情况下,input.nextLine(). charat(0)会给您这个异常。在调用charAt之前,应该检查字符串的长度()。

Now that I see you are using input.nextInt() to read the inputs in your run() method, what you have to do is add a input.nextLine() at the end of the run() method, to consume the end of the line that contains the final int.

现在我看到您正在使用input.nextInt()来读取run()方法中的输入,您需要做的是在run()方法的末尾添加一个inpu . nextline(),以使用包含最终int类型的行尾。

You still have to check the length of the String you get in input.nextLine(), though, since the user may hit enter instead of hitting Y or N, which will result in the same exception.

不过,您仍然需要检查输入. nextline()中的字符串的长度,因为用户可能会按enter而不是按Y或N,这将导致相同的异常。

#1


4  

input.nextLine() could be an empty String, in which case input.nextLine().charAt(0) will give you that exception. You should check the length() of the String before calling charAt.

inpuot . nextline()可以是一个空字符串,在这种情况下,input.nextLine(). charat(0)会给您这个异常。在调用charAt之前,应该检查字符串的长度()。

Now that I see you are using input.nextInt() to read the inputs in your run() method, what you have to do is add a input.nextLine() at the end of the run() method, to consume the end of the line that contains the final int.

现在我看到您正在使用input.nextInt()来读取run()方法中的输入,您需要做的是在run()方法的末尾添加一个inpu . nextline(),以使用包含最终int类型的行尾。

You still have to check the length of the String you get in input.nextLine(), though, since the user may hit enter instead of hitting Y or N, which will result in the same exception.

不过,您仍然需要检查输入. nextline()中的字符串的长度,因为用户可能会按enter而不是按Y或N,这将导致相同的异常。