这是我所做的编码。但输出是水平对齐
此文本文件如下所示:
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 :(得分: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循环部分变为
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