Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

help needed on inputting - tearing hair out .. please!!!

Thread view: 
patrick_woflian - 28 Nov 2005 16:16 GMT
hey guys, i need help with a very annoying problem...

i am doing a program where the user inputs whether they think a random
number will be higher or lower than their own guess if it is not
correct... the problem im having is with the following code:

            System.out.print(" Do you think the next number will be higher HI or
lower LO than " + x  );

            int y = in.nextInt();
            String guess = in.nextString();

            boolean correct = false;
            if (guess.equals("HI"))
            {
               correct = (guess > y);
            }
            else if (guess.equals("LO"))
            {
               correct = (guess < y);
            }
            System.out.println("The number was " + y + " and you guessed " +
guess + " so you " + (correct ? "ROCK!" : "SUCK!"));

the main problem on the compiler is to do with string and guess....any
ideas what to do? my hair can't take anymore pulling..
Michael Redlich - 28 Nov 2005 16:40 GMT
Hi Patrick:

First, I see that you are trying to compare a string with an int
followed by an assignment to a boolean in the lines:
   correct = (guess > y);
   correct = (guess < y);
I assume that's where your program is not compiling since there are
three different data types being used here.

What is the type of the variable, in?  Are you capturing input from the
command line or a text file?

I may have an example program for what you are trying to accomplish.
It's on my laptop at home, but I have a CD archive here at work, and I
will look for it this afternoon.

Sincerely,

Mike.
patrick_woflian - 28 Nov 2005 17:32 GMT
import java.util.Scanner;
import java.util.Random;

public class dice
    {
        public static void main (String [] args)
            {
                Scanner in = new Scanner(System.in);

                Random rnd = new Random();

            int correctguesses;
            correctguesses = 0;

                int r = rnd.nextInt(12) + 1;

            System.out.print("guess a number ");
            int x = in.nextInt();

                if (x  != r)

            System.out.print(" Do you think the next number will be higher HI or
lower LO than " + x  );

            int y = in.nextInt();
            String guess = in.next();

            boolean correct = false;
            if (guess.equals("HI"))
            {
               correct = ( x > y);
            }
            else if (guess.equals("LO"))
            {
               correct = (x < y);
            }
            System.out.println("The number was " + x + " and you guessed " + y +
" so you " + (correct ? "ROCK!" : "SUCK!"));

            }

        }

this is the program in its full, i am trying to capture input from the
text file.. thanks very much for your help

patrick
Roedy Green - 28 Nov 2005 17:44 GMT
On 28 Nov 2005 09:32:39 -0800, "patrick_woflian"
<gingercrock@hotmail.com> wrote, quoted or indirectly quoted someone
who said :

>else if (guess.equals("LO"))
>            {
>               correct = (x < y);
>            }

what if answered something else besides HI or LO
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Michael Redlich - 28 Nov 2005 18:10 GMT
Patrick:

Thanks for posting all the code for this application.  I will
definitely evaluate by tonight.

In the meantime, you may be able to get something out of the example I
found on my CD.  It was a small application that Chuck Allison
published in one of his articles a few years ago.  See
http://www.freshsources.com/ for more about Chuck Allison's work.  The
code is as follows:

import java.io.*;

public class HiLo
    {
    public static void main(String[] args) throws IOException
        {
        BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
        boolean done = false;
        while(!done)
            {
            boolean found = false;
            int lo = 1;
            int hi = 100;
            int guess = 0;
            while(!found && lo <= hi)
                {
                guess = (lo + hi) / 2;
                System.out.println("Is it " + guess + "?");
                char r = in.readLine().toUpperCase().charAt(0);
                if(r == 'H')
                    lo = guess + 1;
                else if(r == 'L')
                    hi = guess - 1;
                else if(r != 'Y')
                    System.out.println("Try again...");
                else
                    found = true;
                }
            if(lo > hi)
                System.out.println("You cheated!");
            else
                System.out.println("Your number was " + guess);
            System.out.println("Want to play again?");
            done = in.readLine().toUpperCase().charAt(0) != 'Y';
            }
        }
    }

Please contact me if you have any questions...

Mike.
Oliver Wong - 28 Nov 2005 20:12 GMT
> import java.util.Scanner;
> import java.util.Random;
[quoted text clipped - 41 lines]
> this is the program in its full, i am trying to capture input from the
> text file.. thanks very much for your help

   I don't know what file you'r referring to, but your program compiles
without error. As for running it... well... it might not do exactly what you
intend (but then again, you haven't said what you intend for it to do).

   I'll just point out that your program seems to be expecting the user to
enter in 2 integers in a row, but the prompts displayed to the user give no
indication of this.

   In other words, the program asks me to "guess a number", so I do (I
guessed '3'), and then it asks me "Do you think the next number will be
higher HI or lower LO than 3", so I type in "HI", but the program throws an
exception 'cause it was actually expecting me to enter in an integer, as
seen on the line "y = in.nextInt();".

   - Oliver
patrick_woflian - 28 Nov 2005 22:53 GMT
well.. i dont know how wrong my code is.. but i desparetly need help
hehe.. the psuecode solution is as follows:

Roll a 12 sided die to choose a number in the range 1....12
while the user has not made an incorrect guess
ask the user to guess whether they think the next number will be higher
or lower than the current number
prompt the user to enter either "hi" or "lo"
choose a new random number by rolling a 12 sided die
if the users guess was correct
increment a count of correct guesses
update the stroed current number
end if
end while
the game is over - tell the user how many correct guesses they made..

any ideas how to change my program!?!?

patrick
Oliver Wong - 28 Nov 2005 22:59 GMT
> well.. i dont know how wrong my code is.. but i desparetly need help
> hehe.. the psuecode solution is as follows:
[quoted text clipped - 13 lines]
>
> any ideas how to change my program!?!?

   I noticed that in your pseudo code, it sounds like you only ever ask the
user for a number once (not sure though, because the "while the user has not
made an incorrect guess" part is a bit vague to me). If that's the case, you
should really re-evaluate why in your Java code you're asking the user for a
number twice.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.