File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why This Error??

 
Manuel Diaz
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, sorry for my last post. I know now what do I have to do. Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic