1

For some reason I cant get a string number value to convert to an integer the error I get is run:

Exception in thread "main" java.lang.NumberFormatException: For input string: "6327818260"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:495)
    at java.lang.Integer.parseInt(Integer.java:527)
    at MyDirectory.getsize(MyDirectory.java:18)
    at a19010.main(a19010.java:79)

Java Result: 1

If my input string is "6327818260" why can't it become an integer? my code is

public String getsize()
{
  int intSize = Integer.parseInt(mySize);
  int count = 0;
  String dataType = "";
  while (intSize > 1000)
  {   
   intSize = intSize / 1000;
   count++;
  } 
  switch (count)
  {
    case 0:
    dataType = "Bytes";
    break;
    case 1:
    dataType = "KB";
    break;
    case 2:
    dataType = "MB";
    break;
    case 3:
    dataType = "GB";
    break;
    case 4:
    dataType = "KB";
    break;    
  }   
    return intSize + dataType ;
} 

mySize is taken from part of a string gotten from a text file here

 public class MyDirectory 
{
  String myName = "" ;
  String myNum = "";
  String mySize = "";
    public MyDirectory(String line)

  {
    line = line.trim();  
    String[] lineParts = line.split(" ");
     mySize = lineParts[0];
     myNum = lineParts[1];
     myName = lineParts[3];
  }

The line im splitting looks like 6327818260 6486 SUB-TOTAL: E:\WIS26\LCORRES

1
  • 3
    What is the maximum value of an Integer? Commented Apr 19, 2013 at 20:16

3 Answers 3

6

The largest possible integer is 2147483647 which is a good deal less than the value you're trying to parse: 6327818260

You will need to use a long or a BigInteger/BigDecimal to hold that value.

For BigInteger/BigDecimal Strings representing base 10 integers can be parsed by the constructors that take a string argument.

BigDecimal bigDecimal = new BigDecimal("6327818260");
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a method to turn a string into a big decimal? Im have trouble initializing the big decimal from the mySize string
The constructor that takes a String understands decimal integers.
That's what I thought I must be doing something incorrectly my constructor looks like BigDecimal size = BigDecimal(mySize);
That fixed it of corse. I guess I forgot for some reason
2
6327818260 is greater than the Integer.MAX_VALUE `2147483647`

You can try to parse the string to Long

2 Comments

This lets my code compile but It doesn't work for the division since it get rid of the decimal
if it is decimal, try BigDecimal. you can BigDecimal dc = new BigDecimal(str);
0

If it's a 32-bit integer you're looking to pack that into, it's too big.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.