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: New assignment new problem...

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default New assignment new problem...

    Hey,

    Well I passed my last assignment that I had help from here on with a fairly high grade. Now that I've run into another little problem I decided to come back and get some help again. =) The problem this time is that when I run my program in the console it freezes after Player 2 enters a number; I know this has something to do with my 'while' loop but I can't seem to see any problem with the way it's set up.

    import java.io.*;
     
    class Assignment7Question1
    {
        public static void main (String[] args) throws IOException
        {	
    		int intFirstInt = 0, intGuesses = 0, intGuessedNumber = 0; //Creates the three variables that will be used in this program.
     
            InputStreamReader inStream = new InputStreamReader (System.in);
            BufferedReader userInput = new BufferedReader (inStream);
            String inputData; //Creates one string variable
    		System.out.println ("Player 1:"); //Prints the sentence to the cmd.
            System.out.println ("Please input a positive number that is less than 1000:"); //Prints the sentence to the cmd.
            inputData = userInput.readLine ( ); //Places input into inputData string variable.
    		intFirstInt = Integer.parseInt(inputData); //Sets the value saved into inputData to the intFirstInt variable.
     
    		System.out.println ("Player 2:"); //Prints the sentence to the cmd.
    		System.out.println ("Please enter the number that you believe Player 1 has entered."); //Prints the sentence to the cmd.
    		inputData = userInput.readLine(); //Places input into inputData string variable.
    		intGuessedNumber = Integer.parseInt(inputData); //Sets the value saved into inputData to the intGuessedNumber variable.
     
    		while (intGuessedNumber > intFirstInt | intGuessedNumber < intFirstInt);
    		{
    			intGuesses = (intGuesses + 1); //Counts the number of guesses that Player 2 takes to get the correct number.
     
    			if (intGuessedNumber > intFirstInt)
    				System.out.println ("Your guess is too low! Please try again."); //Prints the sentence to the cmd.
    			else if (intGuessedNumber < intFirstInt)
    			{
    				System.out.println ("Your guess is too high! Please try again."); //Prints the sentence to the cmd.
    			}
     
    			inputData = userInput.readLine(); //Places input into inputData string variable.
    			intGuessedNumber = Integer.parseInt(inputData); //Sets the value saved into inputData to the intGuessedNumber variable.
    		}
     
    		if (intGuessedNumber == intFirstInt)
    		{
    			System.out.println ("Congratulations, you guessed correctly!");
    			System.out.println ("It took you "+intGuesses+" guesses to determine the number.");
    		}
    	}
    }


    Sorry for some of the indentation being off, when I CnP the code from Notepad++ to here it messes it up.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: New assignment new problem...

    Hi tyeeeee1! First of all, I'd like to mention that I'm still a beginner in java myself, so I'll apologize ahead of time if I give you any false information. Anyways, on to your problem. I'd like to recommend using print statements to try and debug this. I quickly went over your code, which you could edit and fix the indents, and I understand what you're trying to do. I'd like you to look at your variables and see what inputs they contain: for example, you're Player 1 and 2 name inputs. *hint* Try printing out inputData. Also, really understand your while loop and your if statements. What exactly are you asking your loop and statements to do? Think a bit about the placement of them as well. If you're still confused, try going through your code with pen(cil) and paper and do exactly what the code you're writing does, kind of like you did with the comments.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New assignment new problem...

    while (intGuessedNumber > intFirstInt | intGuessedNumber < intFirstInt); << Remove colon.
    | is not the correct operator. You're looking for ||.

    See: Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: New assignment new problem...

    Quote Originally Posted by newbie View Post
    while (intGuessedNumber > intFirstInt | intGuessedNumber < intFirstInt); << Remove colon.
    | is not the correct operator. You're looking for ||.

    See: Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Newbie has hooked you up, just a small syntax error! I will say this looking through your code though. I have a feeling people are going to be really confused when they guess to high, but the program outputs they guessed to low!

  5. #5
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: New assignment new problem...

    I changed the || to a | while I was trying to fix it up and then forgot to change it back. The error was the ; I had at the end of the while statement, after that I noticed the error that Chico pointed out and fixed it up. Thanks for all the help guys and gals!

  6. #6
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: New assignment new problem...

    IF you don't mind, do you think you can write assignment and post it. I would like to take a crack at it. THanks.

Similar Threads

  1. Problem with School Assignment.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 17th, 2012, 04:12 PM
  2. BankAccount assignment problem
    By ddonn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 6th, 2011, 12:04 PM
  3. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  4. Problem with code for school assignment?
    By Mellisa315 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 16th, 2010, 08:36 PM
  5. Assignment problem.
    By minou13 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 09:51 PM