0
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 *

here is my class 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);
        }
    }
}
3
  • 1
    possible duplicate stackoverflow.com/questions/19801544/… Commented Nov 6, 2013 at 0:33
  • Not Possible Duplicate it is Duplicate Commented Nov 6, 2013 at 0:38
  • 1
    @Prateek but previous topic was deleted by the author. So no duplication from now ;-) Commented Mar 6, 2014 at 11:08

2 Answers 2

4

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 line is 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

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

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

2 Comments

return line == null || line.trim().isEmpty() ? 0 : Long.valueOf(line).longValue(); would be better as it's easier to read.
@SimonArsenault No argument there, assuming that you wanted to treat a null and empty String the same way, just saying...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.