RAK RAK - 1 month ago 18
Java Question

trying to print transpose string[]

So i've been trying to take a txt file which has input like this for eg -

abcddhdj
efghdd
ijkl


to get this -

j
d
hd
dd
dhl
cgk
bfj
aei


i have tried to do this using 2d char array which gave nullexception and arrayoutofbound error and didnt work mostly,then tried string array , arraylist of arraylist of char , and lastly i have been trying using arraylsit of string

here is the closest i got to my solution after lot of searching by using string[] -

public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt")); // PUT YOUR FILE LOCATION HERE
int k=0,i,j=0,x;
String line[] = new String[10] ; //SET THE APPROXIMATE NUMBER OF ROWS
while((line[k] = br.readLine()) !=null)
{System.out.println(line[k]); //print to check input - verified
k++;


}
for(x=0;x<k;x++)
{if(j<line[x].length())
{j=line[x].length()-1;} //this part not working in above loop
}
System.out.println(j); // verified but not working inside previous loop for some reason
System.out.println(k);


for(x=j-1;x>=0;x++) //without this loop,its perfect, but with it gives indexoutofbound error , doesnt run at x=j
{ for(i=0;i<k;i++)
{ System.out.print(line[i].charAt(x));
}
System.out.println();
}

}


here is one output

run:
´╗┐abcd
efgh
ijkl
4 //should have come as 3 since i did length-1
3
chl //notice the d missing , every char of first row shifted,just why
bgk //in outofbound error , it only prints d at the end, need explanation
afj
ei
BUILD SUCCESSFUL (total time: 0 seconds)


if i add a space after abcd it gives indexoutofbound and no output after k
at end i used another method which adds spaces to make all length equal
yet still the output was wrong, plus there is something wrong with this way of thinking , there should be better method

so i tried arraylist , this is giving me more problems again

trying to work this out by any method understandable.

Answer

This ought to do the trick:

The key here is that I pad all the line arrays with empty chars so that each character array is the same length as the longest line.

public static void main(String[] args)
{
    try (BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt")))
    {
        String line;
        List<List<Character>> lines = new ArrayList<>();

        int longestLine = 0;
        while((line = br.readLine()) !=null)
        {
            line = line.trim();

            if (line.length() > 0)
            {
                List<Character> currList = new ArrayList<>();
                for (char c : line.toCharArray())
                {
                    currList.add(c);
                }

                if (currList.size() > longestLine)
                {
                    longestLine = currList.size();
                }

                lines.add(currList);
            }
        }

        // pad all lists to be the same as the longest
        for (List<Character> currList : lines)
        {
            while (currList.size() < longestLine)
            {
                currList.add(Character.MIN_VALUE);
            }
        }

        // go through each list backwards
        for (int i = longestLine - 1; i >= 0; i-- )
        {
            for (List<Character> currList : lines)
            {
                System.out.print(currList.get(i));
            }
            System.out.println();
        }
    }
    catch (Throwable t)
    {
        t.printStackTrace();
    }
}

Example Input:

abcd
efgh
ijkl
g

Example Output:

dhl
cgk
bfj
aeig