• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with nested for loops

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am taking a java course for my degree but its all new to me I am having issues with part of a lab assignment (currently a few days overdue) I am stuck and not sure what to do, we are using FOR loops and nested FOR loops so its relatively simple programming (when you understand it)
Below is where I am stuck, the goal is a repeating statement that starts at 9 and counts 9 9's then goes to 8 and counts 8 8's down to 1 counting 1 1.

class loopPractice{

  public static void main(String[] args){
  for (int i = 9; i >= 0; i--) {
     for (int j = i; j <= i; j++) {
  System.out.print ("" + i);
  System.out.print ("" + j);
 // System.out.println (" " = j);
}  
}
}
}

So if anyone can nudge me in the right direction ie help me fix it till my brain catches up and starts understanding the class better   PLEASE!

Cryter
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, you must clean up your indentation. Without it you'll be banging your head against the wall trying to figure out what's going wrong.
When posting code you need to be clear about what you're asking so that others know where you're stuck. Provide examples of output.
After printing 9 9's (for example) you'll need a println() call to  terminate the current line of numbers.
Your requirements say you decrement until you get down to 1 but your code of i >= 0 will continue down to zero.
You are printing both i and j, which one has the digit 9?
Reexamine your for() parameters. What value are you starting with? What test are you making to determine if you need to enter the loop body? Are you incrementing or decrementing the value at the end of the loop?
 
Jonathan Van Putten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, in the future I will do that, I need one line that would look like this
999999999888888887777777666666555554444333221

so I may have over complicated it, again I am new to this, and I did see that I can highlight and hit the code button as it suggested but after it suggested it I could not get back and it posted it.

Thanks
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jonathan Van Putten wrote:sorry, in the future I will do that, I need one line that would look like this
999999999888888887777777666666555554444333221

So, if this is what you are shooting for, what are you actually getting?
 
Jonathan Van Putten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, in the future I will do that, I need one line that would look like this
999999999888888887777777666666555554444333221

so I may have over complicated it, again I am new to this, and I did see that I can highlight and hit the code button as it suggested but after it suggested it I could not get back and it posted it.

Thanks
 
Jonathan Van Putten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as it is right now 998877665544332211
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, you need to think and raise questions, at least thoughts out loud. We aren't certain on what king of help you are seeking or where you stuck.

So, in the meantime you got an output, it isn't correct according to your specification. Do you understand how such output happens? Any clues popping up to head what might be wrong?
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, welcome to the Ranch
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you been introduced to methods? I could imagine, that such exercise with indices can get confusing within minutes, but having the methods to do right small things would make everything simpler.
 
Jonathan Van Putten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know the logic behind it just not how to code it, basically I start at 9 as the repeating first loop, the nested loop needs to have the first loop repeat the same number of times so 9 times, after the nested loop (inner loop finishes the 9 times) then the first loop needs to decrease the number by 1 going to 8, then the nested loop needs to reference the number so it will have the first loop repeat that number of times, 2nd pass would now be 8 times, it keeps repeating the process till the first loop hits 1 and the inner loop ends there since 1 is not repeated.

I think part of my problem is I am telling it to print i then print j when I need it to print i, j number of times (and j is = to i)

So hopefully that is a better explanation of what I am trying to get it to do.
 
Jonathan Van Putten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are several parts to my lab this is the only one I am stuck on that I have left to do and this one is meant to be a nested for loop to get the task done.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could you implement what is in comments?
 
Jonathan Van Putten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that has been part of my issue in trying to search for examples it seems not many use nested for loops much, they use other ways like what you are suggesting but since that is nothing he has shown us not something we are suppose to use yet.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jonathan Van Putten wrote:I know the logic behind it just not how to code it


Usually logic behind is a direct translation of code. That is why we suggest students often to solve problem on the piece of paper first, and just later on convert it to code.

As an extra helpful approach is to decompose task into smaller tasks, this is what I showed earlier. If you can't solve that part what I wrote in comments, that means your statement about understanding the logic behing isn't correct or at least honest.

I think I'll let you to think for a while, because I think it supposed to be sufficient for an undergrad starter to solve it. You did a start (not that bad), but without enough thought.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A useful debugging approach is to "play computer". That is, with paper and pencil, write down your variable names and the values they hold. Then work through the code mentally making note of modifications to the value or the result of any comparison with the value, step by step.
 
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jonathan Van Putten wrote:I need it to print i, j number of times (and j is = to i)...

So hopefully that is a better explanation of what I am trying to get it to do.



Yes, that's a very good explanation. Not perfect, it could be even simpler, but it's still good. And now that it's out there without all that other code to distract you: write a loop which does that and nothing else. Just that loop. No other code.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch again

I shall keep quiet about loops, to avoid overloading you with information, but a different thing: why are you concatenating Strings "" + i? If you go through the API documentation and find the method used by System.out.print, you will find it can take absolutely anything you can produce as an argument. So why not simply write System.out.print(i); ?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in your first for loop you need to be adding your i int because if you don't it will never end
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your original for loop you have i--? Unless I'm understanding this wrong, you should have i++, or the loop will not function properly.
 
reply
    Bookmark Topic Watch Topic
  • New Topic