🌙 DARK

NumberFormatException - JAVA

 

NumberFormatException
 
Write a program to handle NumberFormatException for the inputs given to create ItemType object.
 
Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names and method names should be the same as specified in the problem statement.

Consider a class ItemType with the following
 attribute,
AttributesData type
nameString
depositDouble
costPerDayDouble

The methods for appropriate getter/setter, default and parameterized constructors are given in the template code.

Override toString() and print the details as in the specified format.

Consider a Main class and test the above class. 
Handle the NumberFormatException in the Main Class and display details using toString() method.
 
The link to download the template code is given below
Code Template

Input and Output format:

Refer to sample Input and Output for formatting specifications.
All the double values should be displayed upto 1 decimal place.

[All Texts in bold corresponds to the input and rest are output]

Sample Input and Output 1:


Enter the Item type details:
Enter the name:
Electronics
Enter the deposit:
1000
Enter the cost per day:
100
The details of the item type are:
Name:Electronics
Deposit:1000.0
Cost Per Day:100.0

Sample Input and Output 2:

Enter the Item type details:
Enter the name:
Electronics
Enter the deposit:
One thousand
java.lang.NumberFormatException: For input string: "One thousand"



Problem Requirements:

Java

KeywordMin CountMax Count
catch1-
KeywordMin CountMax Count
NumberFormatException1-
KeywordMin CountMax Count
try1-



PROGRAM:-
@MRProgrammer89 


CREATE CLASS < ItemType :


public class ItemType {
    
    private String name;
private Double deposit;
private Double costPerDay;

public ItemType(String name, Double deposit, Double costPerDay) {
this.name = name;
this.deposit = deposit;
this.costPerDay = costPerDay;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getCostPerDay() {
return costPerDay;
}

public void setCostPerDay(Double costPerDay) {
this.costPerDay = costPerDay;
}

public Double getDeposit() {
return deposit;
}

public void setDeposit(Double deposit) {
this.deposit = deposit;
}
public String toString() {
        String details = "Name:"+getName()+"\n"+"Deposit:"+getDeposit()+"\n"+"Cost Per Day:"+getCostPerDay();
return details;
}

}


@MRProgrammer89 


CREATE CLASS < Main :

import java.util.Scanner;

public class Main {
public static void main(String args[]) {
       Scanner sc = new Scanner(System.in);
       try
       {
           System.out.println("Enter the Item type details:");
           System.out.println("Enter the name:");
                String name = sc.nextLine();
            System.out.println("Enter the deposit:");
            //double deposit = sc.nextDouble();
            //double deposit = Double.parseDouble(deposit1);
            double deposit =Double.parseDouble(sc.nextLine());
            System.out.println("Enter the cost per day:");
            double perday =Double.parseDouble(sc.nextLine());
            ItemType obj = new ItemType(name, deposit, perday);
                //System.out.println("Enter the Item type details:");
            System.out.println("The details of the item type are:"+"\n"+obj.toString());
       }
       catch(NumberFormatException e)
       {
           System.out.println(e);
       }
}
}







@MRProgrammer89 

NumberFormatException in Java

The NumberFormatException is thrown when we try to convert a string into a numeric value such as float or integer, but the format of the input string is not appropriate or illegal. By illegal format, it is meant that if you are trying to parse a string to an integer but the String contains a boolean value, it is of illegal format. For example - if we try to parse a string to integer but the string is null.

It is an unchecked exception. It is a subclass of IllegalArgumentException and implements the Serializable interface.



  • The input string provided might be null-
    Example- Integer.parseInt(null);
  • The input string might be empty-
    Example- Integer.parseInt("");
  • The input string might be having trailing space-
    Example- Integer.parseInt("123 ");
  • The input string might be having a leading space-
    Example- Integer.parseInt(" 123");
  • The input string may be alphanumeric-
    Example- Long.parseLong("b2");
  • The input string may have an input which might exceed the range of the datatype storing the parsed string-
    Example- Integer.parseInt("135"); The maximum possible value of integer can be 127, but the value in the string is 135 which is out of range, so this will throw the exception.
  • There may be a mismatch between the input string and the type of the method which is being used for parsing. If you provide the input string like "1.0" and you try to convert this string into an integer value, it will throw a NumberFormatException exception.
    Example- Integer.parseInt("1..0");

How to avoid NumberFormatException?

The NumberFormatException is basically caused because the input string is not well formatted or illegal while parsing into a numerical value. So, to avoid this exception, the input string provided has to be well formatted.

To have a valid and well-formatted string, first of all, check whether the input string is not null. Then, check for unnecessary spaces and trim out all of them after that put several checks to verify that argument string matches the type of the method which we are using for parsing the string. If the method is ParseInt(), check that the string is has an integer value and likewise for all other methods do the required checks.



MOTIVATION :


    “The way we spend our time defines who we are 


Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.


VISITE MORE BLOGS




THANK YOU....😊

1 Comments

Thanks for reading the blog. We hope it was useful to you and that you learned something new. Will always be writing on new and interesting topics, so you can visit our website to know the latest updates of our blogs. Thank You!

Previous Post Next Post

Contact Form