我正在使用 CSV 文件中的数据制作购物清单。我在输出格式方面遇到了障碍。我对java不是很熟悉,因为我是编码新手。我想知道是否有人可以告诉我如何将我的标题与下面的信息对齐。现在它的输出是这样的:
Item Category Amount Price Location
Apple Food 12 1 Walmart
Grapes Food 15 0.5 Walmart
我希望它像这样输出:
Item | Category | Amount | Price | Location
Apple | Food | 12 | 1 | Walmart
Grapes| Food | 15 | 0.5 | Walmart
到目前为止,我的代码如下所示:
public static void main(String[] args) {
ArrayList <Caring> csvstuff = new ArrayList <Caring> ();
String fileName = "Project2.csv";
File file = new File(fileName);
try {
Scanner inputStream = new Scanner(file);
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
String Item = values[0];
String Category = values[1];
String Amount = values[2];
String Price = values[3];
String Location = values[4];
System.out.println(values[0] + " " + values[1] + " " + values[2] + " " + values[3] + " " + values[4]);
caring _caring = new caring(Item, Category, Amount, Price, Location);
csvstuff.add(_caring);
}
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}