This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why This Error?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why This Error??" Watch "Why This Error??" New topic
Author

Why This Error??

Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Here is the code:

---------------------------------------------------------------------------
if (value != "a" || value != "b" || value != "c" || value != "d" || value != "e" || value != "f" || value != "g")
{
c.println ("Error in number, please try again");
}

if (value == "a")
row = 0;

if (value == "b")
row = 1;

c.println (row);
}
-------------------------------------------------------------------------

I wonder, why I don't have what I want. I will explain it here. The user input something, if that is different from "a" or "b" to"h" then, the program output "Error in number, please try again". But if the user type "a" the variable "value" has to be 0, if the user types "b" the "value" has to be 1, now, why I dont have this. Is there anything wrong??.

THE VALUES INPUTED ARE LETTERS NOT NUMBERS!.


Note: I love programming.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
The equality operators (==,!=)in Java check if two objects represent the same reference.



Instead you must use the equals method inherited from Object and overriden by String.

[code]
System.out.println(a.equals(b)); //will print true
System.out.println(a.equals(c)); //will print true
[code]

In case of non-case sensitive operations use equalsIgnoreCase of the String class
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
OK, but that does not answer my problem, I dont want to compare the strings.
I just want to check the following:

If value = a then row = 0;
If value = b then row = 1;
If value = c then row = 2;

This is Pascal code, I dont know how to implement it in Java. Please help AGAIN.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
How come you do not want to compare the String, you are doing it again.

Let's try this firs.



You could also simplify:



P.D.

I do not have a Java compile right now, so please forgive me if the code does not compile the first time.
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Wrong! again. Ok, lets do this step by step.

when you run the program, the output is:

Please enter a letter:


If the user types "a" I want the program convert that "a" into "0". Im doing that like this:

value = c.readLine();
if (value == "a")
row = 0


This part works perfectly, but when I type "b" I want the program convert that "b" into "1". So I have this:

if (value == "b")
row = 1

And there is the problem I think, I type "b" and the program returns "0". WHY??
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
I think you might be missunderstanding me. I am giving you tips. I can't even see you code to tell you what is wrong. You have not given me much.

However, no matter what you are doing as long as you continue to use the == operator to know if one string is equal no another, your code is incorrect, whether that is the error you are looking for or not.

If you want more help post your real piece of code and then I might tell what is exactly wrong. I am basing my explanations in supossitions of what you're doing.
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
OK, sorry for my last post. I know now what do I have to do. Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why This Error??
 
Similar Threads
Compile issue -- misplaced construct error
What's wrong with my code?
Troubles with loop
How can I do this?
plssssss Help me!!