473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Nested loop structure

5
hi
i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

55551
55512
55123
51234
12345


I need a code to make above mentioned , in nested loop form.

thanks and regards
Oct 31 '07 #1
8 2849
JosAH
11,448 Expert 8TB
hi
i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

55551
55512
55123
51234
12345


I need a code to make above mentioned , in nested loop form.

thanks and regards
Try to figure out the regularity: Java counts starting at zero, so there are lines
0, 1, 2, 3 and 4. On line i (i in [0,4]) there are i+1 '5' characters followed by the
digits i+1, ... i+2 etc. until 5 characters are printed.

That observation leeds to a nested loop: the outer loop simply counts from 0 to 4.
The inner loop also counts from 0 to 4 and uses an if statement to decide whether
or not to print 5s or simply increasing digits 1, 2, 3 etc. Of course there are many
ways to solve this little puzzle.

kind regards,

Jos
Oct 31 '07 #2
ajos
283 100+
hi
i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

55551
55512
55123
51234
12345


I need a code to make above mentioned , in nested loop form.

thanks and regards
i'll give you an example, i hope my fellow TSDN's members wont flame at me--->
Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.util.*" %>
  2. <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
  3. <html>
  4. <head><title></title></head>
  5. <body>
  6. <%
  7. ArrayList arrList = new ArrayList() ;
  8. ArrayList subList1 = new ArrayList() ;
  9. ArrayList subList2 = new ArrayList() ;
  10. ArrayList subList3 = new ArrayList() ;
  11.  
  12. subList1.add( "subList1_1" ) ;
  13. subList1.add( "subList1_2" ) ;
  14. subList1.add( "subList1_3" ) ;
  15. subList1.add( "subList1_4" ) ;
  16.  
  17. subList2.add( "subList2_1" ) ;
  18. subList2.add( "subList2_2" ) ;
  19. subList2.add( "subList2_3" ) ;
  20. subList2.add( "subList2_4" ) ;
  21.  
  22. subList3.add( "subList3_1" ) ;
  23. subList3.add( "subList3_2" ) ;
  24. subList3.add( "subList3_3" ) ;
  25. subList3.add( "subList3_4" ) ;
  26.  
  27. arrList.add( subList1 ) ;
  28. arrList.add( subList2 ) ;
  29. arrList.add( subList3 ) ;
  30.  
  31. request.setAttribute("myList", arrList );
  32. %>
  33.  
  34. <c:forEach var='mainItem' items='${requestScope.myList}' varStatus='status'>
  35.       Main ArrayList Count: <c:out value='${status.count}'/> <br />
  36.       <c:forEach var='subItem' items='${mainItem}'>
  37.             <font color="red">Names: <c:out value='${subItem}'/></font>
  38.             <br />
  39.       </c:forEach>
  40.  
  41. </c:forEach>
  42. </body>
  43. </html>
  44.  
Oct 31 '07 #3
ajos
283 100+
i'll give you an example, i hope my fellow TSDN's members wont flame at me--->
Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.util.*" %>
  2. <%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
  3. <html>
  4. <head><title></title></head>
  5. <body>
  6. <%
  7. ArrayList arrList = new ArrayList() ;
  8. ArrayList subList1 = new ArrayList() ;
  9. ArrayList subList2 = new ArrayList() ;
  10. ArrayList subList3 = new ArrayList() ;
  11.  
  12. subList1.add( "subList1_1" ) ;
  13. subList1.add( "subList1_2" ) ;
  14. subList1.add( "subList1_3" ) ;
  15. subList1.add( "subList1_4" ) ;
  16.  
  17. subList2.add( "subList2_1" ) ;
  18. subList2.add( "subList2_2" ) ;
  19. subList2.add( "subList2_3" ) ;
  20. subList2.add( "subList2_4" ) ;
  21.  
  22. subList3.add( "subList3_1" ) ;
  23. subList3.add( "subList3_2" ) ;
  24. subList3.add( "subList3_3" ) ;
  25. subList3.add( "subList3_4" ) ;
  26.  
  27. arrList.add( subList1 ) ;
  28. arrList.add( subList2 ) ;
  29. arrList.add( subList3 ) ;
  30.  
  31. request.setAttribute("myList", arrList );
  32. %>
  33.  
  34. <c:forEach var='mainItem' items='${requestScope.myList}' varStatus='status'>
  35.       Main ArrayList Count: <c:out value='${status.count}'/> <br />
  36.       <c:forEach var='subItem' items='${mainItem}'>
  37.             <font color="red">Names: <c:out value='${subItem}'/></font>
  38.             <br />
  39.       </c:forEach>
  40.  
  41. </c:forEach>
  42. </body>
  43. </html>
  44.  
try to manipulate with this example.
Oct 31 '07 #4
X106
5
I understand the concept, jos,but I can't write in code, I just wrike like that

class NestedLoop {
public static void main (String [] a) {
int n=5, i, j;
for (i = 1; i <=n; i++) {

Then I don't know how to write.Can help me how to write in code?
Oct 31 '07 #5
r035198x
13,262 8TB
I understand the concept, jos,but I can't write in code, I just wrike like that

class NestedLoop {
public static void main (String [] a) {
int n=5, i, j;
for (i = 1; i <=n; i++) {

Then I don't know how to write.Can help me how to write in code?
Write the loops that Jos was explaining above.
Oct 31 '07 #6
JosAH
11,448 Expert 8TB
Write the loops that Jos was explaining above.
... or go fancy and write a single loop:

Expand|Select|Wrap|Line Numbers
  1. private static void square(int n) { // assume 1 <= n <= 9
  2.    char[] line= new char[n];
  3.    Arrays.fill(line, (char)('0'+n));
  4.    String s= new String(line);
  5.    for (int i= 1; i <= n; i++)
  6.       System.out.println(s= (s+i).substring(1));
  7. }
  8.  
You can't turn this in because there's no nested loop :-P

kind regards,

Jos
Oct 31 '07 #7
r035198x
13,262 8TB
.....

You can't turn this in because there's no nested loop :-P

kind regards,

Jos
Yep, but keep the ideas in mind for the time when you are no longer writing code for school only.
Oct 31 '07 #8
X106
5
Thank you ,you all help me a lot!!!!!!!!!!
Oct 31 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: kbass | last post by:
I am new to Python and I am attempting to retrieve data from a database and I would like to place this data into a nested dictionary. After placing the data into a dictionary, I would like to loop...
25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
4
by: so.intech | last post by:
for example, ret = 0; for(i=0; i<3; i ++;) { for(j=0; j<4; j++;) { for(k=0; k<3; k++;) { for(m=0; m<4; m++;) {
25
by: GY2 | last post by:
I writing some documentation and I want to describe a common code structure which is used to step through all the items in a collection (e.g. each file in a subdirectory) while applying more and...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
8
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
25
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.