Hi there,
I'm new here, and i'm just starting computer science in University. I did well on my first assignment without any help but now I'm getting pretty stumped. The program is supposed to ask the user for a time, entered as "hours:minutes:seconds". It is then supposed to check to see if hours is an int, minutes is an int, and seconds is an int. If they're not ints, it sends an error message and restarts the loop.
The hours also can't be large than 23, minutes larger than 59 and seconds larger than 59. It checks everything, but when it checks if the input is an int, it prompts all if statements after. I've tried using brackets to nest it but I haven't gotten it to work.
Also, at the end of the input, it is supposed to calculate how many seconds since midnight the input prompted is. That's not very important, but to do that I would have to calculate it within the loop because input outside of the loop is no longer stored, correct?
Thanks guys, here is my code thus far;
/*In this assignment you will practice using: • If statements • Loops • The Scanner class • The JOptionPane class For each program in this assignment, the last three statements in the method named main must be: System.out.println("\nProgrammed by Stew Dent") ; System.out.println("Date: " + new Date() ) ; System.out.println("** End of processing. **") ; This is referred to as the termination message. Replace Stew Dent with your real name. To use the date method you must import java.util.Date. */ import java.util.Date; import javax.swing.*; import java.util.*; public class onARollHopefully { public static void main (String [] args) { // initialize variables String input = "a"; String empty = ""; String prompt1 = "Please enter the time as hours:minutes:seconds"; Scanner keyboard; int timeEntered; int hours = 0; int minutes = 0; int seconds = 0; int maxHours = 23; int maxMinutes = 59; int maxSeconds = 59; // input // input = JOptionPane.showInputDialog(null, "Please enter the time as hours:minutes:seconds."); // timeEntered = Integer.parseInt(input); while (!input.equals(empty)) { input = JOptionPane.showInputDialog(prompt1); keyboard = new Scanner(input); keyboard.useDelimiter(":"); { if (keyboard.hasNextInt() == true) {hours = keyboard.nextInt(); } else { JOptionPane.showInputDialog("Hours isn't valid, re enter"); } if (keyboard.hasNextInt() == true) {minutes = keyboard.nextInt();} else {JOptionPane.showInputDialog("Minutes not valid, re enter"); } if (keyboard.hasNextInt() == true) {seconds = keyboard.nextInt(); } else {JOptionPane.showInputDialog("Seconds not valid, re enter"); } if (hours > maxHours) { JOptionPane.showInputDialog("Hours is larger than 23, please re enter"); } else { } if (minutes > maxMinutes) { JOptionPane.showInputDialog("Minutes is larger than 59, please re enter"); } else { } if (seconds > maxSeconds) { JOptionPane.showInputDialog("Seconds is larger than 59, please re enter"); } else { } } } }
Cheers,
Matt