In: Computer Science
Write a program that accepts a number of minutes and converts it to days and hours. For example, 6000 minutes represents 4 days and 4 hours. Be sure to provide proper exception handling for non-numeric values and for negative values.
Save the file as MinuteConversionWithExceptionHandling.java
MinuteConversionWithExceptionHandling.java
import java.util.Scanner;
import java.io.IOException;
import java.util.*;
public class Main
{
public static void main(String[] args) {
int minutes;
int days;
int hours;
int min;
Scanner input = new
Scanner(System.in);
try {
//Read the input
System.out.print("Enter the minutes
to convert:");
minutes =input.nextInt();
//Check for the negative input
if(minutes<0)
throw new Exception("The entered number is negative");
//Convert to days
days=minutes/(24*60);
//convert to hours
hours = (minutes%(24*60))/60;
//convert to minutes
min=(minutes%(24*60))%60;
System.out.println(days+"days"+" "+hours+"hours"+"
"+min+"minutes");
}
//for non-numeric input
catch(InputMismatchException e) {
System.out.println("Invalid input,Please enter integer");
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Output
Enter the minutes to convert:hfkesjf
Invalid input,Please enter integer
Output
Enter the minutes to convert:-89
The entered number is negative
Output
Enter the minutes to convert:6000
4days 4hours 0minutes