An error occurred while converting String to int.

Source: Internet
Author: User

An error occurred while converting String to int.

Preface

I plan to write a blog almost a year ago, but I haven't written it for various reasons. Instead, some of my colleagues wrote it shortly after I suggested that they write a blog, such as Zhang Bo, I will write an article every time I sum up a knowledge point. This will not only make IT easier for me to view and read IT later, but also facilitate the reference of IT friends.

Today is July 22, April 25, 2017, with light rain. I was planning to write it again at night, but I don't want to drag it any more. I encountered a string-to-int type error during Excel parsing and file uploading last night. So I will write my first blog today. Although simple, it is also a good start. I hope that I can stick to writing and write some good blogs in the future. Of course, if you can find a solution to the problem from your blog, I am very pleased.

The following is a simple program:

1 package com. gxxy. team1.yyd. controller; 2 3 public class NumDemo {4 public static void main (String [] args) {5 String num1 = "2333"; 6 String num2 = "414.0 "; 7 // you can use the following method to convert an Integer string to int 8 int intNum1 = Integer. parseInt (num1); 9 int intNum3 = Integer. valueOf (num1); 10 // float string cannot be converted into an Integer int11 directly using the following method // int intNum2 = Integer. parseInt (num2); 12 // int intNum4 = Integer. valueOf (num2); 13 // if you want to convert it to "double" type, then convert it to int14 Double douNum1 = Double. valueOf (num1); 15 Double douNum2 = Double. valueOf (num2); 16 17 int intNum5 = douNum1.intValue (); 18 int intNum6 = douNum2.intValue (); 19 System. out. println ("-----------" + intNum1); 20 System. out. println ("-----------" + intNum3); 21 System. out. println ("-----------" + douNum1 + "," + douNum2); 22 System. out. println ("-----------" + intNum5 + "," + intNum6); 23} 24}
If you use the following method for conversion, the following error is returned: int intNum2 = Integer. parseInt (num2); int intNum4 = Integer. valueOf (num2); Exception in thread "main" java. lang. numberFormatException: For input string: "414.0" at java. lang. numberFormatException. forInputString (NumberFormatException. java: 65) at java. lang. integer. parseInt (Integer. java: 580) at java. lang. integer. parseInt (Integer. java: 615) at com. gxxy. team1.yyd. controller. numDemo. main (NumDemo. java: 11)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.