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

Thread: nested loop structure

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default nested loop structure

    Hello guys,

    I am trying to make a code using a nested loop structure. How would I input a number and the output would be one line of asterisk?
    I am trying to get my code to have an output like this:

    enter int: 1
    *
    enter int: 2
    **
    enter int: 3
    ***
    ect.

    This is the code I have so far, but after you enter a int, it just counts the total lines I have specified.

    import java.util.Scanner;
     
    public class Loop1 {
     
    public static void main(String[] args) {
     
    Scanner input = new Scanner( System.in );
     
    String line;
     
    System.out.println("Please enter the total number of lines to output, as any integer greater than 0 and less than 71:");
    line = input.next();
     
    for(int x=0; x<71; x++){
     
    for(int y=0; y<=x; y++){
     
    System.out.print("*");
    }
    System.out.println("");
    }
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: nested loop structure

    I am confused as to what the requirements are. Perhaps I'm misunderstanding, but you seem to have a nested loop to print a single line.

  3. The Following User Says Thank You to copeg For This Useful Post:

    captianjaax (September 21st, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: nested loop structure

    What I am trying to do it print out a line based upon the number the user inputs. When a user inputs a number, it is supposed to print off that manylines of asterisks. An example of the output I am trying to get is this:

    enter int: 1
    *
    enter int: 5
    *
    **
    ***
    ****
    *****

  5. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: nested loop structure

    package test;

    import java.util.Scanner;

    public class Test {

    public static void main(String[] args) {

    Scanner input = new Scanner( System.in );

    String line;

    System.out.println("Please enter the total number of lines to output, as any integer greater than 0 and less than 71:");
    line = input.next();

    for(int x=0; x<new Integer(line); x++){

    for(int y=0; y<=x; y++){

    System.out.print("*");
    }
    System.out.println("");
    }
    }
    }
    hope this will work fine as per your requirement.

  6. The Following User Says Thank You to cs13 For This Useful Post:

    captianjaax (September 21st, 2011)

  7. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: nested loop structure

    thank you copeg, you are right, i did have set as a single line

  8. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: nested loop structure

    This is awesome, thank you!

    Quote Originally Posted by cs13 View Post
    package test;

    import java.util.Scanner;

    public class Test {

    public static void main(String[] args) {

    Scanner input = new Scanner( System.in );

    String line;

    System.out.println("Please enter the total number of lines to output, as any integer greater than 0 and less than 71:");
    line = input.next();

    for(int x=0; x<new Integer(line); x++){

    for(int y=0; y<=x; y++){

    System.out.print("*");
    }
    System.out.println("");
    }
    }
    }
    hope this will work fine as per your requirement.

  9. #7
    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 structure

    cs13, please read this before you post again: http://www.javaprogrammingforums.com...n-feeding.html
    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!

  10. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: nested loop structure

    This is just my opinion but,
    To his defense, I don't think this is "spoon feeding" or whatever you wanna call it. I wrote the code, just had a line wrong and he identified it. Its not like I posted the assignment and he did it for me. He just fixed a line.

  11. #9
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: nested loop structure

    It is spoon-feeding.

    He posted a solution, which could have been done better, with no explanations on what he changed or why the code works in comparison.
    He stripped the learning / researching process away from you.

    Albeit it was a very simple problem, It's still very much regarded as spoon-feeding.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  12. The Following User Says Thank You to newbie For This Useful Post:

    KevinWorkman (September 22nd, 2011)

Similar Threads

  1. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM
  2. [SOLVED] Problems with Loop Structure
    By Sean137 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 13th, 2010, 02:59 PM
  3. The defitition of nested loop
    By silentbang in forum Java Theory & Questions
    Replies: 2
    Last Post: October 16th, 2010, 02:38 PM
  4. do while loop with nested while question
    By johneusmc in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 6th, 2010, 04:45 PM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM