Results 1 to 2 of 2
  1. #1
    Kerrai is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default A few problems with my code..

    Hi

    So a while back i asked for help with my sorting of arrayLists and i finally got that working.

    But sadly i ran into some other problems, hopefully some of you for some ideas on how i can fix this.

    So, the thing is. When i enter something other then an integer it should try to catch that and return that the user needs to input an integer between 0 and 1000. The thing is tho, if i entered an integer before i typed for example "hrrhrr" it also prints out that i guess to low or to high depending on the last entry.

    Any thoughts on why and how i could solve it?

    Also, i need to start
    Java Code:
    long startTime = System.currentTimeMillis();
    After the users first input, and then it should count untill he get it correctly.
    But as it looks now it resets after each try, and if i put it before the "while (guess != number) loop it starts to count from when i run it.

    Any thoughts on this as well?


    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class guessNumber {
    
    
        public void Start() {
    
        	
            Random random = new Random();
            boolean playAgain = true;
    
            String answer = "Y";
            String quit = "Quit";
    
            ArrayList<Score> scores = new ArrayList<Score>();
            
            while (playAgain == true)
            {
            	System.out.println("\nWelcome to Guess the Number!!"); // Welcomes you to the game and sets some integers that are to be used.
            	String name;
             	int guess = -1;
                int tries = 0;
                int number = random.nextInt(1000) + 1; // Randomize a number
                System.out.println(number);
                System.out.println("\nPlease guess a number between 0 and 1000: "); // User inputs his first guess
            	
            while (guess != number){
            	Scanner input = new Scanner(System.in);
            	long startTime = System.currentTimeMillis();	//After first guess the timer starts
    
                String guess1 = input.next(); //A string to check for the "quit" command.
                
                
                if (guess1.equalsIgnoreCase(quit)) //If user enters quit, exit game.
                { 
                	System.out.println("Thanks for playing!");
                	System.exit(0);
                }
                
                try {
                	guess = Integer.parseInt(guess1); //Tries to catch an exception if the user tries to input something else then quit, or a number between 0 and 1000
                } catch (NumberFormatException nfe) {
                	System.out.println("Please enter a \"Number\" between 0 and 1000: ");
                }
    
                
    
                if (guess > number && guess < 1001) { //checks if the input is bigger than the number and smaller than 1000.
                    System.out.println("Your guess is too high, try with a lower number: ");
                tries++;
                }
                else if (guess < number && guess >= 0) { //checks if the input is smaller than the number and bigger than 0.
                    System.out.println("Your guess is too low, try with a higher number: ");
                tries++;
                }
                if (guess <-1 || guess > 1001) { //Checks if the input is smaller then -1 or bigger then 1001 and promts the user to enter a number between 0 and 1000 instead.
                    System.out.println("Try between 0 and 1000 instead: ");
                }
                
                if (guess == number) { //if the input is the same as the number tell the user he has it correct.
                    System.out.println("\nCorrect!");
                tries++;
                
                
                long endTime = System.currentTimeMillis(); //counts the time the user has been playing from first input.
    			long gameTime = endTime - startTime; //timer ends. 
        
                if (tries == 1) //if he got it right on the first try, we set the time to 1 second. 
                {
                	gameTime = 1000;
                }
                
                System.out.println("\nYou guess the correct answer in " + tries + " tries and " + (gameTime/1000) + " seconds" );
                    
                System.out.println("\nEnter your name: "); //Prompt the user to input his name. 
                name = input.next();
                
    			Score currentScore = new Score(tries, gameTime, name); //save the variables to the arrayList.
    			scores.add(currentScore);
    			
                    System.out.print("\nPlay again? (Yes or No)" ); //Ask the user if he wants to play again or not. 
                    answer = input.next();
    
                    if (answer.equalsIgnoreCase("Y")) { //If he/she wants to play again, print out the existing high score list and start the program over. 
                    	System.out.println("\nHigh Score\nName \t Guess \t Time");
                    	Collections.sort(scores);
                    	for (int i = 0;i < scores.size(); i++)
                    	{
                    	System.out.println(scores.get(i));
                    	playAgain = true;
        				}
                    }
                    else if (answer.equalsIgnoreCase("N")) { //if he/she dont want to play again, exit game. 
                         playAgain = false;
                    }
                    else {
                        playAgain = false;
                     } 
                    if (playAgain == false) {
                     System.out.println("\nThank you for playing!");   
                     
    
                    }
                }
            	}
            }
        }
       }
    i'l provide all the code if someone thinks is needed.
    Java Code:
    import java.util.*;
    
    public class Game {
    	
    	public static void main(String[] args) {
    
    		List<Score> scores = new ArrayList<Score>();
    	    Scanner input = new Scanner(System.in);
    	    {
    	    	guessNumber newGame = new guessNumber();
    	    	newGame.Start();
    	    }
    	}
    }
    Java Code:
    import java.util.*;
    
    
    public class Score implements Comparable<Score>{  
    
    	int theScore;
    	double theTime;
    	String playerName;
    	
    	
    	public Score (int theScore, double theTime, String playerName){
    		this.theScore = theScore;
    		this.theTime = theTime;
    		this.playerName = playerName;
    		}
    	public int getScore() {
            return theScore;
        }
    
        public String getName() {
            return playerName;
        }
        public double getTime() {
        	return theTime;
        }
    	
    	public int compareTo(Score sc1) {
    		if(getScore() < sc1.getScore()) return -1;
    		if(getScore() > sc1.getScore()) return 1;
    		
    		else if (getScore() == sc1.getScore()) 
    			if (getTime() < sc1.getTime()) return -1;
    			if (getTime() > sc1.getTime()) return 1;
    			return 0;
    		}
    	
    
    
    	public String toString(){
    		
    		String highScorelist = (playerName + "\t" + " " +  theScore + "\t" +" " + ((int)theTime/1000));
    		return highScorelist;
    			}
    
    			
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    if i entered an integer before i typed for example "hrrhrr" it also prints out that i guess to low or to high depending on the last entry.

    Java Code:
    try {
        guess = Integer.parseInt(guess1);
    } catch (NumberFormatException nfe) {
        System.out.println("Please enter a \"Number\" between 0 and 1000: ");
    }
    if (guess > number && guess < 1001) {

    The code is doing exactly what you told it to do. You catch the number format exception and then print a message but then continue on as if nothing had happened.

    You might find it easier if you separated out the input with its validation (both the exception business and the range checking) into another method.

    Also, watch the names: GuessNumber, start().

Similar Threads

  1. Problems with my source code
    By bilak09 in forum New To Java
    Replies: 10
    Last Post: 04-26-2010, 07:23 AM
  2. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  3. Problems with code
    By jforce93 in forum New To Java
    Replies: 8
    Last Post: 08-14-2009, 03:49 AM
  4. problems with java code! (very new - need help asap!)
    By sumkindafreek in forum New To Java
    Replies: 1
    Last Post: 01-07-2009, 06:00 AM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •