aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes unexpected outcome please help!!! j2sdk1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "unexpected outcome please help!!! j2sdk1.4" Watch "unexpected outcome please help!!! j2sdk1.4" New topic
Author

unexpected outcome please help!!! j2sdk1.4

Jama, Moryan
Greenhorn

Joined: Aug 20, 2002
Posts: 4
public class Password2 {
public static void main( String args[] ) throws Exception {
char one, two, three, four;
System.out.println("Please enter your 4 character password");
one = (char) System.in.read();
two = (char) System.in.read();
three = (char) System.in.read();
four = (char) System.in.read();
System.out.println(one+two+three+four);
if( one == 'B' ) {
System.out.println("The password is valid");
} else {
System.out.println("The password is not valid");
}
}
}
compile with Boss output is ( Boss 407 the password is valid ) I'm not understanding where the numbers are coming from. Can someone please explain...I'm using j2sdk1.4.0
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Alazaya,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
Is your question related to the SCJP exam?
[ August 20, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jama, Moryan
Greenhorn

Joined: Aug 20, 2002
Posts: 4
Sorry about the name and yes my question does pertain to the SCJP this was in a book I'm studying the SCJP from.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Jama,
I'm sorry to say this but your name is still not complying with our naming policy, which states:

"For your publicly displayed name, use a first name, a space, and a last name. Obviously fictitious names or improperly formatted names may be locked out."

Please, change it here. Thank you
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello, try this code:

public class Password2 {
public static void main( String args[] ) throws Exception {
char one, two, three, four;
System.out.println("Please enter your 4 character password");
one = (char)System.in.read();
two = (char) System.in.read();
three = (char) System.in.read();
four = (char) System.in.read();
System.out.println(""+one+""+two+""+three+""+four);
if( one == 'B' ) {
System.out.println("The password is valid");
} else {
System.out.println("The password is not valid");
}
}
}
Output:
Please enter your 4 character password:
BCDE
BCDE
The password is valid
I think the reason is that when you simply write
one+two ... Java thinks you want to add the integer numbers. Remember that there is an automatic casting from char to int but when you want char to int you have to cast explicitly like you did in the (char)System.in.read().
But this is just my idea. I didn't find anything about this phenomenon in Java Language Specification. Did anybody find it?
Ciao
Thomas
Jama, Moryan
Greenhorn

Joined: Aug 20, 2002
Posts: 4
Sorry about the name again.
Thanks Thomas I really appreciate the help that took care of the problem it was adding up the numbers...but I still would like to find out why it only gives 3 numbers rather than 4. Since there is 4 characters being printed on the screen. But thats off topic don't try and answer that one unless you have the time...Thanks in advance for your help Thomas.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I didn't find anything about this phenomenon in Java Language Specification. Did anybody find it?
This phenomenon is called binary numeric promotion and explanations can be found at JLS 15.18.2 Additive Operators (+ and -) for Numeric Types
Jama, Moryan
Greenhorn

Joined: Aug 20, 2002
Posts: 4
but I still would like to find out why it only gives 3 numbers rather than 4. Since there is 4 characters being printed on the screen. But thats off topic don't try and answer that one unless you have the time...Thanks in advance for your help Thomas.

Oops I'm sorry about that final question...I got it...never mind folks...Thanks for the link Valentin it helped.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unexpected outcome please help!!! j2sdk1.4