我的程序从用户那里获得 2 个数字,一个长度为 10,一个长度为 3。我将它们作为字符串获取。然后我尝试使用 Integer.parseInt() 将它们转换为整数。我没有代码错误,但是当我运行程序时出现以下错误。
线程“主”java.lang.NumberFormatException 中的异常:对于输入字符串:“4159238189”在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:495) 在java.lang.Integer.parseInt(Integer.java:527) 在 assn3.secrets.storetoarray(Assn3.java:75) 在 assn3.Assn3.main(Assn3.java:30) Java 结果: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);
}
}
}