txt 파일을 2 차원 배열로 변환했는데, 어떻게하면 더 조직적으로 보이게 할 수 있습니까? 이 같은2 차원 어레이를 구성 테이블 모양으로 변환
내 입력 파일보기는 :
[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/Formatter를 어디서 어떻게 사용 하는가? 고마워. –
문제 없음 여기는 api Formatter에 대한 링크입니다. http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html printf는 다음과 같습니다. api가 사용법에 대해 너무 많이 생각한다고 생각합니다. 다음은 그 예입니다 : http://www.java2s.com/Code/JavaAPI/java.lang/SystemoutprintfsssnStringstr1Stringstr2Stringstr3.htm 자바를 처음 접한다면 가장 친한 친구는 API 문서와 소스 코드입니다. :) – ligerdave