Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot get if statment to work correctly

Status
Not open for further replies.

croucherj3

Programmer
Sep 13, 2003
5
US
Hi, I am a COBOL programmer trying to learn Java. I just wrote a quick program, but when I execute it, it doesn't give the expected results. The code:

class PasswordB
{
public static void main(String[] args) throws Exception
{
char p1, p2, p3, p4;
System.out.print("Enter a four letter password: ");

p1 = (char)System.in.read();
p2 = (char)System.in.read();
p3 = (char)System.in.read();
p4 = (char)System.in.read();

System.out.println("You entered: " + p1 + p2 + p3 + p4);

if(p1 == 'B')
System.out.println(p1 + p2 + p3 + p4 + " is a valid password");
else
System.out.println(p1 + p2 + p3 + p4 + " isnt a valid password");
}
}


Sample execution:
Enter a four letter password: Bffd
You entered: Bffd
370 is a valid password


When I execute it, the println statments following the if statment print numbers instead of letters.
It seems as though the (p1 == 'B') is trying to assign B rather than test it.

Any comments would be appericiated

Thanks,
John
Athens, OH
 
Be careful when you use the + sign in your System.out.println. For instance, if you do this:-

int x = 2;
int y = 3;

System.out.println(x + y); //will print out 5

but

System.out.println(&quot;Result is <&quot; + x + y); //will print out
//Result is 23

but

System.out.println(&quot;Result is <&quot; + (x + y)); //will print
//out Result is 5



I would suggest use the parentheses as well. Copy the below code and name it TestPassword.java
and see what I mean.

public class TestPassword
{
public static void main(String[] args) throws Exception{
char p1, p2, p3, p4;
System.out.print(&quot;Enter a four letter password: &quot;);

p1 = (char)System.in.read();
p2 = (char)System.in.read();
p3 = (char)System.in.read();
p4 = (char)System.in.read();

System.out.println(&quot;You entered: &quot; + p1 + p2 + p3 + p4);

if(p1 == 'B'){
//talk like Yoda to get the right result.
System.out.println(&quot;a valid password&quot; + p1 + p2 + p3 + p4 + &quot; it is&quot; );
}
else {
System.out.println(p1 + p2 + p3 + p4 + &quot;isnt a valid password&quot;);
}
}
}


~za~
You can't bring back a dead thread!
 
I have answered the same question in a previous post, but I can't find it anymore. The output of &quot;System.out.println()&quot; does not seem consistent. I don't know whether you could consider this a bug in java. Sun knows about it for a long time (years), but they don't consider it a bug.
Code:
System.out.println(&quot;&quot; + p1 + p2 + p3 + p4  + &quot; is a valid password&quot; );
will also do the trick. See also Thread269-603639.
The output of
[code
System.out.println(p1 + p2 + p3 + p4 + &quot; is a valid password. A valid password &quot; + p1 + p2 + p3 + p4 + &quot; it is&quot; );
[/code]
is :
===============================
Enter a four letter password: Bffd
You entered: Bffd
370 is a valid password. A valid password Bffd it is
===============================
java works from left to right, so the result of 'p1 + p2' is a number, after + p3 and + p4 the result stays a number. It is only after addding &quot; is a ..&quot; that a converion to String takes place. You can play around and see what the result is of :
Code:
System.out.println(p1);  // B
System.out.println(p2);  // f
System.out.println(p3);  // f
System.out.println(p4);  // d
System.out.println(p1 +  p2);  //168
System.out.println(p1 + &quot;&quot; +  p2);  // Bf
 
Hey thanks for the help maxpower1 and hologram. Tried both of you suggestions and they worked. I would consider this to be a bug but maybe Sun calls it a feature?

Once again, thanks for the help.

John
Athens, OH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top