Results 1 to 6 of 6
  1. #1
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    100
    Rep Power
    0

    Default Catching multiple string parsed number exceptions

    Hi guys. I was counting on the built in NumberFormatException to be able to catch multiple numeric errors, but it doesn't seem to be doing anything:

    BuiltInExceptions.java:
    Java Code:
    package customExceptions;
    import java.util.Scanner;
    
    public class BuiltInExceptions {
    	
    	public void parseNumType() {
    		Scanner dblInput = new Scanner(System.in);
    		Scanner intInput = new Scanner(System.in);
    		try {
    			System.out.println("Enter a double:");
    			String x = dblInput.next();
    			System.out.println("Enter a integer:");
    			String y = intInput.next();
    			double num1 = Double.parseDouble(x);
    			int num2 = Integer.parseInt(y);			
    		}
    		catch(NumberFormatException nfe) {
    			System.out.println("You didn't enter a double or integer correctly.");
    		}
    	}
    }//end of BuiltInExceptions class
    SamsExperimentsMain.java:
    Java Code:
    package samsExperiments;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Deque;
    
    import SortingAlgorithms.*;
    import customExceptions.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.Queue;
    import java.util.TreeMap;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
    import java.util.Locale;
    import java.time.DayOfWeek;
    import java.time.LocalDate;//yyyy-mm-dd
    import java.time.LocalTime;//hh-mm-ss-nn
    import java.time.Month;
    import java.time.temporal.ChronoUnit;
    import java.time.LocalDateTime;//yyyy-mm-dd-hh-ss-nn
    import java.io.IOException;
    import java.time.*;
    
    public class SamsExperimentsMain {
    	
    	public static void main(String[] args){		
    		
    		BuiltInExceptions z = new BuiltInExceptions();
    		//z.dinoEggs();
    		z.parseNumType();
    The output is:
    Enter a double:
    34
    Enter a integer:
    34

    What happened? Why is it not yelling at me for not making the 34 with a decimal point? For the sake of the double "34" not being a non-integer like "34.67", why doesn't it yell, "You didn't enter a double or integer correctly." about that?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Catching multiple string parsed number exceptions

    I believe 34 is a valid double value.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    100
    Rep Power
    0

    Default Re: Catching multiple string parsed number exceptions

    I keep on hearing about creating your own exceptions all over the place, but hardly find anything about creating your own catch blocks to catch your so-called 'custom exceptions'.

    While what Norm said about 34 being a valid double in the previous post was true, it's not really a solution to my problem.
    Consider the following code:

    Java Code:
    package customExceptions;
    import java.util.Scanner;
    
    public class BuiltInExceptions {
    	
    	public void parseNumType() {
    		Scanner dblInput = new Scanner(System.in);
    		Scanner intInput = new Scanner(System.in);
    		try {
    			System.out.println("Enter a double with digits after decimal point:");
    			String x = dblInput.next();
    			System.out.println("Enter a integer:");
    			String y = intInput.next();
    			int num2 = Integer.parseInt(y);
    			
    			double num1 = Double.parseDouble(x);
    			boolean hasDigitsToRightOfDecimal = false;
    			
    			String num1ToStr = String.valueOf(num1);
    			int decimalPoint = num1ToStr.indexOf(".");
    			String leftSide = num1ToStr.substring(0,decimalPoint);
    			String rightSide = num1ToStr.substring(decimalPoint);
    			System.out.println("Left side of decimal point: " + leftSide);
    			System.out.println("Right side of decimal point: " + rightSide);
    			if(rightSide.equalsIgnoreCase(".0")) {
    				hasDigitsToRightOfDecimal = false;
    				System.out.println("hasDigitsToRightOfDecimal = false.");
    			}
    			else {
    				hasDigitsToRightOfDecimal = true;
    				System.out.println("hasDigitsToRightOfDecimal = true.");
    			}
    		}
    		catch(NumberFormatException nfe) {
    			System.out.println("You didn't enter a double or integer correctly.");
    		}
    	}
    }
    How do I create a custom catch block that will actually detect when the boolean hasDigitsToRightOfDecimal is set to false, and throw an exception for it?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Catching multiple string parsed number exceptions

    creating your own exceptions
    You can create your own exception class by extending an existing exception class. See the definition of the NumberFormatException class for an example.
    To use your new class, have code that detects the condition for the exception, create a new instance of the class with a String describing the problem and throw rhe new instance of the class.

    create a custom catch block that will actually detect
    I don't know what a "custom catch block" would be. catch blocks are where caught exceptions go when the exception is caught.
    The "detection" of the condition where the code wants to throw an exception is just normal code with if statements, etc. When a method detects an exceptional condition, it creates an instance of the exception class and uses the throw command. That passes the exception to the method's caller.
    For example when the parseInt method see non-digit characters in the String passed to it, it creates an instance of the NumberFormatException and throws it. That exception is caught by a catch block in the method that called the parseInt method.

    A regular expression might be useful for detecting the pattern of digits that you are looking for. See the API doc for the Double class's valueOf method for some ideas.
    Last edited by Norm; 07-21-2019 at 05:18 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    100
    Rep Power
    0

    Default Re: Catching multiple string parsed number exceptions

    It was going good until l tried to throw my custom exception on line 23:

    BuiltInExceptions.java
    Java Code:
    package customExceptions;
    import java.util.Scanner;
    
    public class BuiltInExceptions {
    	
    	public void parseNumType() throws NumberNotFractionalException{
    		Scanner dblInput = new Scanner(System.in);
    		Scanner intInput = new Scanner(System.in);
    		try {
    			System.out.println("Enter a double with digits after decimal point:");
    			String x = dblInput.next();
    			System.out.println("Enter a integer:");
    			String y = intInput.next();
    			int num2 = Integer.parseInt(y);
    			
    			double num1 = Double.parseDouble(x);
    			
    			String message = "";
    			NumberNotFractionalException z = new NumberNotFractionalException(message);
    			boolean hasDigitsToRightOfDecimal = z.isFractional(x, num1);
    			
    			if(hasDigitsToRightOfDecimal = false) {
    				throw new NumberNotFractionException(message);
    			}
    		}
    		catch(NumberFormatException nfe) {
    			System.out.println("You didn't enter a numeric value properly.");
    		}
    		catch(NumberNotFractionalException e) {
    			System.out.println(e.getMessage());
    		}
    	}
    }
    Line 23 error: "NumberNotFractionException cannot be resolved to a type".

    NumberNotFractionalException.java:
    Java Code:
    package customExceptions;
    
    import java.util.Scanner;
    
    public class NumberNotFractionalException extends Exception{
    	
    	String message = "You entered a whole number. You need to enter a number " +
    			   		 "with numbers to the right of the decimal point.";
    	
    	public NumberNotFractionalException(String message) {
    		super(message);
    	}
    	
    	public boolean isFractional(String x, double num1) {
    				
    		num1 = Double.parseDouble(x);
    		String num1ToStr = String.valueOf(num1);
    		int decimalPoint = num1ToStr.indexOf(".");
    		String leftSide = num1ToStr.substring(0,decimalPoint);
    		String rightSide = num1ToStr.substring(decimalPoint);
    		System.out.println("Left side of decimal point: " + leftSide);
    		System.out.println("Right side of decimal point: " + rightSide);
    		if(rightSide.equalsIgnoreCase(".0")) {
    			System.out.println("The number has nothing on the right side of the decimal point. It isn't fractional.");
    			return false;
    		}
    		else {
    			System.out.println("The number is fractional.");
    			return true;
    		}		
    	}
    }
    How do I do what this guy is doing:
    https://www.youtube.com/watch?v=Q-66i9EVR_8&t=1s
    without creating a object? Surely there is a way to use custom exception functions without creating an object?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Catching multiple string parsed number exceptions

    If the parseNumType() is declared to throw NumberNotFractionalException
    then it should NOT catch that exception. The catch for the exception should be done in the code that calls parseNumType().

    "NumberNotFractionException cannot be resolved to a type".
    Check the spelling.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Catching exceptions where there's no caller
    By xtheunknown0 in forum New To Java
    Replies: 3
    Last Post: 09-28-2013, 04:27 PM
  2. Having trouble catching exceptions
    By fatabass in forum New To Java
    Replies: 2
    Last Post: 03-17-2012, 04:02 AM
  3. Catching Exceptions (quick question)
    By Danieldcc in forum New To Java
    Replies: 3
    Last Post: 02-19-2012, 11:40 PM
  4. catching exceptions (hierarchy)
    By kailash in forum Java Servlet
    Replies: 2
    Last Post: 02-10-2011, 06:43 AM
  5. Help with Catching Exceptions
    By javaman1 in forum New To Java
    Replies: 4
    Last Post: 09-11-2010, 02:06 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
  •