有人可以帮助我吗?我想用一个数组填充一个数组,但我不知道如何因为我是这个java语言的新手。另外,我想学习如何编程的正确方法。提前致谢
import java.lang.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ArffReader{
public static void main(String[] args){
BufferedReader reader = null;
try{
String CurrentLine;
reader = new BufferedReader(new FileReader("weather.nominal.arff"));
int counter = 0;
while((CurrentLine = reader.readLine()) != null){
String[] SplitData = CurrentLine.split(" ");
//Get the relation
if(SplitData[0].equals("@relation")){
System.out.println(SplitData[1]);
}
System.out.println();
//Get the attribute
if(SplitData[0].equals("@attribute")){
System.out.println(SplitData[1]);
}
//}
counter++;
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(reader != null)reader.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
}
// 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
//来自cmd的输出:
weather.symbolic
outlook
temperature
humidity
windy
play
//我更喜欢的输出:
weather.symbolic
outlook temperature humidity windy play
答案 0 :(得分:1)
Instead of this
System.out.println() ;
use System.out.print();
答案 1 :(得分:1)
以下行(@attribute humidity {high,normal}):
String[] SplitData = CurrentLine.split(" ");
会把它分解成一个String数组,如下所示:
SplitData[0] = "@attribute"
SplitData[1] = "humidity"
SplitData[2] = "{high,"
SplitData[3] = "normal}"
因此,如果您想打印湿度,只需打印它:
System.out.print(SplitData[1])
除非您想先将其分配给变量:
String tempData = SplitData[1];
System.out.print(tempData);
但不要将其分配给String数组。
String tempData[] = SplitData[1]; // ERROR!
因为那样你会有不匹配的类型。
答案 2 :(得分:1)
所以你要做的就是在一行中打印属性。
只需使用System.out.print()
代替System.out.println()
。
答案 3 :(得分:0)
此处,使用条件if
进行打印并使用print()
,在打印任何内容后都不提供换行符。
import java.lang.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ArffReader{
public static void main(String[] args){
BufferedReader reader = null;
try{
String CurrentLine;
reader = new BufferedReader(new FileReader("weather.nominal.arff"));
int counter = 0;
boolean check = false;
while((CurrentLine = reader.readLine()) != null){
String[] SplitData = CurrentLine.split(" ");
//Get the relation
if(SplitData[0].equals("@relation")){
System.out.println(SplitData[1]);
System.out.println();
}
//Get the attribute
if(SplitData[0].equals("@attribute")){
if (check == false){
System.out.print(SplitData[1]);
}else{
System.out.print(" "+SplitData[1]);
}
check = true;
}
counter++;
}
System.out.println();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(reader != null)reader.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
}