-
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.
-
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
-
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;
-
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
-
 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
-
 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.
-
Similar Threads
-
By Kerry in forum ASP.NET
Replies: 3
Last Post: 07-11-2007, 10:23 AM
-
By Patrick Troughton in forum .NET
Replies: 361
Last Post: 10-01-2003, 12:00 AM
-
By Emma in forum VB Classic
Replies: 1
Last Post: 06-30-2003, 07:47 AM
-
By Michael Culley in forum .NET
Replies: 154
Last Post: 05-10-2002, 12:06 PM
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|