-
Help with arrays
Hi, I'm writing a program that is supposed to accept a candidates number and number of votes and then print them in (number, votes) form. I've got it pretty much figured out except for one part where I'm supposed to disregard an invalid vote (e.g. -999). I've tried a if/else code but I'm not sure how to make the if statement disregard the previous input. Here's my code for you guys to check:
Code:
import java.util.Arrays;
import java.util.Scanner;
public class VoteTally {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] candidates = new int[10]; //declaring arrays
int[] candidatenum= new int[10];
for (int i = 0; i < candidates.length; i++) { //stating that as long as i is less than candidate[10]'s length, keep looping i++
System.out.print("Enter the candidate number: "); //getting the number of the candidate
candidatenum[i] = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter the candidate's votes: "); //getting the number of votes for candidate
candidates[i] = scanner.nextInt();
scanner.nextLine();
if (candidates[i]< 0) //my attempt an an exception for a negative input for candidates[i]
System.out.println("Invalid vote please try again");
}
for (int i = 0; i < candidates.length; i++)
System.out.println("Candidate "+candidatenum[i]+ ": " +candidates[i]); //printing the arrays side by side within the loop.
}
}
Here are 2 things that happen when I run it.
Example 1:
Enter the candidate number: 1
Enter the candidate's votes: 1
Enter the candidate number: 2
Enter the candidate's votes: 2
Enter the candidate number: 3
Enter the candidate's votes: 3
Enter the candidate number: 4
Enter the candidate's votes: 4
Enter the candidate number: 5
Enter the candidate's votes: 5
Enter the candidate number: 6
Enter the candidate's votes: 6
Enter the candidate number: 7
Enter the candidate's votes: 7
Enter the candidate number: 8
Enter the candidate's votes: 8
Enter the candidate number: 9
Enter the candidate's votes: 9
Enter the candidate number: 10
Enter the candidate's votes: 10
Candidate 1: 1
Candidate 2: 2
Candidate 3: 3
Candidate 4: 4
Candidate 5: 5
Candidate 6: 6
Candidate 7: 7
Candidate 8: 8
Candidate 9: 9
Candidate 10: 10
Example 2:
Enter the candidate number: 1
Enter the candidate's votes: 1
Enter the candidate number: 2
Enter the candidate's votes: 2
Enter the candidate number: 3
Enter the candidate's votes: 3
Enter the candidate number: 4
Enter the candidate's votes: 4
Enter the candidate number: 5
Enter the candidate's votes: 5
Enter the candidate number: 6
Enter the candidate's votes: 6
Enter the candidate number: 7
Enter the candidate's votes: 7
Enter the candidate number: 8
Enter the candidate's votes: -999
Invalid vote please try again
Enter the candidate number: 9
Enter the candidate's votes: 9
Enter the candidate number: 10
Enter the candidate's votes: 10
Candidate 1: 1
Candidate 2: 2
Candidate 3: 3
Candidate 4: 4
Candidate 5: 5
Candidate 6: 6
Candidate 7: 7
Candidate 8: -999 <--- I'm try to disregard that.
Candidate 9: 9
Candidate 10: 10
Thanks for any help you might have. I'm learning, but some things just make me :frusty:
-
Re: Help with arrays
Code:
System.out.print("Enter the candidate's votes: "); //getting the number of votes for candidate
candidates[i] = scanner.nextInt();
scanner.nextLine();
if (candidates[i]< 0) //my attempt an an exception for a negative input for candidates[i]
System.out.println("Invalid vote please try again");
}
You're printing the message here but doing nothing else to change the flow of the program. Try decrementing i after the println statement.
-
Re: Help with arrays
So something like this?
Code:
System.out.print("Enter the candidate's votes: "); //getting the number of votes for candidate
candidates[i] = scanner.nextInt();
scanner.nextLine();
if (candidates[i]< 0){ //my attempt an an exception for a negative input for candidates[i]
System.out.println("Invalid vote please try again");
for (;i < candidates.length; i--);}
}
when I try to run it I get this error after typing in a negative number:
Enter the candidate's votes: -9
Invalid vote please try again
Enter the candidate number: 10
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147483648
at VoteTally.main(VoteTally.java:12)
-
Re: Help with arrays
You want to be doing something like this:
Code:
int input = scanner.nextInt();
while (input < 0) {
System.out.println("Please enter a positive number");
input = scanner.nextInt();
}
candidates[i] = input;
-
Re: Help with arrays
Oh I see! Thanks I'll try that