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 8 of 8

Thread: Nested loop if statement help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Nested loop if statement help

    Right now my code is generating half of what it is supposed to.

    Program asks the user to enter a number from 1 to 10.

    Enter 7

    output will be $$$$$$$

    I'm having issues with my if statement. Once you put in the wrong number it should ask for another number and so on.

    If statement:
    if( num < 1 || num > 10)
    {
         System.out.print("The number you entered is invalid");
    }

    public static void main(String [] args)
        {
     
            Scanner keyboard = new Scanner(System.in);
     
            int num;
            int num_of_rows = 1;
     
            System.out.print("Enter a number from 1 to 10: ");
            num = keyboard.nextInt();
     
            for (int row = 1; row <= num_of_rows; row++)
            {
                for ( int column = 1; column <= num; column++)
                {
                    System.out.print("$");               
                }     
            }  
            System.out.println();
            System.exit(0);
        }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Nested loop if statement help

    So you want to ask for different input *while* their input is invalid, right?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Nested loop if statement help

    Yes, that is correct.

    So, a while loop would be the thing to use?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Nested loop if statement help

    Quote Originally Posted by CSUTD View Post
    Yes, that is correct.

    So, a while loop would be the thing to use?
    What happened when you tried it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Nested loop if statement help

    Not working, I may have done it wrong.

    I enter 2 it prints out "$$" and the "The number you entered is invalid". Even if I enter 20 it says the same thing. But it doesn't ask for the user to enter another number.

    My while loop
    while(num < 1 || num > 10)
    {
        // nested loop in here
        System.out.println("\nThe number you entered is invalid");
    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Nested loop if statement help

    Without an SSCCE I can only guess at what your code is doing. But in that while loop, I don't see a place where you're asking for user input.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Location
    Texas
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Nested loop if statement help

    Quote Originally Posted by KevinWorkman View Post
    Without an SSCCE I can only guess at what your code is doing. But in that while loop, I don't see a place where you're asking for user input.
    public static void main(String [] args)
        {
            Scanner keyboard = new Scanner(System.in);
     
            int num = 0;
            int num_of_rows = 1;
     
            while(num < 1 || num > 10)
            {
                System.out.print("Enter a number from 1 to 10: ");
                num = keyboard.nextInt();
     
                for (int row = 1; row <= num_of_rows; row++)
                {
                    for ( int column = 1; column <= num; column++)
                    {
                        System.out.print("$");               
                    } 
                }
               System.out.println("\nThe number you entered is invalid");
            }
     
            System.out.println();
            System.exit(0);
        }

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Nested loop if statement help

    I'm really not sure what you expect the program to do- look through it line by line, and it should become pretty clear what's going on.

    You're asking for a number, and regardless of its value you're printing out "$" a certain number of times. Then, regardless of its value, you're telling them the number is invalid. Then, if the number is indeed valid, you're repeating the process.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help simplifying huge nested if statement?
    By racecar333 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 1st, 2011, 01:03 AM
  2. nested loop structure
    By captianjaax in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 21st, 2011, 04:00 PM
  3. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM
  4. Nested If Else Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 6th, 2010, 02:46 PM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM