2014-09-21 83 views
-1

這是我所做的編碼。但產量水平對齊輸出水平對齊而不是垂直對齊

這個文本文件是這樣的:

1,2,8,4,5,6,7,7,

3,4,5,6,7,8, 

5,6,7,8,9,9, 

1,2,3,4,5,8,9,0, 

我想要的結果是這樣的:

2 8 4 5 6 7 7 

4 5 6 7 8 

6 7 8 9 9 

2 3 4 5 8 9 0 

代碼:

 Scanner scanner = new Scanner(new File("test.txt")); 

     int row = 0; 
     int col = 0; 

while (scanner.hasNextLine()) 
{ 
    String currentline = scanner.nextLine(); 

    row++; 

    String[] items = currentline.split(","); 
    int[] intitems = new int[items.length]; 

    for (int i = 1; i < items.length; i++) 
    { 
     intitems[i] = Integer.parseInt(items[i]); 
     System.out.println(intitems[i]); 
     col = i; 

    } 
    col++; 
    System.out.println("Column: " +col); 
} 

System.out.println("Row: " +row); 


    } 

我想要輸出爲垂直對齊而不是水平對齊。有沒有辦法做到這一點?由於

回答

0

你做的一切權利獲得

如果你改變

System.out.println(intitems[i]); <- by using this line, each element is written 
            in new line 

System.out.print(intitems[i] + " ");<-by using this line, each element is written in 
             one line till the for loop is over 

,所以你可以編輯該行

System.out.println("\nColumn: " + col);

System.out.pritnln(); <- in order the next set of elements be written in new line 

你對你的代碼迴路部分成爲

 for (int i = 1; i < items.length; i++) { 
      intitems[i] = Integer.parseInt(items[i]); 
      System.out.print(intitems[i] + " "); <- change print to println 
      col = i; 

     } 
     col++; 
     System.out.println(); <- get rid of the written info 
+0

感謝您的幫助。 – Jen 2014-09-21 15:42:11

+0

@你受歡迎。 – 2014-09-21 15:43:35