Text input


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Text input

  1. #1
    kleric Guest

    Text input


    I'm at a loss here.
    I'm tying to read text from standard input and then play with the string
    in an if statement, but for some reason, the if statement won't take it.
    Here's the snippet of code:

    /*********************/
    public static void main(String[] args) throws IOException {
    BufferedReader stdin = new BufferedReader
    (new InputStreamReader(System.in));


    while(true){

    String selection = stdin.readLine();

    if( selection == "a") {
    System.out.println("blah");
    }

    }
    }
    /***********************/

    "Blah" is never printed out even if "selection" really does equal "a".
    It DOES work though, if I set "selection" to "a" before the if statement.
    The if statement acts as if "selection" is null, but when I change the condition
    to, "if( selection != null)", then it is accepted and prints out "blah" like
    it's supposed to.
    It also works if I use numeric values rather than strings.
    Anyone know what the problem is?


  2. #2
    Paul Clapham Guest

    Re: Text input

    This is a common error. To compare Strings for equality, use the equals()
    method, like so:

    if (selection.equals("a"))

    When you use "==", you are comparing to see if the two sides refer to the
    same object. In your case, they refer to different String objects that
    contain the same data. This is a general rule in Java for all objects.
    (And that's why it works with numeric values, because they aren't objects.)

    PC2

    kleric <kleric_@hotmail.com> wrote in message
    news:39ea83d0$1@news.devx.com...
    >
    > I'm at a loss here.
    > I'm tying to read text from standard input and then play with the string
    > in an if statement, but for some reason, the if statement won't take it.
    > Here's the snippet of code:
    >
    > /*********************/
    > public static void main(String[] args) throws IOException {
    > BufferedReader stdin = new BufferedReader
    > (new InputStreamReader(System.in));
    >
    >
    > while(true){
    >
    > String selection = stdin.readLine();
    >
    > if( selection == "a") {
    > System.out.println("blah");
    > }
    >
    > }
    > }
    > /***********************/
    >
    > "Blah" is never printed out even if "selection" really does equal "a".
    > It DOES work though, if I set "selection" to "a" before the if statement.
    > The if statement acts as if "selection" is null, but when I change the

    condition
    > to, "if( selection != null)", then it is accepted and prints out "blah"

    like
    > it's supposed to.
    > It also works if I use numeric values rather than strings.
    > Anyone know what the problem is?
    >




  3. #3
    Rita Williams Guest

    Re: Text input



    ...
    > while(true){
    >
    > String selection = stdin.readLine();
    >
    > if( selection == "a") {
    >
    >...


    Try
    if( selection.equalsIgnoreCase("a'))

    This should work even if user keyed in "A".

    Rita

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center