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

Thread: IF statement/Array

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

    Default IF statement/Array

    /* *****************************************
    AUTHOR 
     
     ******************************************** */
     
    import java.util.Arrays;
    import javax.swing.*; // import the swing library for I/O
     
    class painting
    {
     
     
     
     
    	public static void main (String[] args)
    	{
     
    		vote();
    		System.exit(0);
     
    	} 
     
     
    	public static void vote()
    	{
    		String[] paintings = {"Mona Lisa","Water Lilies","The Scream","A Young Rembrandt"}; 
    		int[] scores = {0,0,0,0}; 
    		String painting;
     
    		for (int y=0; y<paintings.length; y++)
    		{
    			System.out.println("Vote "+ y + " for " + paintings[y]);
    		}
     
     
    		for (int i=0; i<paintings.length; i++)
     
    		{
     
    			painting = JOptionPane.showInputDialog("Which painting would you like to vote for?");
    			System.out.println(Arrays.toString(paintings));
     
    			if (paintings[i].equalsIgnoreCase(painting))
    			{
    				JOptionPane.showMessageDialog(null, "You voted for " + painting);
    				scores[i] = scores[i] + 1;
    			}	
    			else if(painting.equals("-33"))
    			{
    				for(int z=0; z<paintings.length; z++)
    					System.out.println((scores[z] + " " + paintings[z]));
    			}
    			else 
    			{
    				JOptionPane.showMessageDialog(null, "Invalid Entry");
     
    			}
     
    		}
     
    	}	
     
    }

    Why does my program skip the IF statement and just goes to the else part and prints "Invalid Entry" even when i enter a correct painting :S

  2. #2
    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: IF statement/Array

    goes to the else part
    That means the condition in the if() was false.
    Print out the values of the variables used in the if condition to see why the if is never true.
    Be sure to add delimiters any Strings printed so you know if there are leading or ending spaces:
    println("theString="+theString+"<")
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to assign an array cell value into a switch statement
    By PC_flea in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 16th, 2013, 07:42 AM
  2. How to assign an array cell value into a switch statement
    By PC_flea in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2013, 06:25 AM
  3. Changing strings in a switch statement n stroing it in an array
    By ajw1993 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: February 20th, 2013, 05:25 PM
  4. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 01:28 PM
  5. Use variable in array declaration statement
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2010, 03:42 PM