Results 1 to 4 of 4
  1. #1
    Kblavkalash is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default While loop does not function properly

    Hi guys. I have a simple problem with my While loop but I was looking into it long time and I don't understand why loop goes over again all the time and never ends. As you see cont = "t", but later I enter "n" with keyboard, but loop still goes on. I even made this line:
    Java Code:
    System.out.println(cont);
    just to make sure how does cont looks like just before loop ends and it says that cont is "n", but the while loop never ends. Inputt is a method I made to enter strings but it works well.


    Java Code:
     
              String cont = "t";
    
              while (cont != "n"){
    	  System.out.println("Enter new part name:");
    	  partName2 = inputt(); 		  
    	  System.out.println("Do you want to enter aditional parts? y/n");
    	  [B]System.out.println(cont);[/B] // cont is "t" here
    	  cont = inputt();	 
    	  [B]System.out.println(cont);[/B] // cont is "n" here if I enter "n".
    	  }

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    == does not work as expected on Strings; they check if they are the same String object, not if they represent the same text. Use "s1.equals(s2)" to see if two strings match instead.

  3. #3
    Kblavkalash is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Toll View Post
    == does not work as expected on Strings; they check if they are the same String object, not if they represent the same text. Use "s1.equals(s2)" to see if two strings match instead.
    Yeah now it works :)

    Thanks for fast answer!

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Remember to mark as solved! You can do that in Thread Tools at the top. It'll redirect you to a missing page, but it'll work anyway.

Similar Threads

  1. How do you properly use break?
    By louist in forum New To Java
    Replies: 3
    Last Post: 03-08-2011, 02:58 AM
  2. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 09:00 PM
  3. Why java cant do sum properly
    By thetimwu in forum New To Java
    Replies: 3
    Last Post: 05-26-2010, 07:40 AM
  4. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  5. Replies: 3
    Last Post: 06-05-2008, 11:10 PM

Posting Permissions

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