# random number generator println problem?!?!?

Page 1 of 1

## 6 Replies - 584 Views - Last Post: 28 February 2012 - 12:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=268593&amp;s=07a87aab945b033653d9feb2868eb6e1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hellswindstaff

Reputation: 1
• Posts: 20
• Joined: 27-February 12

# random number generator println problem?!?!?

Posted 27 February 2012 - 06:59 PM

/* For some reason the System.out.println statements for all of the "else if"
statements will not be displayed.
**/

```/*This is a game in which the user guesses
which number the random number generator created.
**/

import java.util.Random;
import java.util.Scanner;

class ch4no17
{
public static void main(String[] args) {
Random ranNum = new Random();// random number
int number;
int numInput = 0;
number = ranNum.nextInt(25);//guess between 0-25
Scanner numIn = new Scanner(System.in);//allows user to input number
numInput = numIn.nextInt();//sets user input equal to number
System.out.println(numIn.nextLine());//displays user input

//pre-test loop which tests to see if user has guess the
//randomly generated number correctly

while (numInput != number)
{
numInput = numIn.nextInt();
}

if (numInput == number)
System.out.println("Congratulations! You got it right!");

else if (numInput < number)
System.out.println("Too low, try again");

else if (numInput > number)
System.out.println("Too high, try again");

}
}

```

Is This A Good Question/Topic? 0

## Replies To: random number generator println problem?!?!?

### #2 zehawk

Reputation: 4
• Posts: 213
• Joined: 23-February 12

## Re: random number generator println problem?!?!?

Posted 27 February 2012 - 07:01 PM

Y do u have a .nextLine in there? just have a System.out.println(numIn);

### #3 exiles.prx

Reputation: 65
• Posts: 241
• Joined: 22-November 10

## Re: random number generator println problem?!?!?

Posted 27 February 2012 - 07:22 PM

I bet your code is halting here because its waiting for an input value.
```while (numInput != number) {
numInput = numIn.nextInt();
}
```

EDIT: and it will not exit the while loop until the number you input is equal to number.

This post has been edited by exiles.prx: 27 February 2012 - 07:25 PM

### #4 hellswindstaff

Reputation: 1
• Posts: 20
• Joined: 27-February 12

## Re: random number generator println problem?!?!?

Posted 27 February 2012 - 11:01 PM

I have an value for numInput it is 0. It's at the top.

.nextLine is suppose to be there, but I would have to show you a visual for the program for it to be obvious.

The thing it is running fine. Everything is compiling. It's just not showing;

"Too low, try again"
or
"Too high, try again"

while it is running.

### #5 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: random number generator println problem?!?!?

Posted 27 February 2012 - 11:14 PM

What exiles is saying is that:

Lets say your number is 42.
user enters 41, loop tries again
user enters 39, loop tries again
user enters 42, the condition becomes false exiting the loop.

```while (numInput != number) {
numInput = numIn.nextInt();
}

```

now, you check to see if the numInput is equal to number, which will always happen since you made your loop forces the value to be equal to the number.

This post has been edited by Mylo: 27 February 2012 - 11:15 PM

### #6 jdavi134

Reputation: 42
• Posts: 225
• Joined: 26-October 11

## Re: random number generator println problem?!?!?

Posted 27 February 2012 - 11:15 PM

I'm confused, why are your "else if" statements not within the while loop?

Don't you want it to run until you guess correctly? If you keep asking them for an input over and over until they get it right, then of course the statements will never be printed. Those statements need to be inside the while loop.

### #7 hellswindstaff

Reputation: 1
• Posts: 20
• Joined: 27-February 12

## Re: random number generator println problem?!?!?

Posted 28 February 2012 - 12:42 AM

Wow... okay I see now. Careless error.

ty guys/ gals!