ILikeTurtles
ILikeTurtles

Reputation: 1020

Parsing Phone Number without the use of split

I am attempting to write a JAVA program for school. It requires that we use a provided class, refrain from the use of split, and parse a phone number separated by : between the different parts.

    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));
    }
}

My Driver:

    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;
        }
    }
}

The error I receive is:

   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)

Can someone help me understand what I am doing wrong? I don't believe I am using the parseInt wrong. I looked at http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html in an attempt to figure it out but I THINK my code is correct. Can anyone tell me what I am doing wrong here?

Upvotes: 1

Views: 529

Answers (3)

pb2q
pb2q

Reputation: 59637

Note that the substring method begins the sub-string at the specified index, inclusive. The values that you're getting from indexOf are the indices of a : character. There are problems with your usage of substring and indexOf(char, index).

Print out - or inspect in the debugger - each index that you get, and your sub-strings before trying to parse them, and you'll see what's wrong.

For example, in your AJDissector constructor where you're getting the substrings, try:

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

For each index, look at the whole String, and - remembering that indices begin at 0 - do the work of substring in your head.

Upvotes: 3

edwardsmatt
edwardsmatt

Reputation: 2044

Maybe you should look a little further through the Java API, possibly at something like: java.util.Scanner.

From the javadocs:

A simple text scanner which can parse primitive types and strings using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

java.util.Scanner API Docs

Upvotes: 0

Daniel Haro
Daniel Haro

Reputation: 391

There is a another way to do this of your intrested. You can use the string.replace method to break it up and or to insert your : notation here is example from another program

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

This set of replace string does some bizzare formating that to complicated to explaine for the topic of this post but study ythis and you will get the idea. To inderstand this refer to the java String api

Upvotes: 1

Related Questions