Results 1 to 9 of 9
- 01-21-2010, 10:23 AM #1
- 01-21-2010, 10:27 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
Then rather than using three loops with a println call for each element you need to access all three values from one loop and use printf.
- 01-21-2010, 10:30 AM #3
I don't get it sorry (I'm still beginner in Java):shy:
here is the program
Java Code:public class aster { public static void main (String [] args) { int [] arr = {13,2,9,6,1,17,4}; System.out.println("Element "); for (int i=0 ; i<arr.length;i++) { System.out.println(i); } System.out.println("Values"); for (int i=0 ; i<arr.length;i++) { System.out.println(arr[i]); } System.out.println("Graphically"); for (int i=0 ; i<arr.length;i++) { System.out.println(); for (int star=0 ; star<arr[i];star++) { System.out.print('*'); } } } }
- 01-21-2010, 11:16 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
So try to implement what I suggested (if you don't want to bother with printf then use simple println, but printf would be cleaner, see the API docs). As you can see, you currently have 3 "for (int i =" loops, you need to do all three of those actions in a single loop with a single print statement.
- 01-21-2010, 11:50 AM #5
How can I write the column name above each one
Java Code:for (int i=0 ; i<arr.length;i++) { System.out.print(i +"\t"+ arr[i]); System.out.print("\t"); for (int star=0 ; star<arr[i];star++) { System.out.print('*'); } System.out.println(); }
Last edited by o0oNorao0o; 01-21-2010 at 11:55 AM.
- 01-21-2010, 11:59 AM #6Java Code:
System.out.print ("Element"); System.out.print ("\t"+"Values"); System.out.println ("\t"+"Graphically");
- 01-21-2010, 12:10 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
That was why I suggested printf and suggested a single println as the alternative. You do know how to concatenate Strings, right? See the API docs for the printf method (and make sure to follow the link to the format string page).
Java Code:"%7d %6d %s\n"
Last edited by masijade; 01-21-2010 at 12:16 PM.
- 01-21-2010, 01:11 PM #8
Where should I put this code "%7d %6d %s\n"?
yeah I know how to join multiple Strings
- 01-21-2010, 01:24 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 14
Similar Threads
-
NaN output?
By bigj in forum New To JavaReplies: 2Last Post: 01-06-2010, 10:32 AM -
what is this output?
By bigj in forum New To JavaReplies: 5Last Post: 01-04-2010, 06:07 PM -
How to duplicat drawOval horizontally?
By ntagrafix in forum AWT / SwingReplies: 1Last Post: 12-03-2009, 10:37 PM -
Can anyone tell me how this output?
By vysh in forum Advanced JavaReplies: 16Last Post: 05-10-2009, 09:32 PM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-26-2009, 12:44 AM
Bookmarks