2

我正在创建一个类来从文件中读取对象,该对象之前已保存到文件(.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();
}
4

3 回答 3

2

怀疑这是问题所在:

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

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

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

看看是否有帮助...

(顺便说一句,我会在循环内声明readIn变量- 不需要将它放在外面。我还会使用块来关闭文件。)finally

于 2013-02-11T19:21:02.220 回答
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);
   }
}
于 2013-02-11T19:34:49.447 回答
0

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

于 2013-02-11T20:06:49.813 回答