4 Replies - 231 Views - Last Post: 19 December 2013 - 09:56 PM Rate Topic: -----

#1 Spiked Penguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-December 13

Do while loop

Posted 19 December 2013 - 06:58 PM

Sorry for my blindness on this answer. I am currently trying to just go threw some things I feel like I remember from when I coded. I took a break for a while because of laziness. :(/> So I am requesting confirmation on the name and if I put "No" it terminates the program when it should go back to the do...?

package whileLoop;
import java.util.Scanner;

public class whileLoop {

	public static void main(String[] args){
		
		Scanner scanner = new Scanner(System.in);
		String input;
		String input_1;
		
		do{
			System.out.print("What is your name: ");
			input = scanner.nextLine();
			System.out.println("So your name is: " + input);
			input_1 = scanner.nextLine();
		}
		while(input_1 == "No");
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Do while loop

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7301
  • View blog
  • Posts: 12,158
  • Joined: 19-March 11

Re: Do while loop

Posted 19 December 2013 - 07:14 PM

Use .equals to compare Strings. == does identity comparison, so it's asking if two objects are references to the same instance. That's not going to be the case when you're comparing a literal from your code to a value input by the user.

So what you want is

while(input_1.equals("No"));


Was This Post Helpful? 1
  • +
  • -

#3 ChrisNt  Icon User is online

  • D.I.C Addict

Reputation: 198
  • View blog
  • Posts: 576
  • Joined: 31-July 13

Re: Do while loop

Posted 19 December 2013 - 07:15 PM

First of all to compare Strings you have to use method equals..

ex.
if( name.equals("John") ) {
           
}


if you terminate the program it will never go back again..im not sure what you are asking..
Was This Post Helpful? 0
  • +
  • -

#4 Spiked Penguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 19-December 13

Re: Do while loop

Posted 19 December 2013 - 09:11 PM

View Postjon.kiparsky, on 19 December 2013 - 07:14 PM, said:

Use .equals to compare Strings. == does identity comparison, so it's asking if two objects are references to the same instance. That's not going to be the case when you're comparing a literal from your code to a value input by the user.

So what you want is

while(input_1.equals("No"));



Thank you for the assistance; this helped much. Also, thank you for explaining the difference.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7301
  • View blog
  • Posts: 12,158
  • Joined: 19-March 11

Re: Do while loop

Posted 19 December 2013 - 09:56 PM

Glad I could help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1