Results 1 to 14 of 14
Thread: Print to next Line
- 09-09-2009, 08:25 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
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:
What is the issue, pls help.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");
}
}
- 09-09-2009, 08:37 AM #2
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"); } } }
- 09-09-2009, 08:37 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 09-09-2009, 08:45 AM #4
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");
- 09-09-2009, 09:04 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-09-2009, 04:32 PM #6
Thanks for that.
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!
Originally Posted by r035198x
Is WAY better thanJava 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.");
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.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.");
- 09-09-2009, 04:35 PM #7
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.
- 09-09-2009, 04:39 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 09-09-2009, 04:43 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 09-09-2009, 04:43 PM #10
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.
- 09-09-2009, 05:15 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
- 09-09-2009, 07:05 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-09-2009, 07:08 PM #13
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-10-2009, 09:03 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
Reading in a line of data and splitting the line up into variables
By guru32 in forum New To JavaReplies: 9Last Post: 04-07-2009, 03:51 AM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
print line
By kazitula in forum Java AppletsReplies: 2Last Post: 01-26-2008, 02:05 PM -
how to print output on same line in 'while loop'?
By acidblue in forum New To JavaReplies: 5Last Post: 12-13-2007, 02:30 AM -
Reading in data from file line by line
By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks