java.lang.NumberFormatException:对于输入字符串:“”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19801823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:17:48  来源:igfitidea点击:

java.lang.NumberFormatException: For input string: " "

java

提问by

Exception in thread "main" java.lang.NumberFormatException: For input string: "    " 
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:430)
at java.lang.Long.valueOf(Long.java:540)
at PckUtiles.lexec.leer(lexec.java:62)
at PckUtiles.lexec.verificar(lexec.java:34)
at PckjForms.Main.main(Main.java:40)

*I have next error when run project "Exception in thread "main" java.lang.NumberFormatException: For input string: " the function of my class is to avoid re-run the application. could help to locate the fault. thank you very much *

*运行项目“线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“我的类的功能是避免重新运行应用程序时出现下一个错误。可以帮助定位故障。非常感谢您 *

here is my class lexec

这是我的班级 lexec

public class lexec {
    private String ruta = System.getProperties().getProperty("user.dir");
    private File archivo = new File(ruta + "\Sifme.tmp");
    private int contador = 20;

    public lexec(){};

    public boolean verificar(){
        if(archivo.exists()){
            long time = leer();
            long res = rTiempo(time);
            if(res<contador){
                JOptionPane.showMessageDialog(null, "La aplicación esta en ejecución");
                System.exit(0);
                return false;
            }else{
                tarea_();
                return true;
            }
        }else{
            sifme();
            tarea_();
            return true;
        }
    }

    public long leer(){
        String line = "0";
        BufferedReader br;
        try{
            br = new BufferedReader(new FileReader(archivo));
            while(br.ready()){
                line = br.readLine();
            }
        }catch(IOException e){
            System.err.println(e.getMessage());
        }
        return Long.valueOf(line).longValue();
    }
    public void tarea_(){
        ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
        ses.scheduleAtFixedRate(
                new Runnable(){
                    @Override
                    public void run(){
                        sifme();
                    }
                },1000,contador*1000,TimeUnit.MILLISECONDS);
    }

    public void sifme(){
        Date fecha = new Date();
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter(archivo));
            bw.write(String.valueOf(fecha.getTime()));
            bw.close();
        }catch(IOException e){
            System.out.println(e.getMessage());
        }
    }
    public long rTiempo(long tiempoN){
        Date fecha = new Date();
        long t1 = fecha.getTime();
        long tiempo = t1 - tiempoN;
        tiempo = tiempo/1000;
        return tiempo;
    }
    public void detruir_(){
        if(archivo.exists()){
            archivo.delete();
            System.exit(0);
        }
    }
}

采纳答案by Daniel Kaplan

Although you didn't tell us what line gives us the error (even though you should have) I can deduce that it's this line:

虽然你没有告诉我们哪一行给了我们错误(即使你应该有)我可以推断出这是这一行:

    return Long.valueOf(line).longValue();

The problem is lineis a string of whitespace, not a numeric string. You can't expect to convert whitespace into a Long. That's why you get this error.

问题是line一串空格,而不是一个数字串。您不能期望将空格转换为Long. 这就是您收到此错误的原因。

回答by MadProgrammer

I would have thought the exception self explanatory, " "can not be parsed as a Long.

我原以为异常是不言自明的," "不能解析为Long.

Try using something like return line == null ? 0 : line.trim().isEmpty() ? 0 : Long.valueOf(line).longValue();to determine the validity of the Stringvalue first and return a default value where it's not.

尝试使用类似的方法return line == null ? 0 : line.trim().isEmpty() ? 0 : Long.valueOf(line).longValue();首先确定String值的有效性,然后在不存在的情况下返回默认值。

Should you not care about differentiating between a nullStringor an empty Stringyou could also use return line == null || line.trim().isEmpty() ? 0 : Long.valueOf(line).longValue();which may be easier to read

如果您不关心区分 anullString或空,String您也可以使用return line == null || line.trim().isEmpty() ? 0 : Long.valueOf(line).longValue();它可能更容易阅读

Or, throw some kind of exception where the value does not meet your exceptions if required

或者,如果需要,在值不符合您的异常的情况下抛出某种异常