java - 使用数组时出现 java.util.nosuchelementException 错误?

标签 java arrays

对于我的程序,我将使用数组从数据文件中分离正数和负数。我能够编译该程序,但当我运行 main 时,出现错误 java.util.nosuchelementexception。我不太确定错误发生在哪里以及为什么会收到它。非常感谢一些帮助。

这是程序

import java.util.Scanner;
import java.io.*;
public class Prog404aAP
{
public static void main(String args[])
{
    //declares variables and arrays;
    int[] pos = new int [13];
    int[] neg = new int [13];
    int newFile; 

    //sets up kbrader
    Scanner kbReader=new Scanner(System.in); 

    //sets up scanner
    Scanner inFile = null;

    //input for 1st set of data
     try
    {
        inFile = new Scanner(new File("prog404a1.dat"));
    }
    catch (FileNotFoundException e)
    {
        System.out.println("File not found!");
        System.exit(0);
    }


    System.out.println("Run File? 1 for yes 2 for no. Only 2 files are available to run");
    int decision = kbReader.nextInt();

    while( decision == 1 )
    {

    //header 
    System.out.println("Positive \t Negative");


    //stores the numbers from the file into both arrays depending on value
    for(int index = 0; index < 13; index++)
    {
        if(inFile.nextInt() < 0)
        {
           pos[index] = inFile.nextInt(); 
        }
        else
        {
            neg[index] = inFile.nextInt();
        }
    }

        //for loop for formatted output
        for(int index = 0; index < 13; index++)
        {
            System.out.println( pos[index] +"\t  " +neg[index]);

        }

    System.out.println("Run File? 1 for yes 2 for no. Only 2 files are available to run");
    newFile = kbReader.nextInt();

    if(newFile == 1)
    {
        decision = 1;
        //sets up scanner 
    inFile = null;

    //input for 2nd set of data
     try
    {
        inFile = new Scanner(new File("prog404a2.dat"));
    }
    catch (FileNotFoundException e)
    {
        System.out.println("File not found!");
        System.exit(0);
    }

}
else
{
    decision = 2;
}

}
}
}

这是我的数据文件 prog404a1

3
66
54
-8
22
-16
-56
19
21
34
-34
-22
-55
-3
-55
-76
64
55
9
39
54
33
-45

这是我的 prog404a2 数据文件

 51
-66
-54
-22
-19
8
10
56
34
22
55
3
55
76
45
-21
-34
-64
-55
-9
-89
-54
-3
32
45
-25

堆栈跟踪

java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Prog404aAP.main(Prog404aAP.java:56)

最佳答案

for(int index = 0; index < 13; index++)
{
    if(inFile.nextInt() < 0)
    {
       pos[index] = inFile.nextInt(); 
    }
    else
    {
        neg[index] = inFile.nextInt();
    }
}

您为每个值调用 nextInt() 两次。当到达第 7 个元素时,您实际上是在尝试读取不存在的第 14 个元素,并导致此异常。您需要将值放入变量中:

for(int index = 0; index < 13; index++)
{
    int nextVal = inFile.nextInt();
    if(nextVal  < 0)
    {
       pos[index] = nextVal ; 
    }
    else
    {
        neg[index] = nextVal ;
    }
}

关于java - 使用数组时出现 java.util.nosuchelementException 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48814553/

相关文章:

java - 不太了解Struts2 XWork中依赖注入(inject)的机制

将 CSV 转换为 XML 文件的 Java lib 或应用程序?

java - Java 中的 SAX 解析器

javascript - 如何将菊花链/点表示法中的 JavaScript 对象展开为具有嵌套对象和数组的对象?

php - 两次查询输出数据

c - 如何从 C 中的函数返回字符数组?

java - 限制某些方法的有效负载 CommonsRequestLoggingFilter Spring

java - 无法显示高分辨率 PNG 图像

c - 使用 while 循环理解文件 I/O

arrays - 在clojure中将字节转换为字符串