If(variable=="string") not true, yet returning "string".


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: If(variable=="string") not true, yet returning "string".

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Texas.
    Posts
    8

    If(variable=="string") not true, yet returning "string".

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    
    /**
     * Write a description of class Game here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Game
    {
        private String sideChoice;
        private String nameChoice;
        private String nameOfVillan;
        private String nameOfHero;
        
        public Game()
        {
        }
        public void run()
        {
            System.out.println("Do you want to use your own names? (y/n)");
            nameChoice = (String) JOptionPane.showInputDialog("Enter y or n");
            if(nameChoice == "y")
            {
                System.out.println("Please put the name of the hero. \n");
                nameOfHero = (String) JOptionPane.showInputDialog("Enter the name of the hero");
                System.out.println("Please put the name of the villan. \n");;
                nameOfVillan = (String) JOptionPane.showInputDialog("Input the name of the villan");
            }
            else if(nameChoice=="n")
            {
                nameOfHero = "Jeff";
                nameOfVillan = "Markum";
            }
            else
            {
                System.out.println("Please choose either y or n.");
                System.out.println("Stupid idiot java not getting the value of " + nameChoice + " nameChoice.");
            }
    This returns:
    Please choose either y or n.
    Stupid idiot java not getting the value of n nameChoice.
    Could you please tell me what I did wrong? It seems to be getting the value of nameChoice (it gave it to me in the println), but niether the if or the else if are executing, even when the variable is supposed to prove it true.

  2. #2
    Join Date
    Jan 2005
    Posts
    61
    Well, I don't want to get slated here for giving bad advice, but if you are comparing strings, you should use the equals() method instead of ==, even if == seems to be working.

    There may be other problems there but I'll keep quiet and let someone else comment on that as my ego is quite fragile at the moment

  3. #3
    Join Date
    Aug 2005
    Posts
    43
    I usually prefer using compareTo instad of equals....the difference is that compareTo compares both strings lexographically while equals compares the objects themselves...
    Code:
    String str = "string";
    
    if(str.compareTo("string")==0)
       return true;
    else
       return false;

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    to will808: I'm sorry for that...

    Well, I don't want to get slated here for giving bad advice
    I assume that one was for me, I was in a hurry and didn't pay much attention
    to the form, and I sincerely hope it won't keep you from contributing here.
    I could certainly have been more polite.
    -----
    As for the string compare stuff, I've checked, and if two String variables in
    closely knitted code are assigned the same string value then java will
    sometimes optimize memory usage and in fact assign the same reference
    to both, like:
    String a="OK";
    String b="OK";
    if (a==b) {
    // this will (sometimes) be true .....
    }
    ..but of course code that relies on this is a recipe for disaster...
    eschew obfuscation

  5. #5
    Join Date
    Jan 2005
    Posts
    61
    Quote Originally Posted by sjalle
    I assume that one was for me, I was in a hurry and didn't pay much attention
    to the form, and I sincerely hope it won't keep you from contributing here.
    I could certainly have been more polite.
    Not a problem - as you can tell I'm still new to all this, so sometimes I know what is right but not necessarily why it's right, and might not explain myself very well.

    Won't stop me posting here though - I need the help

  6. #6
    Join Date
    Sep 2005
    Location
    Texas.
    Posts
    8
    Quote Originally Posted by chimps
    I usually prefer using compareTo instad of equals....the difference is that compareTo compares both strings lexographically while equals compares the objects themselves...
    Code:
    String str = "string";
    
    if(str.compareTo("string")==0)
       return true;
    else
       return false;
    This works much better, thanks.

  7. #7
    Join Date
    Aug 2005
    Posts
    43
    no prob.

Similar Threads

  1. Replies: 3
    Last Post: 07-11-2007, 10:23 AM
  2. Rumors for beta 2
    By Patrick Troughton in forum .NET
    Replies: 361
    Last Post: 10-01-2003, 12:00 AM
  3. Access database with VB GUI
    By Emma in forum VB Classic
    Replies: 1
    Last Post: 06-30-2003, 07:47 AM
  4. Why integer and int32
    By Michael Culley in forum .NET
    Replies: 154
    Last Post: 05-10-2002, 12:06 PM
  5. Re: applet- help- the code
    By shafi in forum Java
    Replies: 1
    Last Post: 07-26-2001, 11:32 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center