• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unexpected outcome please help!!! j2sdk1.4

 
Jama, Moryan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jama, Moryan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic