English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

What does it mean?


import java.io.*;

class Block{

public static void main(String[] args) throws Exception{
BufferedReader userInput=new BufferedReader (new InputStreamReader(System.in));

String username;

boolean condition = true;

if (username=="lene") {
username=userInput.readLine();
System.out.println("Condition is true.");
} // end block one


else { // begin block 2
System.out.println("Condition is false.");
} // end block 2
}
}

2007-11-07 21:58:53 · 3 answers · asked by Junkie 1 in Computers & Internet Programming & Design

3 answers

It's initialized, but in
if (username=="lene") { , you should have the
username=userInput.readLine(); before the if(username=="lene"){ so you can read the String before a block of code is run.

2007-11-07 22:08:09 · answer #1 · answered by xtrafile7 3 · 2 0

Yep, what the first guy said. It's uninitialized...all you've done is create a reference to the space in memory. You must read the line into the user name variable before testing for equivalence. Also, I think it would be better to use a .equals or .compareTo method to test for equality. You don't want to run the risk of testing to see if the hex memory locations are equal. In other works...chances are that currently it will always be false.

2007-11-08 07:08:21 · answer #2 · answered by Fieyr 4 · 0 0

The test for the username is first.
Then you read the username.

So the test is on an uninitialised variable. It has no value.

2007-11-08 06:06:38 · answer #3 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers