Results 1 to 19 of 19
- 04-02-2012, 06:50 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
while loop repeat word until user types "stop", code problem
Hi, my assignment is as follows:
Write a program which repeatedly asks the user to enter a word, and displays the word back to the user. If the word that the user enters is “Stop”, however, the program exits after displaying the word.
It seems fairly simple - however im just learning about loops now. I have made a code for it, but it wont compile successfully - i get an error with line Word = input.next(); that "input cannot be resolved". Everything else seems fine. Can someone take a look and see if i'm missing something or find the reason it will not work? Thanks so much!
import java.util.Scanner;
import java.io.*;
public class WordLoop {
public static void main(String[] args) throws IOException {
{
Scanner in = new Scanner(System.in);
String Word;
System.out.println("Enter a word or type stop to quit the program.");
Word = input.next();
while (!Word.equals("stop"))
{
System.out.println("Word is: " + Word);
}
}
}
}
- 04-02-2012, 06:53 PM #2
Re: while loop repeat word until user types "stop", code problem
When do you declare and instantiate a variable named input?
For the record, when posting code, make sure you follow the standard naming conventions and use the code tags to make your code easier to read.How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-02-2012, 06:59 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: while loop repeat word until user types "stop", code problem
I'm very new to java - what are the standard naming conventions?
Oh, I suppose i didn't declare input. I was attempting to make it so that when the user enters a word, the program will return the word by displaying it... I'm just lost as to what to do to fix my program - i feel like i'm barely grasping the while loop.
Thanks for the response.
- 04-02-2012, 07:06 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: while loop repeat word until user types "stop", code problem
You declared 'in', but don't use it.
Should that be 'input'?
And (just to get you past an inevitable problem we see all the time from Scanner) I'd suggest (for Strings) using nextLine().Please do not ask for code as refusal often offends.
** This space for rent **
- 04-02-2012, 07:06 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: while loop repeat word until user types "stop", code problem
I just tried to declare input like you said, and now im getting an issue with Scanner in = new Scanner(System.in); that the local variable is never read. Did I go about this change all wrong? I'm trying but this is difficult for me. Thanks for the help. Here is what I changed:
Scanner in = new Scanner(System.in);
String Word;
System.out.println("Enter a word or type stop to quit the program.");
Scanner input = null;
Word = input.next();
while (!Word.equals("stop"))
- 04-02-2012, 07:10 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: while loop repeat word until user types "stop", code problem
Nevermind, you were right with your reply.
Now my code works. The only problem is, instead on continuously asking the user to input a word, it just keeps displaying it hundreds of times over and over again. I want to be able to keep asking the user to enter a word, that way if they want to exit the program they can enter "Stop".
Here is the changes i made:
import java.util.Scanner;
import java.io.*;
public class WordLoop {
public static void main(String[] args) throws IOException {
{
Scanner in = new Scanner(System.in);
String Word;
System.out.println("Enter a word or type stop to quit the program.");
Word = in.next();
while (!Word.equals("stop"))
{
System.out.println("Word is: " + Word);
}
}
}
}
- 04-02-2012, 07:15 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: while loop repeat word until user types "stop", code problem
You have your Scanner, you just called it 'in'.
You just needed to either change the name of the Scanner from 'in' to 'input', or change all uses of 'input' to 'in'.
No need for another Scanner.Please do not ask for code as refusal often offends.
** This space for rent **
- 04-02-2012, 07:20 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: while loop repeat word until user types "stop", code problem
Thanks, did you see my last reply? I think you were responding to my previous response. I changed it to "in" and it works correctly - thanks! The only issue now is that it displays the word the user inputs continously until you manually stop the program. As i am brand new to loops, I cant figure out how to alter the program so that it will continuously [I]ask[I] the user to input and words and then displays it, until the user enters stop.
- 04-02-2012, 07:28 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: while loop repeat word until user types "stop", code problem
Cross-posted.
I really must stop leaving these windows open so long.
OK, you now have it looping, but you need a way to get more input from the user inside the loop, in order to let them change the value of 'Word'.
So try and think what line of code you could put in there that would do that.
(Hint: you've already done it once).Please do not ask for code as refusal often offends.
** This space for rent **
- 04-02-2012, 08:04 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: while loop repeat word until user types "stop", code problem
Hmm , yes i see what you're saying. Would i possibly use this line "Word = in.next();" after System.out.println("Word is: " + Word); ?
- 04-02-2012, 08:09 PM #11
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: while loop repeat word until user types "stop", code problem
I tried adding that line from my last post, and this is what I got:
Enter a word or type stop to quit the program.
Hello
Word is: Hello
Now it doesnt seem to loop at all...
- 04-02-2012, 09:16 PM #12
Re: while loop repeat word until user types "stop", code problem
Post your updated code. Don't forget the code tags.
Try stepping through your program in your head, following each line. If that doesn't show you your logic error, then add some print statements to check your assumptions. If that still doesn't help you figure it out, step through your program with a debugger.How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-03-2012, 12:33 AM #13
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: while loop repeat word until user types "stop", code problem
What are code tags? And I got it! That worked! Thank so much that really was very helpful to me!
Here is my program (I guess without the code tags since I'm not sure what they are)
import java.util.Scanner;
import java.io.*;
public class WordLoop {
public static void main(String[] args) throws IOException {
{
Scanner in = new Scanner(System.in);
String Word;
System.out.println("Enter a word or type stop to quit the program.");
Word = in.next();
while (!Word.equals("stop"))
{
System.out.println("Word is: " + Word); \\ prints the word the user entered
System.out.println("Enter a word or type stop to quit the program.");
Word = in.next(); \\waits for user to input more information
}
}
}
}
- 04-03-2012, 02:43 AM #14
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
issue with my While loop
I'm trying to run a program that asks for the user to input a sentence, then it says whether the sentence has an even or odd number of words. It's supposed to keep looping until the user types "Stop Already!" as the sentence. I almost have it, except for some reason I cant understand, the program doesnt stop running when i type "Stop Already". It also displays the question more than once. Here is an example of what it displays and how it does not stop.
Enter a sentence or type 'Stop Already' to quit the program.
hello out there!
That's odd!
Enter a sentence or type 'Stop Already! to quit the program.
That's odd!
Enter a sentence or type 'Stop Already! to quit the program.
That's odd!
Enter a sentence or type 'Stop Already! to quit the program.
How are you?
That's odd!
Enter a sentence or type 'Stop Already! to quit the program.
That's odd!
Enter a sentence or type 'Stop Already! to quit the program.
That's odd!
Enter a sentence or type 'Stop Already! to quit the program.
Stop Already!
That's odd!
Enter a sentence or type 'Stop Already! to quit the program.
That's odd!
Enter a sentence or type 'Stop Already! to quit the program.
Oh, and also, it only displays "Thats odd" and never "Thats even" which i also cant figure out. Any help will be greatly appreciated! Thanks so much.
Here is my OddSentence:
public class OddSentence {
String input = "";
int num;
OddSentence(String line) {
input = line;
}
public boolean isOdd() {
num = input.split(" ").length;
if(num % 2 == 0){
return true;
}
else {
return false;
}
}
}
And here is my OddSentenceTester:
import java.io.IOException;
import java.util.Scanner;
public class OddSentenceTester
{
public static void main(String[] args) throws IOException {
boolean odd;
String words = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence or type 'Stop Already' to quit the program.");
words = in.next();
while (!words.equals("Stop Already!"))
{
OddSentence first = new OddSentence(words);
odd = first.isOdd();
if(odd = true)
System.out.println("That's odd!");
else
System.out.println("That's even!");
System.out.println("Enter a sentence or type 'Stop Already! to quit the program.");
words = in.next();
}
}
}
- 04-03-2012, 04:15 AM #15
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: issue with my While loop
Java Code:if(odd = true)
Java Code:if(odd) { }
-----
As for why it never stops. That can only be because the expression words.equals("Stop Already!") is never true. So what is words being set to? Find out with
Java Code:System.out.println("Enter a sentence or type 'Stop Already! to quit the program."); words = in.next(); System.out.println("words read as: " + words);
When posting code, please use the "code" tags so that the code is readable. There is a faq somewhere on the site describing how to use the tags.
- 04-03-2012, 07:40 AM #16
Re: while loop repeat word until user types "stop", code problem
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-03-2012, 07:44 AM #17
Re: while loop repeat word until user types "stop", code problem
Merging the double post from http://www.java-forums.org/new-java/...hile-loop.html
Please keep this discussion in one place. Multiple threads for the same question are neither welcomed nor tolerated.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-03-2012, 07:45 AM #18
Re: issue with my While loop
Double posted question: http://www.java-forums.org/new-java/...e-problem.html
Merging the two threads. Please don't do this again.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-03-2012, 10:49 AM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: while loop repeat word until user types "stop", code problem
See my first post regarding the use of nextLine() rather than next.
Using next() the Scanner returns everything up to a newline, but not the newline which is still in the buffer.
The next next() call eats that newline character.
Since you are working with Strings and you want the whole line then use nextLine() which will give you the whole line and not leave the newline character in the buffer.Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 09:37 PM -
Program skips "If" code and goes straight to "Else"
By Logik22 in forum New To JavaReplies: 12Last Post: 01-21-2012, 06:40 PM -
Setting up "Stop" and "Resume" with SourceDataLine
By davisburns in forum Advanced JavaReplies: 0Last Post: 03-08-2011, 04:05 PM -
An "if" statement inside a "for" loop?
By soccermiles in forum New To JavaReplies: 18Last Post: 04-20-2010, 04:44 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 08:35 AM
Bookmarks