私は学校向けのJAVAプログラムを書こうとしています。:
提供されたクラスを使用し、splitの使用を控え、異なる部分で区切られた電話番号を解析する必要があります。
package aaronjonesprog1;
public class AaronJonesProg1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
AJDissector phone = new AJDissector("1:919:882:5000");
System.out.println(phone.getPhoneNumber());
System.out.println(phone.getPhoneNumber(4));
System.out.println(phone.getPhoneNumber(1));
System.out.println(phone.getPhoneNumber(3));
System.out.println(phone.getPhoneNumber(2));
}
}
私の運転手:
package aaronjonesprog1;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Aaron
*/
public class AJDissector
{
private String phoneColon;
private int areaCode, preCode, number, countryCode, emptyNum;
public AJDissector(String phoneNum)
{
this.phoneColon = phoneNum;
int index0 = phoneNum.indexOf(":");
int index1 = phoneNum.indexOf(":", index0);
int index2 = phoneNum.lastIndexOf(":");
this.countryCode = Integer.parseInt(phoneNum.substring(0, index0));
this.areaCode = Integer.parseInt(phoneNum.substring(index0, index1));
this.preCode = Integer.parseInt(phoneNum.substring(index1, index2));
this.number = Integer.parseInt(phoneNum.substring(index2));
}
public String getPhoneNumber()
{
return this.phoneColon;
}
public int getPhoneNumber(int a)
{
switch (a)
{
case 1:
return this.number;
case 2:
return this.countryCode;
case 3:
return this.preCode;
case 4:
return this.number;
default:
return this.emptyNum;
}
}
}
私が受け取るエラーは次のとおりです。
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at aaronjonesprog1.AJDissector.<init>(AJDissector.java:26)
at aaronjonesprog1.AaronJonesProg1.main(AaronJonesProg1.java:18)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
誰かが私が間違っていることを理解するのを手伝ってもらえますか?間違った使い方をしているとは思いませんparseInt
。http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.htmlを調べて理解しようとしましたが、コードは正しいと思います。誰かが私がここで間違っていることを教えてもらえますか?