Results 1 to 5 of 5
  1. #1
    ziongio is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default I'm having problems with my simple prompting program.

    The if statement won't activate when I run it. Only the else statement seams to work. I think there might be something wrong with the string inside the if statement. Can someone guide me in the right direction?
    Here is my simple program:


    import java.io.* ;

    class name
    {
    public static void main(String args[]) throws IOException
    {
    InputStreamReader istream = new InputStreamReader(System.in) ;
    BufferedReader bufRead = new BufferedReader(istream) ;

    String name1;


    System.out.println("Hi, what's your name? : ");
    name1 = bufRead.readLine();

    if(name1 == "exit") {
    System.out.println("Good Bye!");
    }

    else
    System.out.println(name1 +",it's so nice to meet you!");



    }
    }

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    to compare strings, use equals(). I suggest using the Scanner class to get your input.

  3. #3
    JCollier is offline Member
    Join Date
    Aug 2008
    Location
    Boston
    Posts
    11
    Rep Power
    0

    Default

    There are two areas of memory used in Java, Stack Space and Heap Space. Local variables and their content are stored in the Stack, while the Heap Space is used for Objects and dynamic memory allocation. When you create a String (which is an object), a reference to the memory space where the string object is held in the Heap is created in the Stack. When you're comparing using == "exit", A new String object for the word "exit" is created in a different place in the Heap, and a new pointer is created in the Stack. In comparing two references that point to different areas of memory, == will evaluate to false. using something like name1.equals("exit") will evaluate the contents of the strings, rather than references to Heap Space. I hope that was helpful, and, as I'm new myself, I hope someone will jump in to correct me if I'm wrong.

    Jared

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    This is one interesting article you might find useful : String Literal Pool

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    ziongio is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Simple program help
    By jtyler in forum New To Java
    Replies: 3
    Last Post: 09-20-2010, 07:43 AM
  2. simple program
    By blastoff in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 11:25 PM
  3. Two simple array problems.
    By DMiller in forum New To Java
    Replies: 4
    Last Post: 11-06-2009, 11:50 AM
  4. Simple Program
    By TheRocket in forum Advanced Java
    Replies: 15
    Last Post: 12-30-2008, 02:35 PM
  5. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 06:23 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •