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 4 of 4

Thread: Catch block problem. Please fix my problem.

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Catch block problem. Please fix my problem.

    Im having a class GuessingGame.java that should compiles and runs. However, if the user enters something which is not a number (eg. "Dunno"), the program crashes. Add a catch block to the code so that instead of crashing, the program prints "That's not even a number!" and asks the user to pick another number as usual.

    This are my codes. I can't run this because I dont know how to use that "Catch-block" problem.

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.util.Random;

    public class GuessingGame
    {
    private int target;
    private BufferedReader in;

    public GuessingGame()
    {
    Random rand;

    rand = new Random();
    target = 1 + rand.nextInt(10);
    in = new BufferedReader (new InputStreamReader (System.in), 1);
    }

    private int guess()
    {
    String line;
    while (true)
    {
    try
    {
    System.out.print("Your guess: ");
    line = in.readLine();
    return Integer.parseInt(line);
    }
    catch (IOException e){
    System.out.println("That's not even a number!: " + e);
    } }

    }

    public void play()
    {
    int num;

    num = guess();
    while (num != target)
    {
    if (num < target)
    System.out.println("Higher");
    else if (num > target)
    System.out.println("Lower");
    num = guess();
    }
    System.out.println("You got it!");
    }

    public static void main(String args[])
    {
    HulaanMo game;
    game = new HulaanMo();
    System.out.println("Guess a number between 1 and 10");
    game.play();
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Catch block problem. Please fix my problem.

    Welcome to the forum! Please read this topic to learn how to post code correctly along with other useful info for newcomers.

  3. #3
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Catch block problem. Please fix my problem.

    hi friend,

    you can use the while with in try and catch block like this,

    private int guess()
    {
    int line=0;                                      //chaned the datatype
    try
    {
    while (true)
    {
     
    System.out.print("Your guess: ");
    line = Integer.parseInt(in.readLine());  //Here i parse the string to int in a single statment
     
    }
     
    }
    catch (Exception e){                   //Here is given only Exception instead of IOException
    System.out.println("That's not even a number!: " + e);
    }
    return line;                                //Here i put return type
    }

    It may work.

    Thanks,
    Last edited by Ganeprog; February 3rd, 2014 at 12:38 AM. Reason: comments given

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Catch block problem. Please fix my problem.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Random;
     
     
    public class Demo
    {
    	private int target;
    	private BufferedReader in;
     
    	public Demo()
    	{
    		Random rand;
     
    		rand = new Random();
    		target = 1 + rand.nextInt(10);
    		in = new BufferedReader (new InputStreamReader (System.in));
    	}
     
    	private int guess() throws Exception
    	{
    		int line;
    		while (true)
    		{
     
    				System.out.print("Your guess: ");
    				line = Integer.parseInt(in.readLine());
    				return (line);
    		}
     
    	}
     
    	public void play()
    	{
    		int num;
     
    		try
    		{
    		num = guess();
    		while (num != target)
    		{
    			if (num < target)
    				System.out.println("Higher");
    			else if (num > target)
    				System.out.println("Lower");
    			num = guess();
    		}
    		System.out.println("You got it!");
    		}
    		catch(Exception e)
    		{
    			System.out.println("That's not even a number! ");
    		}
     
    	}
     
    	public static void main(String args[])
    	{
    		Demo game;
    		game = new Demo();
    		System.out.println("Guess a number between 1 and 10");
    		game.play();
    	}
    }


    --- Update ---

    Please do study code.
    Last edited by Praful Chougale; February 3rd, 2014 at 01:48 AM. Reason: Msg

Similar Threads

  1. Replies: 4
    Last Post: November 16th, 2012, 02:57 PM
  2. InputMismatchException try catch block not working as expected
    By mikhl in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 23rd, 2011, 07:02 PM
  3. try-catch block reports to have an Error that I don't know how to fix
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 24th, 2011, 06:27 AM
  4. Little easy to fix problem
    By adammint7 in forum Java Theory & Questions
    Replies: 9
    Last Post: April 14th, 2011, 10:07 AM
  5. Need Help i cant fix this problem ...
    By mico332 in forum Member Introductions
    Replies: 3
    Last Post: March 18th, 2011, 06:12 PM