Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Adding two arrays

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding two arrays

    Hi I have this code that i've written, I was wondering how I would add 1 to my int[] scores array when a user inputs one of the paintings.
    For example: If the user enters "Mona Lisa" it would add 1 to the int scores and it would becomes int[] scores = {1,0,0,0}; and if i did this for "the scream" it would then be int[] scores = {1,0,1,0};

    Also if instead of making the user type "Mona Lisa" they could just enter 1, and for the next painting they'd just enter 2...etc

    THANKS IN ADVANCE!
    /* *****************************************
    AUTHOR 
     
    ******************************************** */
     
    import javax.swing.*; // import the swing library for I/O
     
    public class painting
    {
     
     
     
        public static void main (String[] param)
        {
     
            vote();
            System.exit(0);
     
        } // END main
     
     
       /* ***************************************************
           Define some commands of our own to use above
       *************************************************** */
     
     
        /* ***************************************************
           Set up an array containing animals then find one asked for by the user
        */
     
        public static void vote()
        {
     
           // Declare variables
           //
    	String total = "-33";
           	String[] paintings = {"Mona Lisa","Water Lilies","The Scream","A Young Rembrandt"};
    	int[] scores = {0,0,0,0};
           	String searchKey; //the thing looked for
      // 
     
     
           //now can get an answer quickly without calculating just looking it up
    	for (int y=1; y<paintings.length; y++)
    	{
       		System.out.println("Vote "+ y + " for " + paintings[y]);
       	}
     
     
           for (int i=0; i<paintings.length; i++)
     
          		{
    	  		String painting = JOptionPane.showInputDialog("Which painting would you like to vote for?");
               	if (paintings[i].equals(painting))
            	{
                  		JOptionPane.showMessageDialog(null, "You voted for " + painting); 
     
    		}
     
    		if(total.equals(painting))
    		{
    			for(int z=0; z<paintings.length; z++)
    			System.out.println(scores[z] + paintings[z]);
     
    		}
    	   	else 
    		{
    			JOptionPane.showMessageDialog(null, "Invalid entry");
    		}
     
           }
     
     
    	}	
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,765
    My Mood
    Mellow
    Thanks
    234
    Thanked 714 Times in 700 Posts

    Default Re: Adding two arrays

    You shouldn't be using parallel arrays to keep track of data. Create a class, Paintings, or whatever you want to call it, and include instance variables, "name", and "votes", with the appropriate getter (accessor) and setter (mutator) methods to increase and report votes as needed.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding two arrays

    Ok will do this, another problem with my code is that after the first loop it keeps going to the else statement rather than doing the first if statement when i enter a valid painting :s any ideas?
    Thanks.

Similar Threads

  1. Regarding service adding
    By sabarimanoj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 07:06 AM
  2. Large number while adding arrays
    By LoganC in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 6th, 2012, 08:27 PM
  3. Adding if statements.
    By LoganC in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 26th, 2012, 02:11 PM
  4. [Help] Problem with Class arrays, adding/editing and deleting
    By Grant_Searle in forum Object Oriented Programming
    Replies: 7
    Last Post: December 16th, 2011, 09:10 AM
  5. Adding two matrix together
    By papated21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 2nd, 2011, 12:52 PM