0

这个程序读取文本文件,在这种情况下,读取这个:

Sam

5 0 0 0

Dave

5 5 0 0

Bill

5 -5 0 0

Louise

3 3 5 0

在我的程序早期,我执行以下 while 循环:

            int count = 0;


        while (input.hasNextLine())
        {

            count++;

            if (count % 2 == 1) //for every other line, reads the name
            {
                String line = input.nextLine(); 
                names.add(line); //puts name into array
            }

            if (count % 2 == 0) //for every other line, reads the ratings
            {
                ArrayList<Integer> rtemp = new ArrayList<Integer>();
                while (input.hasNextInt())
                {
                    int tempInt = input.nextInt();
                    rtemp.add(tempInt);
                }               
                allratings.add(rtemp);  

            }


        }

出于某种原因,当这个 while 循环完成时,int 计数为 14。我只希望它为 8,并认为它应该是,考虑到只有 8 行文本,它应该是为每一行执行。显然,这会在程序的后期引起很大的问题。知道发生了什么吗?

4

1 回答 1

0

从你的第二个循环看来,光标永远不会移动到下一行......我在你之后添加了下面的行 allratings.add(rtemp); 并且它起作用了。

if(input.hasNext())
  input.next();
于 2012-04-22T15:21:33.687 回答