Results 1 to 19 of 19
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By KevinWorkman

Thread: while loop repeat word until user types "stop", code problem

  1. #1
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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);

    }
    }
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default 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 **

  5. #5
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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"))

  6. #6
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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);

    }
    }
    }
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default 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 **

  8. #8
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default 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).
    JMAsterson likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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); ?

  11. #11
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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...

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default 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.
    JMAsterson likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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


    }
    }
    }
    }

  14. #14
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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();
    }

    }
    }

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: issue with my While loop

    Java Code:
    if(odd = true)
    The expression "odd=true" assigns the value true to odd - in other words it makes odd true. This is almost certainly not what you wanted to do. Rather you meant to carry out some statements if odd really is true. The construction for that is:

    Java Code:
    if(odd) {
    
    }
    You can also say "if(odd==true){" but the comparison (not assignment) with true adds nothing and can be opportunity for a mistake as you have found.

    -----

    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.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: while loop repeat word until user types "stop", code problem

    Quote Originally Posted by JMAsterson View Post
    What are code tags?
    This site has a FAQs section, so that the same questions don't have to be answered over and over again in the technical forums. Did you think of looking there to find out?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: issue with my While loop

    Double posted question: while loop repeat word until user types "stop", code problem

    Merging the two threads. Please don't do this again.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default 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

  1. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  2. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 05:40 PM
  3. Setting up "Stop" and "Resume" with SourceDataLine
    By davisburns in forum Advanced Java
    Replies: 0
    Last Post: 03-08-2011, 03:05 PM
  4. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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