[英]I am not able to parse the string in java to long
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Transactions trans=new Transactions();
System.out.println(request.getParameter("assetid"));
System.out.println(request.getParameter("barcodeno"));
System.out.println(request.getParameter("location"));
System.out.println(request.getParameter("employeeid"));
System.out.println(request.getParameter("categoryid"));
String categoryId=request.getParameter("categoryid");
String employeeId=request.getParameter("employeeid");
String Assetid=request.getParameter("assetid");
String locationcode=request.getParameter("locationcode");
long AssetId=Long.parseLong(Assetid);
long categoryid123=Long.parseLong(categoryId);
long employeeid=Long.parseLong(employeeId);
long LocationCode=Long.parseLong(locationcode);
System.out.println("here the employeeId id is"+employeeId);
System.out.println("here the Assetid id is"+Assetid);
trans.setBarcodeno(request.getParameter("barcodeno"));
trans.setLocation(new LocationMaster(LocationCode));
trans.setAssetId(AssetId);
trans.setCategory(new CategoryMaster((categoryid123)));
trans.setEmployee(new EmployeeMaster(employeeid));
//trans.setEquipment(new EquipmentMaster(employeeid));
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
Session sessionHb=sessionFactory.openSession();
Transaction tx=sessionHb.beginTransaction();
HttpSession session=request.getSession();
sessionHb.save(trans);
System.out.println("transition saved");
tx.commit();
response.sendRedirect("Assetisued.jsp");
//CategoryMaster categorymaster =;
}
以下是我在解析之前从中获得的值
Asset id=101
Categoryid=1
Barcode =1
Employeeid=10
locationid=1
亲爱的先生,只要提供NumberFormatException
我就无法将字符串值解析为。 我能够显示所有的字符串值。 我检查了所有内容,但无法完全理解问题所在。 请检查并给我您的反馈...
首先,将这些参数的值存储到变量中,然后打印变量的值以及将要执行的操作。
例如:
String categoryId=request.getParameter("categoryid");
System.out.println(categoryId);
long categoryid123=Long.parseLong(categoryId);
查看是否可能发生NumberFormatException。
您的输入中可能有空格。
尝试删除空格,例如:
long AssetId=Long.parseLong(Assetid.trim());
与实际错误相比,这在您如何查找问题中似乎更成问题。
为了更容易发现问题,请确保遵循命名约定,即变量应放在驼峰中
long locationCode=Long.parseLong(locationcode);
不
long LocationCode=Long.parseLong(locationcode);
我建议的第二件事是使您的变量名称更具可描述性和区别性。 一个好的规则是:“如果变量之间的唯一区别是大小写,则它们太相似了。”
有关此的更多信息,请访问http://www.oracle.com/technetwork/java/codeconv-138413.html
最后,如果您遇到问题并提出疑问,则应该找到仍然会产生问题的最少代码量。 这样,可以更轻松地为您提供帮助,因此人们会更愿意提供帮助。
现在针对实际问题,我认为您需要确保您没有解析太多空间。 即在
Barcode =1
您应该删除空间,使其成为
Barcode=1
而在
Asset id=101
我认为该空间也会引起一些问题。
很抱歉,我没有解决您的问题,但是我希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.