-1

我试图用包含空格的文本文件填充字符串的arraylist,使得arraylist中的每个字符串之间将包含一个空格其中的第一个和第二个数字,并且没有前导或尾随空格。我不幸遇到了以下错误顺序运行它时:java.util.NoSuchElementException当使用.equals,arraylist和读取文件与扫描仪

Exception in thread "main" java.util.NoSuchElementException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at scannerTest.main(scannerTest.java:26) 

以下是我的代码:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class scannerTest 

{ 

private static ArrayList<String> myStore = new ArrayList<String>(); 

public static void main(String[] args) 
{ 
    int id = 0; 
    int inv = 0; 
    boolean idStarted = false; 
    boolean idFinished = false; 
    try 
    { 
     Scanner file = new Scanner(new File("file50.txt")); 
     System.out.println("File: " + "file50.txt"); 
     while (file.hasNextLine()) 
     { 
      for (int b = 0; b < (file.nextLine()).length(); b++) 
      { 
       if (!file.next().equals(" ")) 
       { 
        id = id + Integer.parseInt(file.next()); 
        idStarted = true; 
       } 
       if (idStarted = true && file.next().equals(" ")) 
       { 
        idFinished = true; 
       } 
       if (idFinished = true && !file.next().equals(" ")) 
        inv = inv + Integer.parseInt(file.next()); 
      } 
      String result = Integer.toString(id) + " " + Integer.toString(inv); 
      myStore.add(result); 
     } 
    }  
    catch (FileNotFoundException e) 
    { 
     System.out.println("ERROR: File '" + "file50.txt" + "' not found!"); 
    } 
    System.out.println(myStore); 
} 

}

文件看起来是这样的:

3679  87 
    196  60 
17914  12 
18618  64 
    2370  65 
    584  85 
18524  34 
12024   5 
    6992  76 
18410  56 
    9267  68 
18995  56 
    6265  58 
    6835  94 
14151  82 
11485  39 
15559  33 
18465  27 
19967  45 
13520  38 
    5529  11 
    3433   5 
17911  96 
18181  60 
11549  88 
14896  81 
    184  14 
    4329  64 
18871  69 
15141  87 
11584  32 
14088  92 
18061   3 
    206  31 
13066   8 
19623  88 
12705  14 
    9351   8 
17753  70 
15917  51 
    768  85 
15814  60 
15320  82 
    8303  90 
    7282  73 
14092  48 
10629  19 
12917  63 
15006  53 
12328  63 

我看不出为什么错误发生在第26行:

if (!file.next().equals(" ")) 

我想要做的是检查一行中的下一个对象是否为空格,如果是这种情况,请忽略它。任何有关这个问题的帮助将不胜感激。

+0

每次你调用一个以'next'开头的方法时,你正在读取它。至少有两种情况会立即跳出对你的代码进行扫视,你会丢掉你读到的东西 –

+0

http://docs.oracle.com/javase/tutorial/essential/io/scanning.html –

回答

1

如果您想重复使用,则需要将file.next()的值存储在变量中。

调用next会增加它指向的位置。

尝试

String val = file.next(); 

if (val.equals (" ") 
    . 

    . 

if (idStarted = true && val.equals(" ")) 
+0

我明白你的意思了,在这里说,我实现了它,但我仍然得到同样的错误。 – user3141179

+0

您使用nextLine也会导致此问题。使用getLine获取字符串,然后解析该字符串。不需要再使用扫描仪。 –

0

你可以修剪你的输入,并分配到一个字符串。那么因为每行只有两个索引,所以可以用空格分隔符将它们拆分为一个数组,并用空格将它们连接在一起。

public static void main(String[] args) 
{ 
    try 
    { 
     Scanner file = new Scanner(new File("test1/file50.txt")); 
     System.out.println("File: " + "file50.txt"); 
     while (file.hasNextLine()) 
     { 
      String test = file.nextLine().trim(); 
      String[] array = test.split("\\s+"); 

      myStore.add(array[0]+" "+array[1]); 
     } 

    } 
    catch (FileNotFoundException e) 
    { 
     System.out.println("ERROR: File '" + "file50.txt" + "' not found!"); 
    } 

    for(int i=0;i<myStore.size();i++){ 
     System.out.println(myStore.get(i)); 
    } 

}