存储一个数组以填充它并在单行中打印它
Store an array to populate it and print it in single line
有人可以帮助我吗?我想用一个数组填充一个数组,但我不知道该怎么做,因为我是这种 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
//命令输出:
weather.symbolic
outlook
temperature
humidity
windy
play
//我喜欢的输出:
weather.symbolic
outlook temperature humidity windy play
Instead of this
System.out.println() ;
use System.out.print();
下一行(@attribute humidity {high, normal}):
String[] SplitData = CurrentLine.split(" ");
会将其分解成一个字符串数组,如下所示:
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 tempData[] = SplitData[1]; // ERROR!
因为那样的话你会有不匹配的类型。
所以您要做的是在一行中打印属性。
只需使用 System.out.print()
而不是 System.out.println()
。
这里,使用条件 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();
}
}
}
}
有人可以帮助我吗?我想用一个数组填充一个数组,但我不知道该怎么做,因为我是这种 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
//命令输出:
weather.symbolic
outlook
temperature
humidity
windy
play
//我喜欢的输出:
weather.symbolic
outlook temperature humidity windy play
Instead of this
System.out.println() ;
use System.out.print();
下一行(@attribute humidity {high, normal}):
String[] SplitData = CurrentLine.split(" ");
会将其分解成一个字符串数组,如下所示:
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 tempData[] = SplitData[1]; // ERROR!
因为那样的话你会有不匹配的类型。
所以您要做的是在一行中打印属性。
只需使用 System.out.print()
而不是 System.out.println()
。
这里,使用条件 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();
}
}
}
}