Results 1 to 11 of 11
  1. #1
    keycoffee is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default can some one help me with nested loop?

    Hi. I'm a beginner of java and im getting confusing about how to make an output like this.

    JAVA

    I want to make an output with this word by using nested loop.

    I'm beginner but when I asked my friend how to do it, he just tole me to use the nested loop. but I dont know how the nested loop works. can any one help me with the code for this with nested loop or other easier way to do it?
    Last edited by keycoffee; 01-24-2010 at 06:28 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The question you ask doesn't make much sense. What is the exact assignment that you have? Can you type it here word for word?

  3. #3
    Newbie666 is offline Member
    Join Date
    Jan 2010
    Posts
    46
    Rep Power
    0

    Default

    edit: nevermind..
    Last edited by Newbie666; 01-24-2010 at 07:10 AM.

  4. #4
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by keycoffee View Post
    Hi. I'm a beginner of java and im getting confusing about how to make an output like this.

    JAVA

    I want to make an output with this word by using nested loop.

    I'm beginner but when I asked my friend how to do it, he just tole me to use the nested loop. but I dont know how the nested loop works. can any one help me with the code for this with nested loop or other easier way to do it?
    if you just want to print the word java

    you dnt need a nested loop, you dont need a loop at all.

    Java Code:
    System.out.println("JAVA");
    that would work fine.

  5. #5
    ecliptical is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default

    What RaIdEn said is correct, but if you want you can also use:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    public class TestCode
    {
       public static void main (String[] args)
       {
         JOptionPane.showMessageDialog(null, "JAVA");
       }
    }
    Enjoy.

  6. #6
    keycoffee is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default thanks guys

    I'm sorry guys. I misunderstood. I'm supposed to make the output like this.
    *
    **
    ***
    ****

    I want to make this kind of output by using the word "JAVA" .

    The question actually said like this

    Write a program to display your first name on the screen by drawing the individual
    letters using the same letters on a 5 5 grid.

    This was the question what I was assigned to do. But it's my first week of my assignment and I didnt learn nothing much. I guess it's not the nested loop or the loop but just the simple code. But I really don't get how to do it cause I'm a really beginner. can some one teach me please? I appreciate it.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by keycoffee View Post
    ...But it's my first week of my assignment and I didnt learn nothing much. I guess it's not the nested loop or the loop but just the simple code. But I really don't get how to do it cause I'm a really beginner. can some one teach me please? I appreciate it.
    Given the confines of this forum and our other time commitments (we're volunteers after all), we're not so good at "teaching" here, but much better at answering direct questions such as, "I'm getting such-and-such error on line 34 with so-and-so code that I've posted below". To learn what you need to know requires study. I recommend that you start with your notes, your books, and this decent tutorial: reallybigindex

    Much luck!

  8. #8
    keycoffee is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Thank you for the tips fubarable. I try to learn from the website which you posted. I appreciate it.

  9. #9
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    I am a bit lost as to the intent of your question.
    Think you are asking for something like
    Java Code:
    J
    JA
    JAV ....
    Am writing this as psuedocode and as WHILE loop, but translates directly into FOR.
    You will have string fuctions something like substring ("JAVA", 0, 1) -> JA

    Java Code:
    set string start index = 0
    set string start index = 0
    set string length
    while(loop index < stringLength) {
        print substring ("string", start_index, end _ndex);
        increment end_index
    }
    You should know or will learn how the C/C++/Java for loop is implemented using WHILE.
    I don't see the need for a nested loop.
    Last edited by rdtindsm; 01-24-2010 at 08:35 PM.

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    This is a classic problem, though I usually see it with the **** like you demonstrated.

    A nested loop simply means a loop inside a loop. It gives you two counting variables (in the case of a for loop which this problem usually uses).

    So, you need to print out 4 lines and each line needs to print something an increasing number of times. See a pattern? line 1 prints 1 *. Line 2 prints 2 * and so on.

    So, for every line you print, you need to print the same number of *'s as the line number.

    That statement describes a nested loop - an outer loop that prints from 1 to n lines, and an inner loop that prints 1 to n *'s on that line. A combination of System.out.print("*"); and System.out.println(); inside a pair of loops will do the trick, try it out, I don't want to spoon feed it to you.

  11. #11
    keycoffee is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Explanation of Nested Loop (very strange)
    By Jonotron in forum New To Java
    Replies: 5
    Last Post: 01-09-2011, 03:54 AM
  2. JAVA: String char removal with nested loop
    By igniteflow in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 03:09 AM
  3. Replies: 4
    Last Post: 09-05-2008, 05:47 PM
  4. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-21-2008, 12:00 AM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 02:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •