Jump to content

not reading string? [help]

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
for some reason the string is not being read correctly, basically it is supposed to display like "Correct" if the users entered string == to check1, check2 etc. but it keeps saying it is wrong even when it is right.. any help?

 public static void stringManipulation ()
    {
        c = new Console ();
        String answer1;
        String check1 = "The eyes";
        String answer2;
        String check2 = "debit card";
        String answer3;
        String check3 = "silent";
        String answer4;
        String check4 = "conversation";
        String answer5;
        String check5 = "garbage man";
        int ptz = 0;
        c.println ("\t\t\tWelcome to the anagram solver!\n\n\n");
        c.println ("\t\tYou will be giving famillar brands, names, phrases etc.");
        c.println ("\t\tyour job is to unscramble them with there matching phrase,");
        c.println ("\tas fast as possible! You will be giving a score at the end indicating if");
        c.println ("\t\t\tyou have failed or passed this!");
        c.println ("--------------------------------------------------------------------------------");
        c.println ();
        c.println ("Your first clue is: (They see)");
        answer1 = c.readString ();
        if (answer1 == check1)
        {
            ptz++;
            c.println ("Correct + 1 pt!");
            c.println ();
            c.println ();
        }
        else
        {
            c.println ("Wrong . .");
            c.println ();
            c.println ();
        }
        c.println ("Your second clue is: (Bad credit)");
        answer2 = c.readString ();
        if (answer2 == check2)
        {
            ptz++;
            c.println ("Correct + 1 pt!");
            c.println ();
            c.println ();
        }
        else
        {
            c.println ("Wrong . .");
            c.println ();
            c.println ();
        }
        c.println ("Your third clue is: (Listen)");
        answer3 = c.readString ();
        if (answer3 == check3)
        {
            ptz++;
            c.println ("Correct + 1 pt!");
            c.println ();
            c.println ();
        }
        else
        {
            c.println ("Wrong . .");
            c.println ();
            c.println ();
        }
        c.println ("Your fourth clue is: (Voices Rant On)");
        answer4 = c.readString ();
        if (answer4 == check4)
        {
            ptz++;
            c.println ("Correct + 1 pt!");
            c.println ();
            c.println ();
        }
        else
        {
            c.println ("Wrong . .");
            c.println ();
            c.println ();
        }

        c.println ("Your fifth clue is: (Bag Manager)");
        answer5 = c.readString ();
        if (answer5 == check5)
        {
            ptz++;
            c.println ("Correct + 1 pt!");
            c.println ();
            c.println ();
        }


        else
        {
            c.println ("Wrong . .");
            c.println ();
            c.println ();
        }

        c.println ("Your total number of points is: " + ptz);
        if (ptz <= 3)
        {
            c.println ("Sorry you failed..");
        }


        else
        {
            c.println ("You passed, congratulations!");
        }
    }


#2
mariob316

mariob316

    Newbie

  • Members
  • Pip
  • 8 posts
"if the users entered string == to check1, check2 etc."

== should be .equals()

ex:
if (answer2.equals(check2))

        {

            ptz++;

            c.println ("Correct + 1 pt!");

            c.println ();

            c.println ();

        }



#3
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
The reason behind this is as follows. Using == with objects compares the objects references, NOT the objects value.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users