txtファイルを2次元配列に変換しましたが、どのように整理するといいですか?2d配列を整理テーブルの表記に変換する
私の入力ファイルは次のようになります。
[Name], Exam1, Exam2, Exam3 John, 99, 88, 89 May, 99, 100, 100 Mary, 100, 100, 100 Peter, 60, 60, 60
現在、私は得る:
[Name] Exam1 Exam2 Exam3 John 99 88 89 May 99 100 100 Mary 100 100 100 Peter 60 60 60
私はデータは、私はそれをどのように行うことができ、より読みやすいテーブルのように見えるしたいですか?
コード:
public static void main(String[] args) throws Exception{
File file = new File("test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
int width = 0, height = 0;
String line = "";
/*Find number of row and column of the file.*/
while ((line = br.readLine()) != null)
{
if (width == 0)
{
/*Find the number of row using split method(",")*/
String[] str = line.split(",");
width = str.length;
}
height++;
}
System.out.println("Row : " + height);
System.out.println("Column : " + width);
/*Adding values to the 2D Array.*/
String[][] data = new String[height][width];
br = new BufferedReader(new FileReader(file));
for (int i = 0; i < height; i++)
{
if ((line = br.readLine()) != null)
{
for (int j = 0; j < width; j++)
{
String[] str = line.split(",");
data[i][j] = str[j];
System.out.print(data[i][j] + " ");
}
}
System.out.println("");
}
}
はどうもありがとうございました。 printfの
またはあなたが以下のようにしている、このような方法でそれを行うことができフォーマッタ
感謝。私はjavaに新しいです、どこでどのようにprintf/Formatterを使用するのですか?どうもありがとう。ここ –
全く問題 は、APIフォーマッタへのリンクではありません:printfのためとしてhttp://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html 、私はしないでくださいapiが使い方について話しすぎると思う。ここに例があります:http://www.java2s.com/Code/JavaAPI/java.lang/SystemoutprintfsssnStringstr1Stringstr2Stringstr3.htm もしあなたがJavaを使い慣れていないなら、あなたの親友はapiドキュメントとソースコードです。 :) – ligerdave