救命啊,问题出在哪?

yorkzjy 2010-02-22 12:25:27
写了一个java的程序,读取文件的数字,可是不知道哪里有个exception了,请高手指点:
这是data.txt

1.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 0



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("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");

}


}
}
...全文
74 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
oO临时工Oo 2010-02-22
  • 打赏
  • 举报
回复

空格问题

String[] ss = sLine.split(" ");这一名中的空格可能和文件文件中两数分隔的空格不一样,我指的是编码格式.

你将文件本件中的空格复制到上面一句sLine.split(" ")的空格位置.

说不定有用
masterfojava 2010-02-22
  • 打赏
  • 举报
回复
次循环只走了一次,也就是你在
while ((sLine = br.readLine()) != null)
里面限制的!!不能读取下一行了!!

aSysBang 2010-02-22
  • 打赏
  • 举报
回复
catch(Exception e)
{
e.printStackTrace();
System.out.println("exception caught");

}

这样就能看到是什么异常了

此外 public class test
要改成 public class Test
xiaoguan_Java 2010-02-22
  • 打赏
  • 举报
回复
先坐上沙发再说啊 。。。
yorkzjy 2010-02-22
  • 打赏
  • 举报
回复
学java不久,有劳高手了
ScAREcrOw_ss 2010-02-22
  • 打赏
  • 举报
回复
不知道你的逻辑是什么,不好给你改。
你的代码的Exception应该是读入了空格,然后强制转换成数字造成的。
另外,break导致循环只进行了一次
最后,建议这句这样写
String[] ss = sLine.trim().split("\\s");
bobo364 2010-02-22
  • 打赏
  • 举报
回复
/*
* 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时间退出循环了
yorkzjy 2010-02-22
  • 打赏
  • 举报
回复
引用 6 楼 final_xt 的回复:
else
                    {
                        break;
                    }

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

这个我知道,不太自信而已。谢谢了
yorkzjy 2010-02-22
  • 打赏
  • 举报
回复
引用 3 楼 l417584711 的回复:
        catch(Exception e)
        {
       e.printStackTrace();
                System.out.println("exception caught");

        }

这样就能看到是什么异常了

此外 public class test
要改成 public class Test


e.printStackTrace();
这个好啊,太感谢了
final_xt 2010-02-22
  • 打赏
  • 举报
回复
else
{
break;
}

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

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧