我试图将单词存储在java数组中由昏迷分隔
的文件中.文件是
年龄,收入,学生,信用评级,类:购买电脑
青年,高,不,公平,无
青年,高,不,优秀,无
中年,高,不,优秀,没有
高级,中等,不,公平,是
高级,低,是的,公平的,是的
高级,低,是的,优秀的,没有
public class Test {
public static void main(String args[]) throws FileNotFoundException, IOException{
FileInputStream f=new FileInputStream("F:\\pr\\src\\dmexam\\inp2.txt");
int size,nr=7,nc=5,j=0,i=0;
char ch;
String table[][]=new String[nr][nc];
size=f.available();
table[0][0]=new String();
while(size--!=0){
ch=(char)f.read();
if(ch=='\n')
{
i++;
if(i>=nr)
break;
table[i][0]=new String();
j=0;
continue;
}
if(ch==',')
{
j++;
table[i][j]=new String();
continue;
}
table[i][j]+=ch;
}
f.close();
System.out.println("The given table is:::---");
for(i=0;i<nr;i++){
for(j=0;j<nc;j++){
System.out.print(" "+table[i][j]);
System.out.print(" ");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但输出是
给定的表是::: ---
但如果for改变了这样的话
System.out.println("The given table is:::---");
for(i=0;i<nr;i++){
for(j=0;j<nc-1;j++){ …Run Code Online (Sandbox Code Playgroud)