Hi
I want the output to be appear horizontally rather than vertically:o
I mean I want the columns as you see in the picture beside each other not under each other:o
Printable View
Hi
I want the output to be appear horizontally rather than vertically:o
I mean I want the columns as you see in the picture beside each other not under each other:o
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.
I don't get it sorry (I'm still beginner in Java):shy:
here is the program
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('*');
}
}
}
}
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.
How can I write the column name above each one
Is that what you mean ?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();
}
it appears like that :(:(Code:System.out.print ("Element");
System.out.print ("\t"+"Values");
System.out.println ("\t"+"Graphically");
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).
(assuming a single space between the "headers")Code:"%7d %6d %s\n"
Where should I put this code "%7d %6d %s\n"?
yeah I know how to join multiple Strings