如何对齐输出?

时间:2015-10-01 02:46:07

标签: java arff

有人可以帮我解决这个问题。我不知道如何调整它。我很感激你的帮助。你可以教我适当的编码吗...谢谢!

当前代码

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 个答案:

答案 0 :(得分: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 - Formfeed
  • \n - Newline