A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张玉建 中级黑马   /  2013-9-26 21:59  /  2199 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张玉建 于 2013-9-26 23:49 编辑




  • public class PropertiesDome {
  • /**
      * @param args
      * @throws IOException
      */
    public static void main(String[] args) throws IOException {
      // TODO Auto-generated method stub
      Properties prop = new Properties();
      File file= new File("counnt.ini");
      if(!(file.exists()))
      {
       file.createNewFile();
      }
      FileInputStream fileIn= new FileInputStream(file);
      prop.load(fileIn);
      int counnt =0;
      String value = prop.getProperty("time");
      if(value!=null) {
       counnt= Integer.parseInt(value);//也就是这里出现异常
       if(counnt>=5)
       {
        System.out.println("次数已到");
       }
      }
      counnt++;
      prop.setProperty("time", counnt+" ");
      FileOutputStream fileOut=new FileOutputStream(file);
      
      prop.store(fileOut,"");
      fileIn.close();
      fileOut.close();
      
    }
  • }
    Exception in thread "main" java.lang.NumberFormatException: For input string: "1 "
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:456)
    at java.lang.Integer.parseInt(Integer.java:497)
    at com.intcast.mianshi.PropertiesDome.main(PropertiesDome.java:37)




  • counnt= Integer.parseInt(value);//也就是这里出现异常
  • 求解释

评分

参与人数 1技术分 +1 收起 理由
黄兴旺 + 1

查看全部评分

5 个回复

倒序浏览
好像是转换出错了,你试试先打印一下values看有没有什么字符之类的东西
回复 使用道具 举报
当取到空时,无法进行转换,会抛出异常
回复 使用道具 举报
本帖最后由 早知道 于 2013-9-26 23:03 编辑

类型转换错了,你第一次放入数据的时候
  1. prop.setProperty("time", counnt+" ");
复制代码
改成
  1. prop.setProperty("time", counnt+"");
复制代码
就没问题了,转化数字的时候遇到空字符串报错
回复 使用道具 举报
早知道 发表于 2013-9-26 23:02
类型转换错了,你第一次放入数据的时候改成就没问题了,转化数字的时候遇到空字符串报错 ...

错误依然存在,
回复 使用道具 举报

你应该没把那个配置文件删掉,删掉重新运行就ok了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马