我把它改到一个main中,看看yData在命令行下的输出 代码是: BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); String line; int i=0; while((line = in.readLine()) != null){ yData[i] = Integer.parseInt(line); System.out.println(yData[i]); } //close the input stream in.close(); 结果数值确实能打印出来,不过出现这样的异常 Exception in thread "main" java.lang.NumberFormatException : For input strintg;"" at java.lang.NumberFormatException.forInputString(NumberFormatExcepton.java:48) at java.lang.Integer.parseInt(Integer.java:489) at java.lang.Integer.parseInt(Integer.java:518) at CoordinateTest.main(CoordinateTest.java:194)图形绘制的代码应该没什么问题,我预先定义一组yData值时,图形可绘出。
哦,是的,打错了,应该是yData[i++] 初始化做了,是 int[] yData = new int[300]; 我觉得还是格式的问题,要不然就算是yData[i]只保存了一个数据,也不至于出现异常啊。
Exception in thread "main" java.lang.NumberFormatException : For input strintg;"" 很明显了。就是最后读到了一个"",然后转化为int值时出错了。你可以处理一下。 try { yData[i++] = Integer.parseInt(line); }catch(NumberFormatException e){ //ignore }
特别是对打印的情况.
如果我定义一个TextArea去显示读取到的数据倒是可以的,就是图形画不出来。我是用ydata数组保存y轴上的数据的。
客户端 程序段是
public void init() {
try{
URL url = new URL("http://ray:8080/servlets-examples/servlet/DbServlet");
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type",
"appplication/x-www-form-urlencoded");
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line;
int i=0;
while((line = in.readLine()) != null){
yData[i] = Integer.parseInt(line);//用一个int 数组yData保存读到的数据
}
in.close();
}public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
CoordinateFrame coordinate =
new CoordinateFrame(500, 400 , BufferedImage.TYPE_INT_RGB);
coordinate.drawFrame(); g2d.drawImage(coordinate, 0, 0, this);
setForeground(Color.blue);
int left = coordinate.getLeftMargine();
int buttom = coordinate.getButtomMargine();
for(int i=0; i<yData.length; i++){
g2d.drawLine(xData[i]+left,400-buttom-yData[i],
xData[i+1]+left,400-buttom-yData[i+1]);
}
}
建议如此调试:
1、在paint方法中用drawString等方法看能否显示ydata数据,如能,怎通讯应该没有问题
2、在paint方法中画图是不涉及ydata数据,且尽量简单,看看是否是作图有问题
yData[i] = Integer.parseInt(line);//用一个int 数组yData保存读到的数据
}
in.close();
}
这段代码大大的有问题,你的yData初始化没有?
如果初始化了,大小为多少阿?
你的i一直是0哦,那数据就只能保存一个地。
代码是:
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line;
int i=0;
while((line = in.readLine()) != null){
yData[i] = Integer.parseInt(line);
System.out.println(yData[i]);
}
//close the input stream
in.close();
结果数值确实能打印出来,不过出现这样的异常
Exception in thread "main" java.lang.NumberFormatException : For input strintg;""
at java.lang.NumberFormatException.forInputString(NumberFormatExcepton.java:48)
at java.lang.Integer.parseInt(Integer.java:489)
at java.lang.Integer.parseInt(Integer.java:518)
at CoordinateTest.main(CoordinateTest.java:194)图形绘制的代码应该没什么问题,我预先定义一组yData值时,图形可绘出。
初始化做了,是 int[] yData = new int[300];
我觉得还是格式的问题,要不然就算是yData[i]只保存了一个数据,也不至于出现异常啊。
很明显了。就是最后读到了一个"",然后转化为int值时出错了。你可以处理一下。
try {
yData[i++] = Integer.parseInt(line);
}catch(NumberFormatException e){
//ignore
}
经验不足啊,多谢chenxv1980了。