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.


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
    Thanked 0 Times in 0 Posts

    Default IF statement/Array

    /* *****************************************
     ******************************************** */
    import java.util.Arrays;
    import javax.swing.*; // import the swing library for I/O
    class painting
    	public static void main (String[] args)
    	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?");
    			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]));
    				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
    Eastern Florida
    Thanked 2,273 Times in 2,244 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:
    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, 04: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, 12: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