Raiana Salmin Zaman
Raiana Salmin Zaman

Reputation: 75

How to print each letter vertically

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] );
}

enter image description here

Upvotes: 1

Views: 520

Answers (2)

vikas kumar
vikas kumar

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]);
    }
}

Output: enter image description here

Upvotes: 0

Ratish Bansal
Ratish Bansal

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

Related Questions