Hey! Im going to make a program that prints out this to the console: 123123.jpg
Anyone got any thoughts on how to do it? I think it has something to do with a for loop and some formatting of the output.. please help with some thoughts
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.
Hey! Im going to make a program that prints out this to the console: 123123.jpg
Anyone got any thoughts on how to do it? I think it has something to do with a for loop and some formatting of the output.. please help with some thoughts
Your thoughts are good enough to get started. Come back with some code when you need help with it.
Ive come up with something, its almost done:private static void printNumbersB(int n) { int teller = 1; for (int lineNumber = 1; lineNumber <= n; lineNumber++) { for (int currentField = 1; currentField <= lineNumber; currentField++) { System.out.printf ("%" + lineNumber + "d", teller); teller += 1; } // Blank line System.out.println(); } }
All i need for it now is to stop printing numbers when it has reached the N parameter, any thoughts?
--- Update ---
Nevermind! Found out if i added a if statement to the end where it breaks out of the loop if the teller is higher than N, worked!
Got some problems now, if i put 45 as a parameter, it prints out correctly like this: 123123.jpg, but if i put it to for example 95, it turns out like this: Untitled123.jpg Somehow it doesnt stop on 95 if i put the parameter higher than 45?O.o
This is excellent. You've done good work with no help, just thinking it through and coding.
It's hard to see from the little pictures what your complaint(s) is/(are). Can you describe better what's going wrong?
Also, post the full updated code (in code tags, please) so that we can see the results for ourselves. Let us know what we need to do to run the code to duplicate the problem condition you've found.
My current issues:
If i put the parameter to 56, it prints out numbers up to 55. But if i put the parameter to 57, it prints out numbers up to 66??
Heres the current code:
private static void printNumbersB(int n) { int teller = 1; for (int linjenummer = 1; linjenummer <= n; linjenummer++) { for (int currentField = 0; currentField < linjenummer; currentField++) { System.out.printf ("%" + linjenummer + "d", teller); teller += 1; } // Blank line System.out.println(); if (teller >= n) { break; } }
To run it you have to call it in the mainmethod with a parameter value (which you probably know)
There are a couple things causing your confusion. First, I've changed the indenting of your code so that it may be easier to see what's going on. Like so:
There are two loops, one inside the other, known as "nested for loops" or some people say "bifurcated loops." Your test for exiting the looping uses the outside loop's control variable, but the test isn't made until after the inside loop completes. If you study the looping, you'll see that some numbers chosen for 'n' will fall on a boundary that will end the program when you'd like, but others will require the inside loop to finish - printing the extra numbers you're complaining about - before exiting the loop.private static void printNumbersB(int n) { int teller = 1; for (int linjenummer = 1; linjenummer <= n; linjenummer++) { for (int currentField = 0; currentField < linjenummer; currentField++) { System.out.printf ("%" + linjenummer + "d", teller); teller += 1; } // Blank line System.out.println(); // changed to a return if (teller >= n) { return; } } }
In short, either move or change the test. I think you'll find moving the current test does not provide exactly what you want, because I think you want it to stop printing at those boundary points that print the entire line and no more. Changing the test to only print the last whole line that's less than the number 'n' is what I think you're after. You'll have to compute where those line boundaries < n fall and end there.
I changed the 'break' to a 'return', because it unambiguously indicates what you want to happen at that point.
Oldemor (October 20th, 2013)
Ah Thank you for your expertise. I moved the test, and it worked like i was hoping it to do. Its okay that it doesn't print the whole line, as of now! Have a great day.