Click here to Skip to main content
15,908,437 members
Home / Discussions / Java
   

Java

 
GeneralRe: abt java project Pin
Richard MacCutchan23-Oct-10 21:52
mveRichard MacCutchan23-Oct-10 21:52 
GeneralRe: abt java project Pin
Cedric Moonen24-Oct-10 20:41
Cedric Moonen24-Oct-10 20:41 
GeneralRe: abt java project Pin
_Erik_25-Oct-10 4:58
_Erik_25-Oct-10 4:58 
Questionjava stacks Pin
Sylvane23-Oct-10 4:18
Sylvane23-Oct-10 4:18 
AnswerRe: java stacks Pin
Richard MacCutchan23-Oct-10 21:47
mveRichard MacCutchan23-Oct-10 21:47 
QuestionJTabel Cell Pin
trioum22-Oct-10 19:53
trioum22-Oct-10 19:53 
AnswerRe: JTabel Cell Pin
David Skelly24-Oct-10 22:16
David Skelly24-Oct-10 22:16 
QuestionNeed help with this code [modified] Pin
zooropean720-Oct-10 16:33
zooropean720-Oct-10 16:33 
Hello, I have created a do-while loop which is supposed to re-run if the user enters "y" or quit if the user enters "n".

It's about a number guessing game. The user must guess the computer's number and if he chooses to quit or if he guesses correctly, he's given the option to replay.The game is supposed to accept as many guesses are necessary until the number is found but when I run it I'm only able to enter one number. On the second attempt I get the error "Exception in thread "main" java.lang.NullPointerException
at Doloop2.main(Doloop2.java:60)"

Can someone help me out? I would appreciate any help.


I am posting my code hoping that it will make things clearer.

import java.util.*;

public class Doloop2
{
	public static void main (String[] args)
	{
		final int MAX = 100;
		int answer, guess, attempt = 0;
		String replay = null;
		
		Scanner scan = new Scanner (System.in);
		Random generator = new Random();
		
		
		answer = generator.nextInt(MAX) + 1;
		
		System.out.print("I am thinking of a number "
						+ "between 1 and 100. Enter your guess (0 to guit):");
		guess = scan.nextInt();
		
		do 
		{		
			if (guess > answer)
			{
				attempt++;
				System.out.print ("Try a smaller number (0 to quit): ");
				guess = scan.nextInt();
			}	
			if (guess < answer)
			{
				attempt++;
				System.out.print ("Try a bigger number (0 to quit): ");
				guess = scan.nextInt();
			}
		
		
			if (guess == answer)
			{
			attempt++;
			System.out.println ("You got it!");
			System.out.println ("Number of total attempts:" + attempt);
			System.out.println ();
			System.out.print("Play again? (y/n)");
			
			 replay = scan.nextLine();
			
			}
			if (guess == 0)
			{
			System.out.println ("You ended the game. Play again? (y/n):");
			
			 replay = scan.nextLine();
			}
			
		}
		
		while (replay.equals("y") || replay.equals("Y"));
		
	}
}


modified on Wednesday, October 20, 2010 11:07 PM

AnswerRe: Need help with this code Pin
Bugclear20-Oct-10 21:32
Bugclear20-Oct-10 21:32 
GeneralRe: Need help with this code Pin
Richard MacCutchan20-Oct-10 22:10
mveRichard MacCutchan20-Oct-10 22:10 
GeneralRe: Need help with this code Pin
zooropean721-Oct-10 8:33
zooropean721-Oct-10 8:33 
GeneralRe: Need help with this code Pin
Richard MacCutchan21-Oct-10 11:21
mveRichard MacCutchan21-Oct-10 11:21 
GeneralRe: Need help with this code Pin
zooropean721-Oct-10 12:16
zooropean721-Oct-10 12:16 
GeneralRe: Need help with this code Pin
Bugclear21-Oct-10 20:31
Bugclear21-Oct-10 20:31 
GeneralRe: Need help with this code Pin
TorstenH.21-Oct-10 22:52
TorstenH.21-Oct-10 22:52 
GeneralRe: Need help with this code Pin
Bugclear21-Oct-10 23:22
Bugclear21-Oct-10 23:22 
GeneralRe: Need help with this code Pin
Richard MacCutchan22-Oct-10 3:05
mveRichard MacCutchan22-Oct-10 3:05 
QuestionException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
vidhyaej20-Oct-10 0:19
vidhyaej20-Oct-10 0:19 
AnswerRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
TorstenH.20-Oct-10 0:56
TorstenH.20-Oct-10 0:56 
GeneralRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
vidhyaej20-Oct-10 1:03
vidhyaej20-Oct-10 1:03 
GeneralRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
TorstenH.20-Oct-10 1:19
TorstenH.20-Oct-10 1:19 
GeneralRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
vidhyaej20-Oct-10 1:29
vidhyaej20-Oct-10 1:29 
QuestionClasses and methods Pin
vidhyaej19-Oct-10 21:28
vidhyaej19-Oct-10 21:28 
AnswerRe: Classes and methods Pin
Nagy Vilmos19-Oct-10 22:01
professionalNagy Vilmos19-Oct-10 22:01 
GeneralRe: Classes and methods Pin
vidhyaej19-Oct-10 22:58
vidhyaej19-Oct-10 22:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.