Copy the code of this Java file: ExceptionSample.javaUsing JCreator, edit ExceptionSample so it can catch exceptions wherein text is entered as String input instead of integer data type input.Submit a copy of your completed Java program to the instructor before the start of the next class.
Question
Copy the code of this Java file: ExceptionSample.javaUsing JCreator, edit ExceptionSample so it can catch exceptions wherein text is entered as String input instead of integer data type input.Submit a copy of your completed Java program to the instructor before the start of the next class.
Solution
Here's how you can modify the Java file to catch exceptions when a string is entered instead of an integer:
-
Open the
ExceptionSample.javafile in JCreator or any other Java IDE you are using. -
You need to use a try-catch block to catch the
NumberFormatExceptionwhich occurs when you try to parse a string that is not a number. Here's a simple example of how you can do this:
import java.util.Scanner;
public class ExceptionSample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an integer:");
try {
int number = Integer.parseInt(scanner.nextLine());
System.out.println("You entered: " + number);
} catch (NumberFormatException e) {
System.out.println("That's not a valid integer!");
}
}
}
In this code, Integer.parseInt(scanner.nextLine()) will throw a NumberFormatException if the input is not a valid integer. The catch block catches this exception and prints a message to the user.
-
Save your changes and run the program to make sure it works as expected.
-
Once you've confirmed that the program works correctly, you can submit it to your instructor. You can do this by emailing the .java file as an attachment, or by printing out the code and handing it in physically, depending on your instructor's preference.
Similar Questions
Create a Java program based on what you have learned so far. Your output MUST HAVE at least two (2) of the following concepts:Objects, Classes, MethodsInheritanceException HandlingJava File Input/Output
Assume that you are in the process of storing the student details of a University in a file. Code a Java application to handle the following members,Student Register Number - StringStudent Name - StringAge – IntegerGetstudents method to write details on the filePrintstudents method to display the details of the studentsIf the student age is less than 17, display a message that “Invalid Age”. Handle this exception in a proper way using one of the exception keyword.Consider storing three student details and display them.
e a Java program for the above with proper exception handling.Input-Output Form 1:
exception handling in java
write a java program Create a class called “Student” with the following details: Name (should contain only alphabets –No numbers and special characters) Register Number in the format (two numbers three characters four numbers) Age (should be greater than 18 and less than 21) Phone number (10 numbers). Write appropriate Exception handling block (separate block for each type of user defined exception) to handle invalid data entry for all data items. If the valid data is entered construct the email id and password of the student and display Email ID format: first name four-digit year @ vitstudent.ac.in and password is “uppercase of first four characters of name + last four numbers of register number+@” Input Format Enter Name: Enter Reg. Number: Enter Age: Enter Phone Number: Output Format Aditya 19BCE1234 20 1234567890
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.