Something wrong with my nested loops? Please help.
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;
Code :
/*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
Re: Something wrong with my nested loops? Please help.
You state:
Quote:
Something wrong with my nested loops?
This confuses me as I see no nested loops in your code, just a single while loop.
Regarding your problem, consider using a boolean variable, inputBad, setting it to true, and looping while it remains true. Then inside of your while loop, set this variable to true if the user's input satisfies all your criteria.
Re: Something wrong with my nested loops? Please help.
Sorry, I was trying to nest them, but when I did that the same problem occurred so I took the brackets out to simplify it for myself.
In the assignment there is a hint;
"This can be done using six nested if statements, each with an else clause."
I have six if statements, each with an else clause... but when I tried to nest them, it didn't work?
I'm pretty new at programming, but won't my while loop keep running until there is no input when prompted for "hh:mm:ss" ?
Just a little confused and frustrated.
Re: Something wrong with my nested loops? Please help.
Quote:
Originally Posted by
MyNamesMatt
Sorry, I was trying to nest them, but when I did that the same problem occurred so I took the brackets out to simplify it for myself.
In the assignment there is a hint;
"This can be done using six nested if statements, each with an else clause."
I think I see what is confusing me: if statements are not loops. Loops use while or for statements, and that's a big difference.
Quote:
I have six if statements, each with an else clause... but when I tried to nest them, it didn't work?
You'll want to tell us more because "it didn't work" doesn't tell us the important details of how it doesn't work, about what behavior your program currently is doing that it shouldn't and what it isn't doing that it should.
Quote:
I'm pretty new at programming, but won't my while loop keep running until there is no input when prompted for "hh:mm:ss" ?
Just a little confused and frustrated.
it will keep looping while input does not equal the empty string (incidentally, there's a String method input.isEmpty() that already does this), but that's not what you want. If at the loop termination your input is empty, you'll have no String to test.
Re: Something wrong with my nested loops? Please help.
Quote:
Originally Posted by
curmudgeon
I think I see what is confusing me: if statements are not loops. Loops use while or for statements, and that's a big difference.
You'll want to tell us more because "it didn't work" doesn't tell us the important details of how it doesn't work, about what behavior your program currently is doing that it shouldn't and what it isn't doing that it should.
it will keep looping while input does not equal the empty string (incidentally, there's a String method input.isEmpty() that already does this), but that's not what you want. If at the loop termination your input is empty, you'll have no String to test.
Oh, I guess I didn't say why it wasn't working. LOL my apoligies.
The problem is, when it checks if it is the input is an INT or a String, if it is an Int, it works fine.
If the input is NOT an int, such as a "xx:10:10", it will prompt for re-entry, but then it will also state the other else statements for minutes and seconds...
That's what I'm confused about...
Am I to use several for loops within the while loop? Or am I to use a while loop within the while loop?
Sorry if I'm so noob and frustrating, This is only my first month of programming.
Thanks again,
Matt.
Re: Something wrong with my nested loops? Please help.
I think that your current problem would be solved if you did as the instructions suggest -- if you nested if blocks. Don't nest any loops as it doesn't tell you to do that, nor do I suggest it. If your attempt at nesting if/else statements fail, then please show us this attempt and describe any errors or mis-behaviors.
Re: Something wrong with my nested loops? Please help.
Thanks curmudgeon, I nested the if blocks, (PROPERLY THIS TIME AROUND >.< ) and it ran smoothly.
Thanks for your input I guess I didn't do it properly the first time, but persistence pays off.
Cheers,
Matt.