Reputation: 1217
My program reads a set of data, processes it, prints stuff, and then is supposed to continue on to the next set of data, until there is no more. My program works if I give it one set of data, but I get an error when I add a second.
In my BlueJ window it says
java.lang.ArrayIndexOutOfBoundsException: 15
The error in the terminal says:
java.lang.ArrayIndexOutOfBoundsException: 15
at Prog4.read_votes(Prog4.java:97)
at Prog4.main(Prog4.java:38)
38 is in the main method:
p4.read_votes(stdin, votesArray);
97 is:
votes[i] = stdin.nextInt();
and it's in the read_votes method:
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++;
}
}
Here is my class and main method:
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.");
}
Here is the data being read from the text file:
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
EDIT:
I changed the read_votes method to:
public void read_votes(Scanner stdin, int votes[])
{
for (int i = 0; i < votes.length && stdin.hasNextInt(); i++)
votes[i] = stdin.nextInt();
}
It gives the correct output for the first set of data, but I get an error when it starts doing the second set. There error reads:
java.util.InputMismatchException
and it's on this line in the main method: num_candidates = stdin.nextInt();
Upvotes: 3
Views: 11877
Reputation: 346
You are reading votes twice in your code.
Your code
int[] votesArray = new int[num_votes];
**p4.read_votes(stdin, votesArray);**
**p4.read_votes(stdin, votesArray);**
When it happens reading formatted data will be broken.
And I have done following changes to your code. then it'll work fine.
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();
}
}
Upvotes: 3
Reputation: 1141
Change the loop in read_votes()
to:
while(i < votes.length && stdin.hasNextInt()) {
// ...
}
Your loop reads the number(s) from the next line.
Upvotes: 0
Reputation: 77167
You're continuing to read nextInt
s as long as the input has them. In particular, you're reading the number 2
(indicating the number of candidates for the next election) and trying to count it as a vote for election 1.
Instead, iterate from 0
to votes.length
in your read_votes
method; if you're guaranteed valid input (or it's acceptable to throw exceptions on bad input), then you don't need to continually check hasNextInt()
.
Upvotes: 0