扫描程序问题:NoSuchElementException

时间:2013-02-11 19:18:04

标签: java

我正在创建一个类来从文件中读取一个对象,之前保存为文件(.txt格式),但在尝试加载文件时收到以下错误:

  

java.util.NoSuchElementException

该方法以这种方式进行,因此用户应该能够输入文件的名称。即使已经尝试调试abit,也只是引用已经给出名称的文件本身(不接受输入)。这是方法的外观:

public void load( String filename ) throws FileNotFoundException 
{
    final String FIELD_STOP = ":";
    String loadFile;
    loadFile = filename +".txt";

    FileReader ansfil = new FileReader( loadFile );
    Scanner fileIn = new Scanner( ansfil );
    fileIn.useLocale( new Locale( "en" ) );

    System.out.println(loadFile);
    fileIn.useDelimiter( FIELD_STOP );
    int size;

    try
    {
        while( fileIn.hasNext() )
        {
            size = fileIn.nextInt();
            archive = new CDarkivImplemented( size );
            CD readIn = new CDImplemented();

            for( int i = 0; i <  size ; i++ )
            {
                readIn = new CDImplemented();
                readIn.title = fileIn.next();
                readIn.artist = fileIn.next();
                readIn.genre = CD.interpretGenre( fileIn.next() );
                readIn.publisher = fileIn.next();
                readIn.year = fileIn.nextInt();
                readIn.ID = fileIn.nextInt();
                archive.addCD(readIn);
            }

        }

    }

    catch(Exception e)
    {
        System.out.println( "Wrong! : " + e );
        System.exit(1);
    }
    fileIn.close();
}

3 个答案:

答案 0 :(得分:2)

怀疑这就是问题:

for( int i = 0; i <=  size ; i++ )

该循环将执行size + 1次,这可能不是你的意思。尝试:

for (int i = 0; i < size; i++)

看看是否有帮助...

(顺便说一下,我在循环中声明readIn变量 - 没有必要把它放在外面。我也是使用finally块关闭文件。)

答案 1 :(得分:0)

public void load( String filename ) throws FileNotFoundException {
   final String FIELD_STOP = ":";
   final String loadFile = filename +".txt";
   try( FileReader ansfil = new FileReader( loadFile )) {
      Scanner fileIn = new Scanner( ansfil );
      fileIn.useLocale( new Locale( "en" ) );
      System.out.println(loadFile);
      fileIn.useDelimiter( FIELD_STOP );
      int size;
      while( fileIn.hasNext()) {
         size = fileIn.nextInt();
         archive = new CDarkivImplemented( size );
         for( int i = 0; fileIn.hasNext() && i < size; i++ ) {
            String title = fileIn.next();
            if( ! fileIn.hasNext()) break;
            String artist = fileIn.next();
            if( ! fileIn.hasNext()) break;
            String genre = fileIn.next();
            if( ! fileIn.hasNext()) break;
            String publisher = fileIn.next();
            if( ! fileIn.hasNext()) break;
            int year = fileIn.nextInt();
            if( ! fileIn.hasNext()) break;
            int ID = fileIn.nextInt();
            archive.addCD(
               new CDImplemented(
                  title, artist, genre, publisher, year, ID ));
         }
      }
   }
   catch(Exception e)
   {
       System.out.println( "Wrong! : " + e );
       System.exit(1);
   }
}

答案 2 :(得分:0)

while循环是什么?正如我从您对文件格式的评论中推断的那样,您可能不需要这样做,并且可能导致错误,因为它在读取完最后一个元素后再循环一次。