Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Something wrong with my nested loops? Please help.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry 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;

    /*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
    Last edited by MyNamesMatt; October 21st, 2012 at 09:56 AM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Something wrong with my nested loops? Please help.

    You state:
    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.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Something wrong with my nested loops? Please help.

    Quote Originally Posted by MyNamesMatt View Post
    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.

    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.

    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.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Something wrong with my nested loops? Please help.

    Quote Originally Posted by curmudgeon View Post
    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.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  7. The Following User Says Thank You to curmudgeon For This Useful Post:

    MyNamesMatt (October 23rd, 2012)

  8. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 11:43 PM
  2. Nested for loops and array
    By bryanboy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2011, 06:45 AM
  3. Using Nested Loops
    By m2msucks in forum Loops & Control Statements
    Replies: 7
    Last Post: November 5th, 2011, 07:05 PM
  4. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM
  5. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM