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

Thread: My first functioning Try Catch program

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default My first functioning Try Catch program

    I have chosen a do-while loop mechanism for my first try catch Java program, but it's kind of a mess:

    package sam;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    import java.util.Scanner;
     
    public class Test {
     
    	public static void main(String[] args){
    		System.out.println("Lets play with some try catch statements.");
    		Scanner input = new Scanner(System.in);
    		int x=1, num1=1, num2=1;
     
    		do{
    			try{				
    				System.out.println("Enter the first (dividend) int: ");
    				num1 = input.nextInt();
    				int parsedNum1 = Integer.parseInt(num1);
    				System.out.println("Enter the second (divisor) int: ");
    				num2 = input.nextInt();	
    				int parsedNum2 = Integer.parseInt(num2);
     
    				int quotient = num1 / num2;//dividend / divisor
    				System.out.println("Our quotient is: " + quotient);
     
    				x = 2;//when the user enters proper input, change x, so that the while condition below can terminate our do while loop.				
    			}
    			catch(NumberFormatException e){
    				//check if num1 is an integer
    				//check if num2 is an integer
    				//statement to catch a NumberFormatException?
     
    				System.out.println("Error. Invalid dividend or divisor. Please try again.");
    				continue;
     
    			}
    		}while(x==1);		
     
    	}//end of main
    }//end of class Test

    Lines 17 and 20 keep throwing the same error, "The method parseInt(String) in the type Integer is not applicable for the arguments (int)".

    I don't understand how the "e" parameter works in line 27's catch statement. Does it have to be "e" for a special reason? Would it still work if I used "fred" for the parameter instead?

    I'm unsure how to proceed making exception handlers. Am I just supposed to use a bunch of if statements? Do I need to use the continue statement in a do-while loop?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My first functioning Try Catch program

    The method parseInt(String) in the type Integer is not applicable for the arguments (int)".
    The parseInt method takes a String value as its argument, not an int value. There would be no need to convert/parse an int value to an int value.
    catch(NumberFormatException e)
    how the "e" parameter works in line 27's catch statement
    It is like the definition of an argument for a method.
    NumberFormatException is the type and e is the name. You can use any name you want. fred would work.

    Note: x is a poor name for a variable that is used to control a loop. Variable names should reflect what kind of values they hold. For the specific case, the variable has 2 values which means a boolean variable could be used. For example stayInLoop would be true to start with and changed to false when the code wants to exit the loop.
    Another possibility would be to use a break statement to exit the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My first functioning Try Catch program

    Quote Originally Posted by SamJava_the_Hut View Post
    I have chosen a do-while loop mechanism for my first try catch Java program, but it's kind of a mess:

    package sam;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    import java.util.Scanner;
     
    public class Test {
     
    	public static void main(String[] args){
    		System.out.println("Lets play with some try catch statements.");
    		Scanner input = new Scanner(System.in);
    		int x=1, num1=1, num2=1;
     
    		do{
    			try{				
    				System.out.println("Enter the first (dividend) int: ");
    				num1 = input.nextInt();
    				int parsedNum1 = Integer.parseInt(num1);
    				System.out.println("Enter the second (divisor) int: ");
    				num2 = input.nextInt();	
    				int parsedNum2 = Integer.parseInt(num2);
     
    				int quotient = num1 / num2;//dividend / divisor
    				System.out.println("Our quotient is: " + quotient);
     
    				x = 2;//when the user enters proper input, change x, so that the while condition below can terminate our do while loop.				
    			}
    			catch(NumberFormatException e){
    				//check if num1 is an integer
    				//check if num2 is an integer
    				//statement to catch a NumberFormatException?
     
    				System.out.println("Error. Invalid dividend or divisor. Please try again.");
    				continue;
     
    			}
    		}while(x==1);		
     
    	}//end of main
    }//end of class Test

    Lines 17 and 20 keep throwing the same error, "The method parseInt(String) in the type Integer is not applicable for the arguments (int)".

    I don't understand how the "e" parameter works in line 27's catch statement. Does it have to be "e" for a special reason? Would it still work if I used "fred" for the parameter instead?

    I'm unsure how to proceed making exception handlers. Am I just supposed to use a bunch of if statements? Do I need to use the continue statement in a do-while loop?
    why you using NextInt? and then Tryin to Parse Int to int value as Norm said .

    you should change your Varies to Strings , int x;
    then try to play with it

    and you can add e.getMessage() to catch block . in my opinion it seems better .

  4. #4
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: My first functioning Try Catch program

    This is a joke. Why is NumberFormatException not doing anything:
    public class Test {
     
    	public static void main(String[] args){
    		Scanner input = new Scanner(System.in);
    		int userInput = 12;
    		boolean stayInLoop = true;
     
    		do{
    			try{				
    				System.out.println("Enter an integer between 0 and 10.");
    				userInput = input.nextInt();
    				//int parsedUserInput = Integer.parseInt(userInput);				
     
    				stayInLoop = false;//break;				
    			}
    			catch(NumberFormatException e){
    				if(userInput > 0 && userInput < 10){
    					System.out.println("Your integer is not between 0 and 10. Try again!"); 
    				}
    				else{
    					e.getMessage();
    				}
    				//statement to catch a datatype input mismatch by user?								
    				continue;				
    			}
    		}while(stayInLoop==true);
    		System.out.println("You're out of the loop after entering " + userInput);
     
    	}//end of main
    }//end of class Test

    Not only does my if statement not work:

    Enter an integer between 0 and 10.
    13
    You're out of the loop after entering 13


    But my program simply crashes instead of continuing the do while loop:
    Enter an integer between 0 and 10.
    12.3

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:875)
    at java.util.Scanner.next(Scanner.java:1496)
    at java.util.Scanner.nextInt(Scanner.java:2128)
    at java.util.Scanner.nextInt(Scanner.java:2087)
    at sam.Test.main(Test.java:16)

    And here I thought that try catch mechanisms were supposed to correct the user, not crash the program.

  5. #5
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: My first functioning Try Catch program

    That is not the exception you are catching.

    Regards,
    Jim

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My first functioning Try Catch program

    To see what method throws an exception, look at the API doc for that class.
    Also to see what exceptions a method throws.
    http://docs.oracle.com/javase/8/docs/api/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: My first functioning Try Catch program

    Quote Originally Posted by jim829 View Post
    That is not the exception you are catching.
    I still don't understand. Is Try Catch supposed to catch errors, or correct them? What can Try Catch do to correct errors that if statements cannot?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My first functioning Try Catch program

    Read through the tutorial about exceptions: https://docs.oracle.com/javase/tutor...efinition.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to return an alert true if the server is functioning normally
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2014, 12:03 AM
  2. Connection pool and Datasource Implementation Is Not Functioning
    By akash_ju in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 03:04 PM
  3. Basic Applet Not Functioning Correctly
    By AlexAndAHalf in forum Java Applets
    Replies: 4
    Last Post: December 7th, 2011, 09:33 AM
  4. Threading not functioning
    By feonx in forum AWT / Java Swing
    Replies: 1
    Last Post: October 31st, 2011, 02:56 PM
  5. Replies: 3
    Last Post: October 19th, 2011, 07:57 AM