以水平对齐而不是垂直对齐方式输出



这是我所做的编码。但是输出是水平排列的

此文本文件如下所示:

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

    }

我希望输出是垂直对齐,而不是水平对齐。有办法做到吗?感谢

除了,你做的一切都很好

如果您更改

 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 

相关内容

  • 没有找到相关文章

最新更新