Results 1 to 5 of 5
Thread: Help with arrays
- 12-03-2011, 07:14 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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:
Here are 2 things that happen when I run it.Java 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. } }
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
- 12-03-2011, 07:23 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Help with arrays
You're printing the message here but doing nothing else to change the flow of the program. Try decrementing i after the println statement.Java 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"); }
- 12-03-2011, 07:33 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: Help with arrays
So something like this?
when I try to run it I get this error after typing in a negative number:Java 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--);} }
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)
- 12-03-2011, 07:40 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Help with arrays
You want to be doing something like this:
Java Code:int input = scanner.nextInt(); while (input < 0) { System.out.println("Please enter a positive number"); input = scanner.nextInt(); } candidates[i] = input;
- 12-03-2011, 08:56 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
how to ask how many arrays you want
By jepoy in forum New To JavaReplies: 5Last Post: 09-06-2010, 03:19 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Arrays
By TheRocket in forum New To JavaReplies: 6Last Post: 12-10-2008, 06:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks