java.lang.NumberFormatException:对于输入字符串:"&引用;
*在运行project“线程中的异常”main“java.lang.NumberFormatException:For input string:”时出现下一个错误: 我的类的功能是避免重新运行应用程序。可以帮助定位故障。非常感谢你 * 这是我的课java.lang.NumberFormatException:对于输入字符串:"&引用;,java,Java,*在运行project“线程中的异常”main“java.lang.NumberFormatException:For input string:”时出现下一个错误: 我的类的功能是避免重新运行应用程序。可以帮助定位故障。非常感谢你 * 这是我的课 Exception in thread "main" java.lang.NumberFormatException: For input string: " " at java.lang.NumberFormatException.forI
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)
公共类lexec{
私有字符串ruta=System.getProperties().getProperty(“user.dir”);
私有文件archivo=新文件(ruta+“\\Sifme.tmp”);
私人int contador=20;
公共lexec(){};
公共汽车(){
如果(archivo.exists()){
长时间=里尔();
长时间=rTiempo(时间);
如果(res虽然你没有告诉我们哪一行给了我们错误(即使你应该有),我可以推断是这一行:
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);
}
}
}
问题是,行
是一个空格字符串,而不是数字字符串。您不能期望将空格转换为长
。这就是为什么会出现此错误。虽然您没有告诉我们哪一行给了我们错误(即使您应该有),但我可以推断是这一行:
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);
}
}
}
问题是,行
是一个空格字符串,而不是数字字符串。您不能期望将空格转换为长
。这就是为什么会出现此错误。我认为异常是不言自明的,“
不能被解析为长
尝试使用类似于return line==null?0:line.trim().isEmpty()?0:Long.valueOf(line).longValue();
的方法首先确定字符串的有效性,并在不有效的地方返回默认值
如果您不关心区分null
字符串
或空字符串
,您也可以使用返回行==null | | | line.trim().isEmpty()?0:Long.valueOf(line).longValue();
,这可能更容易阅读
或者,如果需要,在值不符合您的异常的情况下抛出某种类型的异常我认为异常是不言自明的,“
不能解析为长的
尝试使用类似于return line==null?0:line.trim().isEmpty()?0:Long.valueOf(line).longValue();
的方法首先确定字符串的有效性,并在不有效的地方返回默认值
如果您不关心区分null
字符串
或空字符串
,您也可以使用返回行==null | | | line.trim().isEmpty()?0:Long.valueOf(line).longValue();
,这可能更容易阅读
或者,在值不符合您的异常(如果需要)的情况下抛出某种异常可能重复不可能重复Duplicate@Prateek但之前的主题已被作者删除。因此,从现在起不再重复;-)可能重复不可能重复它是Duplicate@Prateek但是作者删除了前面的主题。因此,从现在开始没有重复;-)返回行==null | | line.trim().isEmpty()?0:Long.valueOf(line.longValue())
会更好,因为它更容易阅读。@SimonArsenault那里没有参数,假设您希望以相同的方式处理null
和空String
字符串,只需说…return line==null | | line.trim().isEmpty()?0:Long.valueOf(line).longValue()
会更好,因为它更容易阅读。@SimonArsenault这里没有参数,假设您想以同样的方式处理null
和空字符串,只要说。。。