使用.equals,arraylist和使用scanner扫描程序读取文件时的java.util.NoSuchElementException

时间:2014-01-22 02:37:13

标签: java file arraylist java.util.scanner nosuchelementexception

我正在尝试使用包含空格的文本文件填充字符串的arraylist,这样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(" "))

我正在尝试做的是检查一行中的下一个对象是否为空格,如果是这种情况则忽略它。任何有关此事的帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

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

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

尝试

String val = file.next ();

if (val.equals (" ")
   .

   .

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

答案 1 :(得分: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));
    }

}