I am very new to Java and need to allow a user to enter a times (HH:MM format) and a rates until you enter stop. The output is to show the max and min rates and times.
I can get the rates but have problems with the times. If I make the times a String, then I can't get the compare to work. I tried to use the .useDelimiter to make it an int so the compare would work but get an error on that as well. I am hoping someone can tell me what I did wrong.
When I run this I get the following error:
Enter Hours and Minutes (HH:MM) Pulse Was Taken: Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at MaxTest.main(MaxTest.java:18)
I can get the rates but have problems with the times. If I make the times a String, then I can't get the compare to work. I tried to use the .useDelimiter to make it an int so the compare would work but get an error on that as well. I am hoping someone can tell me what I did wrong.
Code:
import java.util.*;
public class MaxTest
{
public static void main(String[] args)
{
//declare and initalize variables
int pulseRate = 0, maxPulseRate= 0, minPulseRate=0,maxHours=0,maxMinutes=0;
int maxTime= 0, minTime=0;
Scanner input = new Scanner(System.in);
System.out.print("Enter Patient Pulse Rate OR -1 to Stop: ");
pulseRate = input.nextInt();
input.useDelimiter("[:\n]");
System.out.print("Enter Hours and Minutes (HH:MM) Pulse Was Taken: ");
int hours = input.nextInt();
int minutes = input.nextInt();
int testTime = hours + minutes;
System.out.print(testTime);
while (pulseRate != -1)
{
//get the max and min pulse rates
if (pulseRate > maxPulseRate)
{
maxPulseRate= maxRate(pulseRate,maxPulseRate);
maxTime = testTime;
System.out.print("maxtime: " + maxTime);
}
// if (pulseTime > maxTime)
// {
// maxTime = testTime;
// System.out.print("pulse>: " + maxTime);
//
// maxTime=pulseTime;
// }
// else
// {
// System.out.print("pulse <: " + maxTime);
// maxTime = maxTime;
// }
// }
else if (pulseRate < maxPulseRate)
{
minPulseRate= minRate(pulseRate,minPulseRate);
minTime =testTime;
};
System.out.print("time = " + maxTime);
System.out.print("Enter Patient Pulse Rate OR -1 to Stop: ");
pulseRate = input.nextInt();
if (pulseRate == -1)// exit the loop if -1 is entered
break;
System.out.print("Enter Hours and Minutes (HH:MM) Pulse Was Taken: ");
hours = input.nextInt();
minutes = input.nextInt();
} //end of while
System.out.print(" The max pulse Rate is: " + maxPulseRate + maxTime + "\n ");
System.out.print(" The min pulse Rate is: " + minPulseRate + minTime + "\n ");
} //end of main
//return the max pulse rate between 2 numbers
public static int maxRate(int num1, int num2)
{
int result;
System.out.print("CK MaxRate:\n " + num1 + " " + num2);
if (num1 > num2)
result = num1;
else
result= num2;
return result;
} //end of maxRate
//return the MIN pulse rate between 2 numbers
public static int minRate(int num1, int num2)
{
int result;
System.out.print("CK MINRate:\n " + num1 + " " + num2);
if (num2 == 0) //should be this the first time in
result = num1;
else if (num1 < num2)
result = num1;
else
result= num2;
return result;
} //end of minRate
} //end of class
When I run this I get the following error:
Enter Hours and Minutes (HH:MM) Pulse Was Taken: Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at MaxTest.main(MaxTest.java:18)