croucherj3
Programmer
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
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