Results 1 to 5 of 5
Thread: java gui if statment help
- 02-27-2013, 07:38 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 10
- Rep Power
- 0
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
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"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"); }
- 02-27-2013, 07:55 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
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.Website: Learn Java by Examples
- 02-27-2013, 09:46 AM #3
Godlike
- Join Date
- Nov 2012
- Posts
- 233
- Rep Power
- 1
Re: java gui if statment help
You should capture the input from JOptionPane in a String variable:
See: JOptionPane (Java Platform SE 7 )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 { ... } }
- 02-28-2013, 03:17 AM #4
Member
- Join Date
- Feb 2013
- Posts
- 10
- Rep Power
- 0
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
- 02-28-2013, 06:12 AM #5
Member
- Join Date
- Feb 2013
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Need some help on my if statment.
By ThretZ in forum New To JavaReplies: 1Last Post: 11-06-2012, 04:19 PM -
if statment not working good
By nour in forum New To JavaReplies: 3Last Post: 03-25-2012, 09:29 PM -
If statment not working:(
By Harris68 in forum NetworkingReplies: 6Last Post: 02-24-2010, 10:41 PM -
statment replay!!!
By moamen in forum New To JavaReplies: 4Last Post: 09-25-2009, 12:49 PM -
Issue using FOR statment with NetBeans
By Deathmonger in forum Advanced JavaReplies: 3Last Post: 07-30-2008, 12:46 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks