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 4 of 4

Thread: Why does my IF statement not work?

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

    Default Why does my IF statement not work?

     
    /* *****************************************
    AUTHOR 
     
    ******************************************** */
     
    import javax.swing.*; // import the swing library for I/O
    import java.util.*;
     
    public class painting
    {
     
     
     
        public static void main (String[] param)
        {
     
            Gallery();
            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 Gallery()
        {
     
           // 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=0; y<paintings.length; y++)
    	{
       		System.out.println("Vote "+ (y+1) + " for " + paintings[y]);
       	}
     
     
          	for (int i=0; i<paintings.length; i++)
     		{
     
    	  	String vote = JOptionPane.showInputDialog("Which painting would you like to vote for?");
     
    		if (paintings[i].equalsIgnoreCase(vote))
            	{
     
                  		JOptionPane.showMessageDialog(null, "You voted for " + vote); 
    			scores[i]++;
    			for(int z=0; z<paintings.length; z++)
    			System.out.println(scores[z] + " " + paintings[z]);
     
    		}
    		else if(total.equals(vote))
    		{
    			JOptionPane.showMessageDialog(null, "You have chosen to exit this program, will now close"); 
    			System.exit(0);
     
    		}
    	   	else 
    		{
    			JOptionPane.showMessageDialog(null, "Invalid entry");
    		}
     
           }
     
     
    	}	
     
    }


    My code works perfectly for the first entry but if i enter the same painting twice it seems to skip this part of the code
    		if (paintings[i].equalsIgnoreCase(vote))
            	{
     
                  		JOptionPane.showMessageDialog(null, "You voted for " + vote); 
    			scores[i]++;
    			for(int z=0; z<paintings.length; z++)
    			System.out.println(scores[z] + " " + paintings[z]);
     
    		}
    and just says "invalid entry".

    Any ideas?

    Thanks


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Why does my IF statement not work?

    It seems that you're going through your array of paintings. So the painting in question would only be able to be equal to vote once. Since the painting wouldn't be the one that is there in the iteration and it wouldn't be "-33", then it would read it as invalid.

    What you should probably do is move that for loop that iterates through the list of paintings to after you read in vote. Then, use the for loop to see if the value of vote is one of the values of painting or "-33". (I'm not sure fully what you're trying to do. However, I would think that, perhaps, you could use a while loop to make it keep going, assuming that's what you want, until the user causes it to exit.)

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

    Default Re: Why does my IF statement not work?

    Yeah I want the loop to keep going till the user exits. Will loop the paintings and see how i get on thanks.

    --- Update ---

    I've tried that and I'm still have trouble :/ Im not sure where to add the other loop/move the loop you suggested

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why does my IF statement not work?

    One way to see what the code is doing is to add println statements just before the if statement, that print out the values of: vote, paintings[i], i and total so you can see what the computer sees when the code is executed.
    Be sure to add ids and delimiters to the print out so you know what you are seeing and know the beginning and end of each String:
    System.out.println("an ID "+ theName +"<");
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. the if statement does not work
    By ericgomez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 22nd, 2013, 07:35 PM
  2. Trying to get an "if" statement to work in a "for loop".
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 1st, 2013, 11:16 PM
  3. Default does not work in Switch Statement
    By StrugglerWithJava in forum Loops & Control Statements
    Replies: 3
    Last Post: March 29th, 2013, 07:15 AM
  4. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  5. Can't get the Update statement to work
    By reptar693 in forum JDBC & Databases
    Replies: 1
    Last Post: March 16th, 2012, 02:03 AM