使用数组时出错java.util.nosuchelementexception?

时间:2018-02-15 19:06:50

标签: 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)

3 个答案:

答案 0 :(得分:2)

nextInt()

您为每个值调用for(int index = 0; index < 13; index++) { int nextVal = inFile.nextInt(); if(nextVal < 0) { pos[index] = nextVal ; } else { neg[index] = nextVal ; } } 两次。当到达第7个元素时,您实际上是在尝试读取不存在的第14个元素并导致此异常。您需要将值放入变量:

{{1}}

答案 1 :(得分:0)

实际上你的文件“prog404a1.dat”包含23个元素而你正在调用inFile.nextInt()两次,当index = 12并且文件中没有第24个元素时,它基本上会尝试读取文件中的第24行。所以它抛出noSuchElementException。

答案 2 :(得分:0)

让我给你我的洗礼。我认为您可以使用标准Java命令将整个文件读取到字符串集合,而不是手动执行。并统一分割和打印数字的方法:

public class Prog404aAP {
    public static void main(String args[]) {
        try (Scanner scan = new Scanner(System.in)) {
            System.out.println("Run File? 1 for yes 2 for no. Only 2 files are available to run");

            if (scan.nextInt() == 1) {
                print(Paths.get("prog404a1.dat"));
                System.out.println();
                print(Paths.get("prog404a2.dat"));
            }
        }
    }

    private static void print(Path path) {
        try {
            List<String> values = Files.readAllLines(path);
            int[] pos = values.stream().mapToInt(str -> Integer.parseInt(str.trim())).filter(i -> i >= 0).toArray();
            int[] neg = values.stream().mapToInt(str -> Integer.parseInt(str.trim())).filter(i -> i < 0).toArray();

            System.out.println("Positive\tNegative");

            for (int i = 0, max = Math.max(pos.length, neg.length); i < max; i++) {
                String p = i < pos.length ? String.valueOf(pos[i]) : "\t";
                String n = i < neg.length ? String.valueOf(neg[i]) : "\t";
                System.out.println(p + "\t\t\t" + n);
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}