循环的不同结果
以下代码注释掉的部分去掉注释后就会出现以下错误,红色字体部分标明出现了空串,而Double.parseDouble的参数不能为空串。
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
at java.lang.Double.parseDouble(Double.java:510)
at javaapplication10.JavaApplication10.main(JavaApplication10.java:34)
Java Result: 1
debug后发现去掉注释后while循环判断没成功,row为空的时候仍然进入了while循环,导致str1为空串求高人指点
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package javaapplication10;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;/** * * @author Administrator */public class JavaApplication10 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here String row; FileReader read; try { read = new FileReader("D:/data.txt"); BufferedReader br = new BufferedReader(read); try { while ((row = br.readLine()) != null) { String str1[] = row.split(" ");// for (String stemp : str1) {// double h = Double.parseDouble(stemp);// } } } catch (IOException ex) { Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex); } } catch (FileNotFoundException ex) { Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex); } }}
for (String stemp : str1) { if (stemp.equals("")) { continue; } double h = Double.parseDouble(stemp); System.out.println(h); }
[解决办法]
for (int i = 0; i < 3; i++)