2015-10-01 59 views
0

有人可以幫我解決這個問題。我不知道如何對齊。我很感謝你的幫助。你能教我適當的代碼嗎..謝謝!如何對齊輸出?

目前代碼

import java.util.*; 
import java.io.File; 
import java.io.FileNotFoundException; 

public class Arff{ 
    public static void main(String[] args) throws FileNotFoundException{ 

     File TextFile = new File("weather.nominal.arff"); 
     Scanner reader = new Scanner(TextFile); 

     while(reader.hasNextLine()){ 
      String text = reader.nextLine(); 
      String[] SplitData = text.split(" "); 

      if(SplitData[0].equals("@relation")){ 
       System.out.println(SplitData[1]); 
       System.out.println(); 
      } 
      if(SplitData[0].equals("@attribute")){ 
       System.out.print(SplitData[1]+" "); 
      } 

      if(!SplitData[0].equals("@data") && !SplitData[0].equals("@attribute") && !SplitData[0].equals("@relation")){ 
       System.out.println(SplitData[0].replace(',', ' ')); 
      } 

     } 
    } 
} 

weather.symbolic.arff

@relation weather.symbolic 
    @attribute outlook {sunny, overcast, rainy} 
    @attribute temperature {hot, mild, cool} 
    @attribute humidity {high, normal} 
    @attribute windy {TRUE, FALSE} 
    @attribute play {yes, no} 

    @data 
    sunny,hot,high,FALSE,no 
    sunny,hot,high,TRUE,no 
    overcast,hot,high,FALSE,yes 
    rainy,mild,high,FALSE,yes 
    rainy,cool,normal,FALSE,yes 
    rainy,cool,normal,TRUE,no 
    overcast,cool,normal,TRUE,yes 
    sunny,mild,high,FALSE,no 
    sunny,cool,normal,FALSE,yes 
    rainy,mild,normal,FALSE,yes 
    sunny,mild,normal,TRUE,yes 
    overcast,mild,high,TRUE,yes 
    overcast,hot,normal,FALSE,yes 
    rainy,mild,high,TRUE,no 

電流輸出:

weather.symbolic 

outlook temperature humidity windy play 
sunny hot high FALSE no 
sunny hot high TRUE no 
overcast hot high FALSE yes 
rainy mild high FALSE yes 
rainy cool normal FALSE yes 
rainy cool normal TRUE no 
overcast cool normal TRUE yes 
sunny mild high FALSE no 
sunny cool normal FALSE yes 
rainy mild normal FALSE yes 
sunny mild normal TRUE yes 
overcast mild high TRUE yes 
overcast hot normal FALSE yes 
rainy mild high TRUE no 

優選輸出:

weather.symbolic 

outlook  temperature humidity windy play 

sunny  hot   high  FALSE no 
sunny  hot   high  TRUE no 
overcast hot   high  FALSE yes 
rainy  mild  high  FALSE yes 
rainy  cool  normal  FALSE yes 
rainy  cool  normal  TRUE no 
overcast cool  normal  TRUE yes 
sunny  mild  high  FALSE no 
sunny  cool  normal  FALSE yes 
rainy  mild  normal  FALSE yes 
sunny  mild  normal  TRUE yes 
overcast mild  high  TRUE yes 
overcast hot   normal  FALSE yes 
rainy  mild  high  TRUE no 

回答

1

你應該使用特殊字符

你必須使用\t

之間那麼你應該修改如下。

 if (!SplitData[0].equals("@data") && !SplitData[0].equals("@attribute") && !SplitData[0].equals("@relation")) { 
      System.out.println(SplitData[0].replace(',', '\t')); 
     } 

另外,你應該知道下面的java這對於特殊字符。

與其他編程語言如C++,c,c#一樣,java中有幾個特殊字符。每個人和他們的使用情況如下。

  • \' - 單引號
  • \" - 雙引號
  • \\ - 反斜槓
  • \t - 標籤
  • \b - 退格
  • \r - 回車
  • \f - 換頁
  • \n - 換行
+0

我怎樣才能添加\ t? – soyan

+0

如何添加更多\ t?像這樣System.out.println(SplitData [0] .replace(',','\ t \ t')); – soyan

+0

你corect,你必須做我'\ t \ t'或更多,如果需要的話。 –