Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Beginner, stuck on implementing while loop, compiles fine but still won't run

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Beginner, stuck on implementing while loop, compiles fine but still won't run

    Ahoy there,

    Thanks for taking the time to read this. I'm in a programming class and this is my first adventure into the subject.

    I'm working on a code that is supposed to be a number guessing game.

    Here's where I am at so far

     
     
    import java.util.*;
     
    import java.io.*;
     
    public class number_guessing_game {
     
       public static void main (String[] args) {
     
        BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
     
        double attempts = 0;
        double random = 0;
        double guess = 0;
        String go = "";
     
     
        Random generator = new Random ();
        int answer = generator.nextInt(10) + 1;
     
     
        while(go.equals("yes")) {
            System.out.println("How many chances do you want? ");
            try {
               attempts = Double.parseDouble(input.readLine());
            }
            catch (IOException E) {
              System.out.println(E);
            }
     
            System.out.println("What's the magic number? (1-10)? ");
            try {
               guess = Double.parseDouble(input.readLine());
            }
            catch (IOException E) {
              System.out.println(E);
            }
     
     
     if (guess == answer) {
            System.out.println("Congrats! You're guess is right");
            }
     
            else if (guess != answer) {
            System.out.println("Sorry, that is not correct! Would you like to try again?");
             try {
                go = input.readLine();
              }
              catch (IOException E) {
              System.out.println(E);
              }
              }
          }
     
     }
    }

    I exit nano, compile with no exceptions, but when I go to run it, I just get the next line:

    student21@slisdl1:~/LS590/Labs/Lab4> javac number_guessing_game.java
    student21@slisdl1:~/LS590/Labs/Lab4> java number_guessing_game
    student21@slisdl1:~/LS590/Labs/Lab4>

    Any ideas? I've gone over it a ton and still can't put my finger on the problem.

    Thank you in advance for any suggestions


  2. #2
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Beginner, stuck on implementing while loop, compiles fine but still won't run

    i'm running it but im not getting anything.

    is the user supposed to input something?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner, stuck on implementing while loop, compiles fine but still won't run

    Quote Originally Posted by clydefrog View Post
    i'm running it but im not getting anything.

    is the user supposed to input something?
    Yes, sorry should have put that out there. They're supposed to say how many guesses they want and then attempt to guess the randomly generated number in that many tries.

    If I take the while loop at it works fine.

  4. #4
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Beginner, stuck on implementing while loop, compiles fine but still won't run

    when i run it nothing happens. but when i change the variable String go = ""; to String go = "yes"; it appears to be work as far as i know

    so yeah...lol. was that the issue? because when you have it as String go = ""; then basically the while loop is never hit, which in turn never allows for a guess.
    Last edited by clydefrog; March 1st, 2012 at 01:39 AM.

  5. The Following User Says Thank You to clydefrog For This Useful Post:

    GregC (March 1st, 2012)

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner, stuck on implementing while loop, compiles fine but still won't run

    Holy cow, yep that did it.

    I tried testing that earlier, but I didn't realize I had it as go = "yes " with a space so it never recognized my yes. /facepalm

    Thanks so much!

  7. #6
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Beginner, stuck on implementing while loop, compiles fine but still won't run

    haha no problem.

    yeah, the simplest mistakes are the hardest to find.

Similar Threads

  1. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 04:01 AM
  2. Code compiles fine but doesn't run Properly
    By nadirkhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2011, 12:46 PM
  3. Stuck on a few Statements/Questions on timing of Main Game Loop.
    By StevenW in forum Java Theory & Questions
    Replies: 3
    Last Post: July 19th, 2011, 09:20 AM
  4. [SOLVED] While Loop Help (beginner)
    By Perplexing in forum Loops & Control Statements
    Replies: 4
    Last Post: October 23rd, 2010, 02:00 PM
  5. Stuck -- Loop Pattern
    By CodeNewb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 11th, 2010, 03:02 AM