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

Thread: Throw and catches not working as desired

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Post Throw and catches not working as desired

    So i want user to input either 1 or 2
    If the user enters any number greater than 2
    I want it to throw an exception and print "either 1 or 2"
    If the user enters a string or anything other than integer i want to throw another exception and print "input an integer"
    and then i will make them repeat the whole progress if exceptions are met
    using boolean inside a catch
    but its not working as desired
    it doesnt print "either 1 or 2"
    or "input an integer" when needed
    but the loop works fine until i press 1 or 2
    this is a part of my project so ignore if some stuff looks incomplete
    EDIT: loop never ends,
    System.out.println("press 1 for days or 2 for years");
    		do {
    		input.next();
    		 //gonna ask for input if it is wrong or if they enter something more than 2
    		try
    		{
    		min = input.nextInt();
    		repeat = false;
    		if (input.hasNext()) //gonna throw error if wrong input
    		{
    			throw new Exception("wrong input");
    		}
    		if (min>2) //error when its not 1 or 2
    		{
    			throw new Exception("entereither 1 or 2 dumbass");
    		}
    		}
    		catch (Exception a) //we will catch the error and then make it repeat the whole progress
    		{
    			repeat = true;
    		}
     
    		} while (repeat==true);
    		}
    Last edited by kukupie123; May 12th, 2019 at 06:14 AM.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Throw and catches not working as desired

    You need to display the error message in catch block

     public static void main(String[] args) {
     
            Scanner input = new Scanner(System.in);
            int min = 0;
            boolean repeat = true;
     
            do {
                //gonna ask for input if it is wrong or if they enter something more than 2
                System.out.println("press 1 for days or 2 for years");
                try {
                    min = input.nextInt();
                    repeat = false;
                    if (min > 2) //error when its not 1 or 2
                    {
                        repeat = true;
                        System.out.println("Either enter 1 or 2 ");
                    }
                } catch (Exception a) //we will catch the error and then make it repeat the whole progress
                {
                    repeat = true;
                    System.out.println("input an integer");
                    input.next();
                }
            } while (repeat == true);
        }
    Last edited by John Joe; May 12th, 2019 at 10:01 AM.
    Whatever you are, be a good one

  3. The Following User Says Thank You to John Joe For This Useful Post:

    kukupie123 (May 13th, 2019)

Similar Threads

  1. Not getting desired result
    By cybergalaxy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 4th, 2019, 09:42 AM
  2. [SOLVED] Code is not giving the desired output
    By Tree_Bek in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 07:27 PM
  3. No Error in Code but Output is not desired...!!!
    By Adi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 28th, 2011, 08:56 AM
  4. A few opinions on a project desired
    By LDM91 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2010, 02:36 PM
  5. HELP-WHY THE O/P IS NOT AS DESIRED.???
    By shreyash37 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 20th, 2010, 02:45 PM

Tags for this Thread