2012-08-23 41 views
1

我正在嘗試爲學校編寫一個JAVA程序。它要求我們使用提供的類,避免使用拆分,並解析不同部分之間由:分隔的電話號碼。解析電話號碼不使用拆分

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,但我認爲我的代碼是正確的。任何人都可以告訴我我在這裏做錯了什麼?

+0

瞭解如何使用調試器,並通過您的代碼步。這是你可以得到的最有價值的建議。 – Bohemian

+0

我去了調試,它在我的代碼中顯示了一條綠線。然後我按F5,它吐出了我上面的同樣的錯誤...? – ILikeTurtles

+0

就這樣說,電話號碼不是數字。是的,它們由數字組成,但這並不能使它們成爲數字;他們可以很容易地通過字母A到J.(事實上,公司*在他們的「數字」中使用字母。通常)。Ints通常會吮吸在存儲它們......原因包括這樣一個事實,即使您轉換字母到數字,前導零可以在電話號碼中顯着。 – cHao

回答

1

有一種另一種方法做你intrested這一點。您可以使用方法與string.replace打破它,或插入你:這裏的符號是從其他程序例如

outputString = outputString.replaceAll(",",",\n" + " "); 
outputString = outputString.replaceAll("\\{","\\{\n" +" "); 
outputString = outputString.replaceAll(":",": "); 
outputString = outputString.replaceAll("}",",\n}"); 
StringTokenizer output2 = new StringTokenizer(outputString,", ",true); 

這組替換字符串做了一些格式化的bizzare,要複雜explaine爲主題這篇文章,但研究ythis,你會明白這一點。爲了inderstand這指的是Java字符串API

+0

StringTokenizer是您創建的函數嗎? – ILikeTurtles

+0

我用這個,取而代之我的其他東西;它現在有效。非常感謝! – ILikeTurtles

+0

String Tokenizer是API中的另一個Java類,非常有用,您應該瞭解它! –

3

請注意,substring方法開始指定索引處的子字符串(含)。您從indexOf獲得的值是:字符的索引。您的使用substringindexOf(char, index)存在問題。

打印出來 - 試圖解析它們之前在每次得到指標,和你的子串,你會看到什麼是錯的 - 或檢查在調試器。

例如,在你的AJDissector構造你在哪裏得到的字符串,請嘗試:

System.out.println("substring 2 indexes: " + index0 + ", " + index1); 
System.out.println("substring 2: " + phoneNum.substring(index0, index1)); 

對於每一個指標,看看整個String,和 - 記住,指數開始在0 - 做的工作的substring在你的腦海裏。

+0

index0等於1 index1等於1 index2等於9 因此,我正在使用indexof不正確地出現。謝謝! – ILikeTurtles

+0

不要忘記將pb2q的答案標記爲解決方案,因爲這可以幫助其他人解決同樣的問題。 –

+0

仍然在做我的任務。我還沒有子串的東西工作,但我正在用util掃描程序作爲替代方法查看其他答案。當我得到這個東西的時候,我馬上就會標記。謝謝! – ILikeTurtles

0

也許你應該看得更遠一點兒通過Java API,可能在這樣的:java.util.Scanner

從的javadoc:

一個簡單的文本掃描儀可以解析基本類型和字符串 使用正則表達式。

掃描器斷開其輸入到使用定界符圖案, 它默認與空白匹配。將得到的令牌然後可以 轉換成使用各種未來 方法的不同類型的值。

java.util.Scanner API Docs