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

Thread: Solution Error: Y U No Work??

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Solution Error: Y U No Work??

    I've been recently working on this program, it's supposed to be able to confirm whether or not the guess you input is the same as any of the three answers. The issue is that no matter what I put, it always tells me the guess was wrong. I've checked the output in various places for consistency in the variables, but they all look the way they should. If you could tell me what went wrong, and how to fix it, that'd be much appreciated. Thanks!

    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.io.IOException;
    import java.io.File;
    import static java.lang.System.out;

    public class TestDialogBox {
    public static void main(String args[])
    throws IOException{
    Scanner riddleBook = new Scanner(new File("RiddleBook.txt")); //Initialize scanner for riddles & answers

    String guess = null;
    String answer1 = riddleBook.nextLine();
    String answer2 = riddleBook.nextLine();
    String answer3 = riddleBook.nextLine();

    guess = JOptionPane.showInputDialog(riddleBook.nextLine()) ;

    if (guess == answer1 || guess == answer2 || guess == answer3) {
    JOptionPane.showMessageDialog(null, "Awesome!");
    } else {
    JOptionPane.showMessageDialog(null, "Bummer. The correct answer could have been: " + answer1 + ", " + answer2 + ", or " + answer3);
    JOptionPane.showMessageDialog(null, "Your guess was: " + guess);
    //JOptionPane results are for demonstrating the answers and your guess didn't change
    }
    }
    }




    The text file that it scans is as follows:
    man
    humans
    people
    What goes on four legs in the morning, two legs in the afternoon, and three legs in the evening?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    618
    My Mood
    Sleepy
    Thanks
    10
    Thanked 94 Times in 87 Posts

    Default Re: Solution Error: Y U No Work??

    Never ever compare Stings with == !!! Use String.equals()

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Solution Error: Y U No Work??

    *facepalm

    Thank you, the code actually works now XD I'll try and remember this from now on

Similar Threads

  1. [SOLVED] Simple Error ( But Hard Solution)
    By tommyacton in forum What's Wrong With My Code?
    Replies: 13
    Last Post: August 5th, 2013, 02:38 PM
  2. [SOLVED] Can't find solution for error in my code..
    By adi2609 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 26th, 2013, 12:10 PM
  3. Setbackground doesn't work for me Solution for it?
    By Botscape in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 29th, 2012, 04:59 PM
  4. Solution for error message: Bad operand for binary operator '-'
    By MostinCredible21 in forum AWT / Java Swing
    Replies: 8
    Last Post: September 7th, 2011, 05:28 PM
  5. [SOLVED] Compiled .jar won't work (Error-sound)
    By Fermen in forum Object Oriented Programming
    Replies: 2
    Last Post: March 31st, 2011, 06:24 PM

Tags for this Thread