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

Thread: Help with a for loop and formating

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with a for loop and formating

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with a for loop and formating

    Your thoughts are good enough to get started. Come back with some code when you need help with it.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with a for loop and formating

    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!

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with a for loop and formating

    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

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with a for loop and formating

    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.

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with a for loop and formating

    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)

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with a for loop and formating

    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:
    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;
    		}
     
    	}
    }
    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.

    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.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    Oldemor (October 20th, 2013)

  9. #8
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with a for loop and formating

    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.

Similar Threads

  1. [SOLVED] Java formating
    By maple1100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 14th, 2013, 07:42 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  5. How to format text in java?
    By fourseven in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2009, 09:42 PM

Tags for this Thread