import java.io.BufferedReader;
import java.io.FileReader;
public class str_test 
{
class test
{
double x;
double y;
test()
{
x=0;y=0;
}
}
public static void main(String args[])
{
test t[]=new test[3];
try
{
BufferedReader reader=new BufferedReader(new FileReader("test.txt"));//读取文件
for(int i=0;i<3;i++)
{
String str=reader.readLine();
if(str!=null)
{
String[] newstr = str.split(",");
t[i].x=Double.parseDouble(newstr[0]);
t[i].y=Double.parseDouble(newstr[1]);
}
}
}
catch(Exception e)
{
System.out.println("mistake!");
}
for(int i=0;i<3;i++)
{
System.out.println(t[i].x+","+t[i].y);
}
}
}
上面是我写的一段代码
想要读取test.txt文件里的矩阵
test.txt文件的内容格式是:
3.2,5.9
4.3,7.8
6.3,4.5
运行了N次都是mistake!求问题出在哪里!
谢谢!!!

解决方案 »

  1.   

    二个可能。1。你文件路径正确不?2。test.txt文件的内容格式是修改为:
    3.2,5.9  中间用的逗号应该是英文下的逗号
      

  2.   

    String[] newstr = str.split(",");
    前面加上t[i]=new str_test().new test();
    (确保test.txt在正确的位置)
      

  3.   

    for(int i=0;i<3;i++)
    {
    String str=reader.readLine();
    if(str!=null)
    {
    String[] newstr = str.split(",");
    t[i].x=Double.parseDouble(newstr[0]);
    t[i].y=Double.parseDouble(newstr[1]);
    }
    }
    修改为:
    for(int i=0;i<3;i++)
    {
    test t1 = new test ();
    String str=reader.readLine();
    if(str!=null)
    {
    String[] newstr = str.split(",");
    t1.x=Double.parseDouble(newstr[0]);
    t1.y=Double.parseDouble(newstr[1]);
    t[i]=t1;
    }
    }
      

  4.   

    1) t[]中的元素没有初始化,这样: test t[] = new test[3];
    str_test st = new str_test();
    for (int i=0; i<3; i++)
    t[i] = st.new test();
    2) 文件路径确保正确,应该是在工程的根目录下,如果没把握就用绝对路径3) 文件内容中确保用的不是中文标点“,”,而是英文字符“,”
      

  5.   

    我运行了下你的代码,我要说的有以下几处:
    1、test t[]=new test[3];这是一个对象数组,数组里的每个对象在使用的时候都必须new出来。
    2、文本里的逗号是中文符号还是英文符号要注意。
    3、catch里面最好要把异常打印出来,这样更容易查找哪里出错了。
    4、类的首字母要大写,规范点。
    你的主要错误是对象没有New,因为是内部类,我不太擅长,我是专门写了一个实例化Test()类的方法。