ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用循环JAVA读取文本文件

2019-11-22 14:12:53  阅读:165  来源: 互联网

标签:loops input java


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

在我的BlueJ窗口中说

java.lang.ArrayIndexOutOfBoundsException: 15

终端中的错误显示:

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

38在主要方法中:

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

解决方法:

您正在代码中读取两次投票.

您的密码

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();
    }
}

标签:loops,input,java
来源: https://codeday.me/bug/20191122/2060385.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有