I need to make a program that reads hours in this format (934:9h34) or 1835 (18h35). How can I make my program print an error if somebody writes 966 (the 2 last digits over 59? (66>59)
Given a String str
:
String str = getTheString();
String lastTwoDigits = str.length() > 2 ? str.substring(str.length() - 2) : str;
int result = 0;
try {
result = Integer.parseInt(lastTwoDigits);
} catch (NumberFormatException e) {
System.err.println("Cannot parse string!");
System.exit(1);
}
if (result > 59) {
System.err.println("Number was over 59!");
System.exit(1);
}
By the way, System.err.println()
just prints to standard error rather than standard output, and exit(1)
exits the program with a failing error code.
Hope this helps!
Since the title asks "How to evaluate the 2 last digit of a Int?", we can assume that you already have the value in an int
variable.
To examine the last 2 digits, calculate the remainder when dividing by 100, ie use the %
remainder operator:
int value = /*assigned elsewhere*/;
int lastTwoDigits = value % 100;
if (lastTwoDigits > 59) {
System.out.println("ERROR: Invalid value: " + value);
// value is invalid
}
Of course, you should probably also validate that value
is not negative.
If, however, a value of -934
is valid, and -966
is not, just eliminate the sign by calling Math.abs()
:
int lastTwoDigits = Math.abs(value) % 100;
if (lastTwoDigits > 59) {
System.out.println("ERROR: Invalid value: " + value);
// value is invalid
}
This will work. Ensures that last 2 digits are <= 59.
String[] test = { "934:9h34", "1835", "1994", "iwiwiwiw45", "18h45"
};
// match 2 digits at end of string
Pattern p = Pattern.compile("(\\d\\d)$");
for (String t : test) {
Matcher m = p.matcher(t);
if (m.find()) {
// valid integer so convert and compare
if (Integer.valueOf(m.group(1)) <= 59) {
System.out.println("Passes test: " + t);
continue;
}
}
System.out.println("Fails test: " + t);
}
Learn more about Java and regular expressions here .
This solution will parse the string first, then get the last two digits of the number through result % 100
.
private static void timeFormat(String text) {
int result = 0;
if (text.length() < 2) {
System.err.println("String was too short");
return;
}
try {
result = Integer.parseInt(text);
} catch (NumberFormatException e) {
System.err.println("Failed to parse string");
}
if (result % 100 > 59) {
System.err.println("Number was over 59");
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
timeFormat(scan.nextLine());
scan.close();
}
First convert user given input into String
String hourInString = Integer.toString(userInput);
Then check if the input is valid or not. Minimum length of the input should be at least 3.
if (hourInString.length() < 3) {
System.out.println("invalid input");
System.exit(1);
}
Then retrieve the last two digit using substring
String lastTwoDigit = hourInString.substring(hourInString.length() - 2,
hourInString.length());
Finally you can validate the number-
if (Integer.parseInt(lastTwoDigit) > 59) {
System.out.println("Error");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.