Results 1 to 14 of 14
  1. #1
    singularity is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default Print to next Line

    Hi, My code should print like this:
    54321
    4321
    321
    21
    1
    But It does print like this:
    543214321321211

    My code is here:
    public class triangle {

    public static void main(String args[]) {
    for(int i =5;i>=1;i--)
    for(int j= i;j>=1; j--)
    System.out.print(j);
    System.out.print("\n");

    }
    }
    What is the issue, pls help.

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

    Default

    You're not using brackets around your for loops, meaning the print line happens at the end of the loops. Try:
    Java Code:
    public class triangle {
    
    public static void main(String args[]) {
        for(int i =5;i>=1;i--){
            for(int j= i;j>=1; j--){
                System.out.print(j);
            }
            System.out.print("\n");
        }
    }
    }

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Two things.

    1. replace System.out.print("\n"); with System.out.println();
    2. add braces around your for loops

    With point one, it is just the platform independent, and dependable, way of doing it.
    With point two, and your real problem, is that a loop (or if statement) without braces only includes the next statement as part of its execution block. That means that the second for loop includes the first print statement and the first for loop includes only the second for loop (which, of course, pulls in that first print statement). The second print statement is outside of both loops.

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

    Default

    For cases where a System.out.println(); doesn't work, you can use System.getProperty("line.separator"). For example:
    Java Code:
    String sep = System.getProperty("line.separator");
    System.out.print("Line 1"+sep+"Line 2");

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by quad64bit View Post
    For cases where a System.out.println(); doesn't work, you can use System.getProperty("line.separator"). For example:
    Java Code:
    String sep = System.getProperty("line.separator");
    System.out.print("Line 1"+sep+"Line 2");
    Useless. System.out.println does the same thing in a better way.

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

    Default

    Thanks for that.
    Quote Originally Posted by r035198x
    same thing in a better way.
    You are completely right as always. I din't realize what my life was missing, and then you helped me find it. I wasn't typing "System.out.println()" 700 times for large text blocks. Thanks!
    Java Code:
    System.out.println("Lorem ipsum dolor sit amet, consectetur adipiscing");
            System.out.println("elit. Fusce congue, ipsum vitae mattis ullamcorper,");
            System.out.println("ipsum metus tincidunt felis, in vulputate nisi diam");
            System.out.println("quis orci. Sed dolor neque, pulvinar ac dapibus et,");
            System.out.println("sollicitudin sodales metus. Integer at orci at risus");
            System.out.println("blandit ultricies vel ac risus. In nec nisi nunc,");
            System.out.println("feugiat aliquet sapien. Nullam pulvinar suscipit porta.");
            System.out.println("Proin at nibh tellus, non feugiat erat. Nam augue velit,");
            System.out.println("vestibulum eu luctus et, consectetur et odio. Nam interdum");
            System.out.println("mauris at tortor fermentum sed pellentesque augue ultricies.");
            System.out.println("Nunc ac dapibus enim. Nulla facilisi. Donec non mi nunc.");
    Is WAY better than
    Java Code:
    String s = System.getProperty("line.separator");
            System.out.println("Lorem ipsum dolor sit amet, consectetur adipiscing elit."+s+
                    "Fusce congue, ipsum vitae mattis ullamcorper, ipsum metus tincidunt felis,"+s+
                    "in vulputate nisi diam quis orci. Sed dolor neque, pulvinar ac dapibus et,"+s+
                    "sollicitudin sodales metus. Integer at orci at risus blandit ultricies vel ac"+s+
                    "risus. In nec nisi nunc, feugiat aliquet sapien. Nullam pulvinar suscipit"+s+
                    "porta. Proin at nibh tellus, non feugiat erat. Nam augue velit, vestibulum"+s+
                    "eu luctus et, consectetur et odio. Nam interdum mauris at tortor fermentum"+s+
                    "sed pellentesque augue ultricies. Nunc ac dapibus enim. Nulla facilisi. "+s+
                    "Donec non mi nunc.");
    because r035198x said it's useless. And you're right! Since we're all paid by the hour, I should find a way to make each statement even longer.

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

    Default

    Oh yeah, and since System.out.println() transfers so well to file output as well, it should be perfect and way better. Oh way, no, it isn't and doesn't work for file output, because then you DO need the platform specific line separator. I guess thats one more thing I can spend my time doing, search and replace on 700 System.out.println()'s instead of just 1.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You're right, it's not useless in all cases. For this one, however, it is, IMHO.

    Personally, in the OPs current assignment, it is easier to simply use print to print the number, then a simple println (not print with a line ending sequence) to print the line breaks then it would be to cobble together a string with string concatenation (using line ending sequnces). Again, that is, possibly, as much personal preference as it is performance or ease of coding related, but that's the way I see it.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by quad64bit View Post
    Oh yeah, and since System.out.println() transfers so well to file output as well, it should be perfect and way better. Oh way, no, it isn't and doesn't work for file output, because then you DO need the platform specific line separator. I guess thats one more thing I can spend my time doing, search and replace on 700 System.out.println()'s instead of just 1.
    P.S. use PrintWriter(File) rather than FileWriter and you can use println. And, since both forms apply only to text it is completely applicable.

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

    Default

    Thank you masijade. I don't disagree at all, its just that r035198x has a wonderful way of saying you're (not you) an idiot and that your simple tidbit of information is a waste of the worlds time. I was only saying that just because he does not care to do something a certain way does not make it useless.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Quote Originally Posted by quad64bit View Post
    Thanks for that.
    Java Code:
    String s = System.getProperty("line.separator");
            System.out.println("Lorem ipsum dolor sit amet, consectetur adipiscing elit."+s+
                    "Fusce congue, ipsum vitae mattis ullamcorper, ipsum metus tincidunt felis,"+s+
                    "in vulputate nisi diam quis orci. Sed dolor neque, pulvinar ac dapibus et,"+s+
                    "sollicitudin sodales metus. Integer at orci at risus blandit ultricies vel ac"+s+
                    "risus. In nec nisi nunc, feugiat aliquet sapien. Nullam pulvinar suscipit"+s+
                    "porta. Proin at nibh tellus, non feugiat erat. Nam augue velit, vestibulum"+s+
                    "eu luctus et, consectetur et odio. Nam interdum mauris at tortor fermentum"+s+
                    "sed pellentesque augue ultricies. Nunc ac dapibus enim. Nulla facilisi. "+s+
                    "Donec non mi nunc.");
    You should use a StringBuilder...

    (Tolls runs off)

    :D

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by quad64bit View Post
    Thank you masijade. I don't disagree at all, its just that r035198x has a wonderful way of saying you're (not you) an idiot and that your simple tidbit of information is a waste of the worlds time. I was only saying that just because he does not care to do something a certain way does not make it useless.
    Your post was useless to the OP's problem and that is what I stated.
    If you think that I meant that you are an idiot then you are mistaken.

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    ...

    (Tolls runs off)

    :D
    You might want to run faster because, from what I've gathered, quad64bit doesn't like getting corrected.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Quote Originally Posted by r035198x View Post
    You might want to run faster because, from what I've gathered, quad64bit doesn't like getting corrected.
    I'm...*puff*...going as...*pant*...fast as I...*gasp*...can...

Similar Threads

  1. Replies: 9
    Last Post: 04-07-2009, 03:51 AM
  2. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  3. print line
    By kazitula in forum Java Applets
    Replies: 2
    Last Post: 01-26-2008, 02:05 PM
  4. how to print output on same line in 'while loop'?
    By acidblue in forum New To Java
    Replies: 5
    Last Post: 12-13-2007, 02:30 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Tags for this Thread

Posting Permissions

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