【问题标题】:Reading a text file with a loop JAVA用循环JAVA读取文本文件
【发布时间】:2013-11-22 22:06:33
【问题描述】:

我的程序读取一组数据,对其进行处理,打印内容,然后应该继续处理下一组数据,直到没有更多数据为止。如果我给它一组数据,我的程序就可以工作,但是当我添加第二个时我得到一个错误。

在我的 BlueJ 窗口中显示

java.lang.ArrayIndexOutOfBoundsException: 15

终端中的错误提示:

java.lang.ArrayIndexOutOfBoundsException: 15
at Prog4.read_votes(Prog4.java:97)
at Prog4.main(Prog4.java:38)

38 在 main 方法中:

p4.read_votes(stdin, votesArray);

97 是:

votes[i] = stdin.nextInt();

它在 read_votes 方法中:

public void read_votes(Scanner stdin, int votes[])
{
 //for (int i = 0; i < votes.length; i++)
 //   votes[i] = stdin.nextInt();  

 int i = 0;

  while (stdin.hasNextInt())
  {
     votes[i] = stdin.nextInt();
     i++;
  }
}

这是我的类和主要方法:

public class Prog4
{
int mostVotes = 0;
int mostVotesIndex = 0;
int fewestVotes = 0;
int fewestVotesIndex = 0;

public static void main(String args[]) throws Exception
{
  //Scanner stdin = new Scanner(System.in);
  Scanner stdin = new Scanner(new File("test.txt"));
  int num_votes, num_candidates, election_num = 1;



  System.out.println("Welcome to Prog4!\n");
  Prog4 p4 = new Prog4();

  while (stdin.hasNextLine())
  {

     System.out.println("Results for election " + election_num);

     num_candidates = stdin.nextInt();
     String[] candidateArray = new String[num_candidates];
     p4.read_candidates(stdin, candidateArray);

     num_votes = stdin.nextInt();
     int[] votesArray = new int[num_votes];
     p4.read_votes(stdin, votesArray);


     p4.read_votes(stdin, votesArray);
     System.out.println("Total votes: " + votesArray.length);
     p4.process_highest_vote_getter(candidateArray, votesArray);
     p4.process_lowest_vote_getter(candidateArray, votesArray);
     p4.process_winner(candidateArray, votesArray);
  }

System.out.println("Done. Normal termination of Prog4.");
}

这是从文本文件中读取的数据:

4
Owen
Jack
Scott
Robbie
15 0 1 1 2 3 1 0 0 0 0 1 2 2 1 1

2
Erik
Martin
10 0 1 0 1 0 1 0 0 0 1

编辑:

我将 read_votes 方法更改为:

public void read_votes(Scanner stdin, int votes[])
{
  for (int i = 0; i < votes.length && stdin.hasNextInt(); i++)
     votes[i] = stdin.nextInt();  
}

它为第一组数据提供了正确的输出,但是当它开始执行第二组数据时出现错误。有错误显示:

java.util.InputMismatchException

它在 main 方法的这一行: num_candidates = stdin.nextInt();

【问题讨论】:

    标签: java loops input


    【解决方案1】:

    您在代码中阅读了两次投票。

    你的代码

    int[] votesArray = new int[num_votes];
     **p4.read_votes(stdin, votesArray);**
    
    
     **p4.read_votes(stdin, votesArray);**
    

    当它发生读取格式化数据时会被破坏。

    我已经对您的代码进行了以下更改。然后就可以正常工作了。

    public void read_votes(Scanner stdin, int votes[])
    {
        //for (int i = 0; i < votes.length; i++)
        //   votes[i] = stdin.nextInt();
    
    
        for(int i = 0; i < votes.length; ++i)
        {
            votes[i] = stdin.nextInt();
        }
    }
    
    public void read_candidates(Scanner stdin, String candidates[])
    {
        //for (int i = 0; i < votes.length; i++)
        //   votes[i] = stdin.nextInt();
    
        stdin.nextLine();
        for(int i = 0; i < candidates.length; ++i)
        {
            candidates[i] = stdin.nextLine();
        }
    }
    

    【讨论】:

    • 啊,是的,我读了两遍。我只需要删除其中一个读数!谢谢!
    【解决方案2】:

    只要输入中有nextInts,您就可以继续阅读它们。特别是,您正在阅读数字 2(表示下次选举的候选人人数)并试图将其算作选举 1 的投票。

    相反,在您的read_votes 方法中从0 迭代到votes.length;如果您保证输入有效(或者在输入错误时抛出异常是可以接受的),那么您不需要不断检查hasNextInt()

    【讨论】:

    • 这是有道理的。在我的代码中,我注释掉了我为 read_cotes 方法尝试的一些内容。它应该可以解决您所说的问题,但我得到了同样的错误。新方法是这样的: public void read_votes(Scanner stdin, int votes[]) { for (int i = 0; i
    【解决方案3】:

    read_votes()中的循环改成:

    while(i < votes.length && stdin.hasNextInt()) {
        // ...
    }
    

    您的循环从下一行读取数字。

    【讨论】:

    • 我试过了,我得到一个 InputMismatchException 在线:num_candidates = stdin.nextInt();在我的主要方法中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多