0
votes

My program gets 2 numbers from the user, one length 10 and one length 3. I am getting them as a string. I am then trying to use Integer.parseInt() to turn them into an integer. I have no code errors but when I run the program I get the following error.

Exception in thread "main" java.lang.NumberFormatException: For input string: "4159238189" 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 assn3.secrets.storetoarray(Assn3.java:75) at assn3.Assn3.main(Assn3.java:30) Java Result: 1


public class Assn3 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    secrets agent = new secrets();

    agent.getnumber();
    agent.storetoarray();
}
}

class secrets{
private String initialphone, key;
//private String phonestring, keystring;
private int phonelength, keylength;
private int phoneint, keyint;
private int phonetemp1, phonetemp2;
double[] phonearray = new double[phonelength];
double[] keyarray = new double[keylength];


public void getnumber()
//get the phone number and security code
//If the number and key are not the right length the program will stop
{         
   Scanner input = new Scanner(System.in);
   System.out.print("Please enter the phone number you need encrypted\n"
           + "just enter the 10 digits no dashes\n");
   initialphone = input.next(); 
   phonelength = initialphone.length();
   if(phonelength !=10){
       System.out.print("nope");
       System.exit(0);
   }
   System.out.print("Please enter the encryption key\n"
           + "just 3 digits please\n");
   key = input.next();
   keylength = key.length();
   if(keylength !=3){
       System.out.print("nope");
       System.exit(0);
   }

}

public void storetoarray()
        //Turn the strings to ints
        //A loop chops of the last digit and stores in an array
{



    phoneint = Integer.parseInt(initialphone);
    phonetemp1 = phoneint;
    keyint = Integer.parseInt(key);


    for (int i = phonelength; i>=0; i--)
    {
        phonearray[i] = phonetemp1%10;
        phonetemp2 = phonetemp1 - phonetemp1%10;
        phonetemp1 = phonetemp2;
        System.out.print("Phone temp 2" + phonetemp2);
    }



}

}

2

2 Answers

1
votes

Integers (and ints) can only have a value up to Integer.MAX_VALUE which is (2^31)-1 -- about 2 billion. Your input is larger than that, which makes it not a parseable int, so parseInt() throws the exception. It would work to use Long.parseLong(), which has a much higher MAX_VALUE, but for your purpose, you might not need your variable to be a numeric object at all. Since you're not performing any mathematical operations on it, you can most likely just keep it as a String.

Edit: On second glance I see you are performing some arithmetic on the phone number, but the same effect could most likely be achieved with String operations. It's hard to tell what you're doing there.

0
votes

integer is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647. long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value, range is from –9,223,372,036,854,775,808 to 9 ,223,372,036,854,775,807. This makes it useful when big, whole numbers are needed.

Try this line of code-

long phoneint = Long.parseLong(initialphone);
long phonetemp1 = phoneint;