Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Simple while loop, i mean really simple and i cant do it :(

whileloop

Best Answer sepp2k, 06 October 2016 - 11:20 AM

while(choiceinput == "true")

Strings need to be compared using .equals, not ==. == only works when comparing two string literals or comparing against null (but .equals works in both those cases as well, so there's no reason not to use it). You can't use == when comparing user input.

scan.next() != null;

next never returns null, so this condition will never be true. Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 294 posts

Posted 05 October 2016 - 11:54 AM

Hello all,

 

simple while loop, and i know i am doing something soooo simply wrong, wondering what though? I havent programmed in so long, i am a DBA thinking about finally going to the dark side, the software developer side  B)

import java.util.Scanner;

public class Test003 
{

	@SuppressWarnings("resource")
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		
		String inputstuff;
		String choiceinput;
		
		System.out.println("Would you like to input?");
		choiceinput = scan.next();
		while(choiceinput == "true")
		{
			System.out.println("Enter Something");
			inputstuff = scan.next();
			if (inputstuff == "false")
				break;
			else
			System.out.println(inputstuff);
		}
	}

}


Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#2 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 294 posts

Posted 05 October 2016 - 12:50 PM

I think i am getting close, but still not acting right :(

// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		
		boolean choiceinput = true;
		
		while(choiceinput == true)
		{
			System.out.println("Would you like to input?");
			choiceinput = scan.next() != null;
			if (!choiceinput)
	            break;
			
			String inputstuff;
			System.out.println("Enter Something");
			inputstuff = scan.next();		
			System.out.println(inputstuff);
		}

Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#3 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts

Posted 06 October 2016 - 11:20 AM   Best Answer

while(choiceinput == "true")

Strings need to be compared using .equals, not ==. == only works when comparing two string literals or comparing against null (but .equals works in both those cases as well, so there's no reason not to use it). You can't use == when comparing user input.

scan.next() != null;

next never returns null, so this condition will never be true.

Edited by sepp2k, 08 October 2016 - 03:48 PM.


#4 ariannabates

ariannabates

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 06 October 2016 - 09:44 PM

for ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}