0% found this document useful (0 votes)
10 views3 pages

Assessment 4 Notes

The document contains code examples demonstrating how to handle exceptions in Java. It includes examples of NumberFormatException, InputMismatchException, and validating input is in a given range. The code gets user input, checks for exceptions, and responds appropriately if exceptions occur.

Uploaded by

Rachneet Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Assessment 4 Notes

The document contains code examples demonstrating how to handle exceptions in Java. It includes examples of NumberFormatException, InputMismatchException, and validating input is in a given range. The code gets user input, checks for exceptions, and responds appropriately if exceptions occur.

Uploaded by

Rachneet Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

package Assessment_4_NumberFormatException;

import java.lang.NumberFormatException;
import java.util.Scanner;

public class Question1 {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean a = true;
do {
System.out.println("Enter a number");
String num = s.next();

try {
int n = Integer.parseInt(num);
a=false;
break;
}
catch (NumberFormatException e) {
System.out.println("Not an integer value");
s.nextLine();
}
}
while(a==true);
}
}

package Assessment_4_NumberFormatException;

import java.util.Scanner;

public class Question2 {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean a = true;
do {
System.out.println("Enter a number in range 10-20");
String num = s.next();

try {
int n = Integer.parseInt(num);
if(n<10||n>20){
System.out.println("Number entered is out of
limits");
s.nextLine();
}
else {
a = false;
System.out.println("The entered number is "+num);
break;
}
}
catch (NumberFormatException e) {
System.out.println("Not an integer value");
s.nextLine();
}
}
while(a==true);
}
}
package Assessment_4_InputMismatchException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Exceptional_Handling_Robust_Validation {


public static int readInt(Scanner scanner) {
while (true) {
try {
System.out.print("Enter an integer: ");
return scanner.nextInt();
}
catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter an integer.");
scanner.nextLine(); // Clear the buffer
}
}
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int value = readInt(scanner);
System.out.println("You entered: " + value);
}
}

package Assessment_4_InputMismatchException;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Exceptional_Handling_Switch_Case {


public static void main(String[] args) {
boolean a=false;
do {
System.out.println("Select a number from 1 to 3");
Scanner s = new Scanner(System.in);

try {
int n = s.nextInt();
switch (n) {
case 1:
System.out.println("You have selected 1");
a=true;
break;
case 2:
System.out.println("You have selected 2");
a=true;
break;
case 3:
System.out.println("You have selected 3");
a=true;
break;

default:
System.out.println(" number not from 1 to 3");
s.nextLine();
}
} catch (InputMismatchException e) {
System.out.println("You have entered a non-integer value");
s.nextLine();
}
}while(a==false);
}
}
package Assessment_4_InputMismatchException;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Exceptional_Handling_Age {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean a= false;
do {
try {
System.out.println("Enter your age: ");
int age = s.nextInt();
if (age < 0) {
System.out.println("Age cannot be negative please enter a
valid age");
}
else {
System.out.println("The entered age is : " + age);
a = true;
break;
}
}

catch (InputMismatchException e) {
System.out.println("Enter a valid age");
s.nextLine();
}
}
while(a==false);
}
}

You might also like