Results 1 to 8 of 8
  1. #1
    zahkloxx is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Question Help needed with my little game!

    Hi there!
    New to Java (sort of) and I'm trying to make a little guessing game, it worked perfectly! untill i had people accidentally entering letters.. instead of numbers :/ SO I TRIED TO MAKE A FILTER. big fail

    help me out?


    main class:
    Java Code:
    package guessgame;
    
    import java.util.*;
    
    public class GuessGame {
    
        public static int tries = 0;
        public static int numb;
        Random rnd = new Random();
        
        public static void main(String[] args) {
            Scanner user_input = new Scanner(System.in);
            System.out.print("Enter your name: ");
            String input_name = user_input.next();
            Player.setName(input_name);
            
            System.out.println("Welcome " + Player.Name);
            System.out.println("It's time to play the game..\n"
                    + "I am thinking of a number between 1 and 9, can you guess it?");
            generateNumber();
            playGame();
       }
        
            public static void playGame() {
                Scanner user_input = new Scanner(System.in);
                System.out.print("Make a guess: ");
                String input_guess = user_input.next();
                if (isNumeric(user_input) == false) {
                    System.out.println("That is not a number..");
                    System.out.println("Lets try again!");
                    playGame();
                }  else {
                    tries++;
                    if (numb != input_guess) {
                        System.out.println("Sorry, wrong answer!");
                        playGame();
                    } else {
                        System.out.println("That was the right answer " + Player.Name + " wins!");
                        if (tries > 1) {
                            System.out.println("It took you " + tries + " tries!");
                        } else {
                            System.out.println("It took you " + tries + " try!");
                        }
                    }
                }
               
           }
            
            public static void generateNumber() {
                Random rnd = new Random();
                numb = rnd.nextInt(9);
            }
            
            public boolean isNumeric(String input) {
                try {
                    Integer.parseInt(input);
                    return true;
                }
                catch(NumberFormatException e) {
                    return false;
                }
            }
    }
    Player class:
    Java Code:
    package guessgame;
    
    import java.util.*;
    
    public class Player {
        public static String Name;
        
        public static void setName(String toThis) {
            Name = toThis;
        }
    }

    OK SO the problem is that as you can see i am here trying to parse my string into an int..
    Java Code:
    ........
                if (isNumeric(user_input) == false) {
                    System.out.println("That is not a number..");
                    System.out.println("Lets try again!");
                    playGame();
                }  else {
                    tries++;
                    if (numb != input_guess) {
                        System.out.println("Sorry, wrong answer!");
                        playGame();
                    } else {
    ........
    Method used to parse:
    Java Code:
            public boolean isNumeric(String input) {
                try {
                    Integer.parseInt(input);
                    return true;
                }
                catch(NumberFormatException e) {
                    return false;
                }
            }
    but it says it's NOT a string but rather a scanner, now.. how do I convert a scanner to an int?

    would be very happy if someone could help me out! C:
    Last edited by zahkloxx; 01-11-2013 at 04:51 PM. Reason: ..because.. ahm.. it needed editing? :D

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Help needed with my little game!

    Which line(s) are throwing the error?

  3. #3
    zahkloxx is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Help needed with my little game!


  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Help needed with my little game!

    Your variable user_input is not a String, but that of a Scanner object. You have a String for user input, why not use that?

  5. #5
    zahkloxx is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Help needed with my little game!

    How do i ahh.. do that?

  6. #6
    zahkloxx is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Help needed with my little game!

    Ok I changed it! i found out! one error is solved! one left!

    Java Code:
                    if (numb != input_guess) {
    is still giving this error: Imcomparable types: int and string.

  7. #7
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Help needed with my little game!

    Quote Originally Posted by zahkloxx View Post
    Ok I changed it! i found out! one error is solved! one left!

    Java Code:
                    if (numb != input_guess) {
    It's as it says. You can't compare an int to a String.

    is still giving this error: Imcomparable types: int and string.
    It's as it says. You cannot compare a String and an int.

  8. #8
    zahkloxx is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Help needed with my little game!

    Oh, haha I see now, ok i fixed it, it's good.
    't was just me being stupid C:

    Thanks alot folkes!
    much much appreciated!

Similar Threads

  1. GROUP PROJECT : JAVA GAME : Up to Three Programmers Needed
    By Wesley.laferriere in forum Jobs Offered
    Replies: 8
    Last Post: 05-09-2013, 12:47 AM
  2. Replies: 4
    Last Post: 07-08-2012, 10:07 AM
  3. Help needed with simple Java Snake game
    By AndySun94 in forum New To Java
    Replies: 1
    Last Post: 04-01-2012, 07:59 AM
  4. Replies: 1
    Last Post: 06-02-2011, 10:29 AM
  5. very void here, game ideas needed
    By j2me64 in forum Java Gaming
    Replies: 4
    Last Post: 02-25-2010, 08:49 AM

Posting Permissions

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