Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By SurfMan

Thread: java gui if statment help

  1. #1
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default java gui if statment help

    I'm finishing up my code to my java program and its basically going to read questions from the array, ask user for the answer(a, b, c, or d). Then I'm going to make it calculate number wrong and right. I'm stuck on the part where my if statement only displays my else case "wrong". Here's the code thanks
    Java Code:
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    
    /**
    	This program is a guiquiz
    */
    
    public class GuiQuiz
    {
    	public static void main(String[] args)
    	{
    
    
    		String[] questions = { "What is science", "How many piers", "What is economics",
    			"What year is columbus" };
    
    		JOptionPane.showInputDialog(null, questions[0] + "\nA" + " metal," + "\nB. An art, \nC, Blue. \nD.Rockets");
    
    			if(questions[0].equalsIgnoreCase("a"))
    			{
    				System.out.println("Correct");
    
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null, "WRONG");
    
    			}
    PS I am going to make another array with the correct answers so it can be easier calculating but, I want the first answer to be a but no matter what I put even a I get "WRONG"

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: java gui if statment help

    Hello,

    In your program you are comparing the content of questions array at index 0 to see if it is equals to "a". The questions[0] has a string of "What is science". This string will never equals to the string "a", that's why it always execute the else part of the if statement.

    To get the real answer from the user input you have to read the return value returned by the JOptionPane.showInputDialog() method.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: java gui if statment help

    You should capture the input from JOptionPane in a String variable:
    Java Code:
    String answer = JOptionPane.showInputDialog(...);
    //answer will be null if the user cancelled the dialog
    if ( answer != null) { 
       if ( answer.equalsIgnoreCase( answers[0]) ) {
           //correct answer found in the answers array
       }
       else {
          ...
       }
    }
    See: JOptionPane (Java Platform SE 7 )
    jumpman8947 likes this.

  4. #4
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default Re: java gui if statment help

    Thanks for the help, I will try to put that into my code. Hopefully I can get this to work

  5. #5
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default Re: java gui if statment help

    Thanks a million it works just how I wanted it to.

Similar Threads

  1. Need some help on my if statment.
    By ThretZ in forum New To Java
    Replies: 1
    Last Post: 11-06-2012, 04:19 PM
  2. if statment not working good
    By nour in forum New To Java
    Replies: 3
    Last Post: 03-25-2012, 09:29 PM
  3. If statment not working:(
    By Harris68 in forum Networking
    Replies: 6
    Last Post: 02-24-2010, 10:41 PM
  4. statment replay!!!
    By moamen in forum New To Java
    Replies: 4
    Last Post: 09-25-2009, 12:49 PM
  5. Issue using FOR statment with NetBeans
    By Deathmonger in forum Advanced Java
    Replies: 3
    Last Post: 07-30-2008, 12:46 AM

Posting Permissions

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