954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String Equality to Finish a While Loop

Im trying to figure out why this simple piece of code isnt working.
The aim is for the user to enter a word and then the application echos the word back to them.
If the word is equal to 'done' then the application prints 'Complete'.

import java.util.*;
public class word
{
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		String word;
		String termination = "done";
		
		System.out.println("Enter the list of words, terminated by the word'done'");
		word = sc.next();
		
		while(!word.equalsIgnoreCase(termination));
		{	
			System.out.println(word);
			word = sc.next();
		}
		System.out.println("Complete");
	}
}


For some reason it doesnt work
Thanks

BleepyE
Junior Poster in Training
87 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
it doesnt work


Please explain what the code does. It doesn't work covers lots of cases.

Show the console from when you execute the code and add comments to explain what is not working.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Fixed

import java.util.*;
public class word
{
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		String word;
		boolean isEqualTo;
		
		System.out.println("Enter the list of words, terminated by the word 'done'");
		word = sc.next();
		
		isEqualTo = word.equalsIgnoreCase("done");
				
		while(isEqualTo == false)
		{	
			System.out.println(word);
			word = sc.next();
			isEqualTo = word.equalsIgnoreCase("done");
		}
		
		System.out.println("Finished");
	}
}
BleepyE
Junior Poster in Training
87 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

You should explain what your problem was so the next student that reads this thread can learn from it.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

the code worked fine but:

while(!word.equalsIgnoreCase(termination));


he should just have removed the ';' there.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

I know, realised after debugging for ages. Thanks

BleepyE
Junior Poster in Training
87 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You