写了一个java的程序,读取文件的数字,可是不知道哪里有个exception了,请高手指点:
这是data.txt1.25 0
-1.25 2
0.0 0
-1.25 0
1.0 2
0.0 0
3.33 -1
-1.375 1
1.0 2
1.0 1
0.0 0
-4.75 2
-3.0 -2
3.0 2
0.0 0import java.util.*;
import java.io.*;public class test
{ public static void main(String[] args) //throws Exception
    {

try{
double tmpCoef;
int tmpExpo;
BufferedReader br = new BufferedReader(new FileReader(new File("data.txt")));
String sLine = "";
int flag = 0;
while ((sLine = br.readLine()) != null)
{
//System.out.println(flag);
String[] ss = sLine.split(" ");
tmpCoef = Double.parseDouble(ss[0]);
tmpExpo = Integer.parseInt(ss[1]);//  reads from the file
//System.out.println(tmpCoef+" "+tmpExpo); //double absTmpCoef = absValue(tmpCoef);
//now stores it to the polynominal
if ( tmpExpo != 0 ||tmpCoef !=0.0  )
{
if (flag == 1)
{
//store the data into a
System.out.println("a: "+flag);
//a.display();
}
else if (flag == 2)
{

//store the data into b
//b.addTerm(tmpCoef, tmpExpo);
System.out.println("b: "+ flag); }
else if (flag == 3)
{
//store the data into c
//c.addTerm(tmpCoef, tmpExpo);
System.out.println("c: "+ flag);
}
else if (flag ==4)
{
//store the data into d
//d.addTerm(tmpCoef,tmpExpo);
System.out.println("d: "+ flag);
}
else
{
break;
}
}


if (tmpCoef  == 0.00 && tmpExpo == 0)
{
flag++;
//continue;
} }
}

catch(Exception e)
{
System.out.println("exception caught");

}
    }
}

解决方案 »

  1.   

            catch(Exception e)
            {
             e.printStackTrace();
                    System.out.println("exception caught");        }这样就能看到是什么异常了此外 public class test
     要改成 public class Test
      

  2.   

    次循环只走了一次,也就是你在
    while ((sLine = br.readLine()) != null)
    里面限制的!!不能读取下一行了!!
      

  3.   


    空格问题String[] ss = sLine.split(" ");这一名中的空格可能和文件文件中两数分隔的空格不一样,我指的是编码格式.你将文件本件中的空格复制到上面一句sLine.split(" ")的空格位置.说不定有用
      

  4.   

     else
                        {
                            break;
                        }因为程序走到else里面了,因此你已经退出这个循环了。。flag==0.....呵呵。你程序的逻辑有问题,再仔细看看吧
      

  5.   


    e.printStackTrace();
     这个好啊,太感谢了
      

  6.   

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package exp1;/**
     *
     * @author szhu5
     */
    import java.util.*;
    import java.io.*;public class test {    public static void main(String[] args) //throws Exception
        {        try {
                double tmpCoef;
                int tmpExpo;
                BufferedReader br = new BufferedReader(new FileReader(new File("d://data.txt")));
                String sLine = "";
                int  flag=0;
                while (br.readLine()!=null) {
                    sLine=br.readLine();
                    System.out.println(flag);
                    String[] ss = sLine.split(" ");
                    tmpCoef = Double.parseDouble(ss[0]);
                    tmpExpo = Integer.parseInt(ss[1]);//  reads from the file
                    System.out.println(tmpCoef+"---"+tmpExpo);
                    //System.out.println(tmpExpo);                //double absTmpCoef = absValue(tmpCoef);
                    //now stores it to the polynominal
                    //if (tmpExpo != 0 || tmpCoef != 0.0) {
                        /*if (flag == 1) {
                            //store the data into a
                            System.out.println("a: " + flag);
                            //a.display();
                        } else if (flag == 2) {                        //store the data into b
                            //b.addTerm(tmpCoef, tmpExpo);
                            System.out.println("b: " + flag);                    } else if (flag == 3) {
                            //store the data into c
                            //c.addTerm(tmpCoef, tmpExpo);
                            System.out.println("c: " + flag);
                        } else if (flag == 4) {
                            //store the data into d
                            //d.addTerm(tmpCoef,tmpExpo);
                            System.out.println("d: " + flag);
                        } else {
                            break;
                        }*/
                      //break;
                        //}                if (tmpCoef == 0.00 && tmpExpo == 0) {
                        flag++;
                        //continue;
                    }            }
            } catch (Exception e) {
                System.out.println("exception caught");        }
        }
    }
    问题出在我注释掉的那段代码里,到0.00 0时间退出循环了
      

  7.   

    不知道你的逻辑是什么,不好给你改。
    你的代码的Exception应该是读入了空格,然后强制转换成数字造成的。
    另外,break导致循环只进行了一次
    最后,建议这句这样写
    String[] ss = sLine.trim().split("\\s");