Results 1 to 5 of 5
  1. #1
    Kum4r is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default 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:

    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.
    			                                                                       
    }
    }
    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

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Help with arrays

    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");
                }
    You're printing the message here but doing nothing else to change the flow of the program. Try decrementing i after the println statement.

  3. #3
    Kum4r is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Help with arrays

    So something like this?
    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--);}
                }
    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)

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default 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;

  5. #5
    Kum4r is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  2. how to ask how many arrays you want
    By jepoy in forum New To Java
    Replies: 5
    Last Post: 09-06-2010, 04:19 PM
  3. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. Arrays
    By TheRocket in forum New To Java
    Replies: 6
    Last Post: 12-10-2008, 07:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •