-1

我正在尝试使用包含空格的文本文件填充字符串数组列表,这样数组列表中的每个字符串都将在其中的第一个和第二个数字之间包含一个空格,并且没有前导或尾随空格。不幸的是,我在运行它时遇到了以下错误序列:

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(" "))

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

4

2 回答 2

1

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

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

尝试

String val = file.next ();

if (val.equals (" ")
   .

   .

 if (idStarted = true && val.equals(" ")) 
于 2014-01-22T02:43:31.547 回答
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));
    }

}
于 2014-01-22T05:45:56.297 回答