Reputation: 75
I am trying to print each letter vertically as a table (like the picture attached). Right now this all are printing together as a string
Vertex Distance from Source
ABCDEFGH 0
ABCDEFGH 4
void printSolution(int dist[]) {
System.out.println("Vertex \t\t Distance from Source");
char[] c = new char[]{'A','B','C','D','E','F','G','H'};
for (int i = 0; i < V; i++)
System.out.println( new String(c) +" \t\t "
+ dist[i] );
}
Upvotes: 1
Views: 520
Reputation: 11018
I'm not sure if I'm getting this wrong. But it should be simple I guess. Here is a working code giving this output:
public class Main
{
static void printSolution(int dist[]) {
System.out.println("Vertex \t\t Distance from Source");
char[] c = new char[]{'A','B','C','D','E','F','G','H'};
for (int i = 0; i < c.length; i++)
System.out.println( c[i] +" \t\t "
+ dist[i] );
System.out.println('\n');
}
public static void main(String[] args) {
System.out.println("Hello World");
printSolution(new int[8]);
}
}
Upvotes: 0
Reputation: 2452
new String(c)
actually convert the char array to String. If you want only the character you should do
System.out.println( c[i] +" \t\t "
+ dist[i] );
Upvotes: 1