Hi all!
I've been working on a problem from my Intro to Java class for a couple of weeks and I feel like I'm close. I turned it in and only got partial credit. Here goes: This program allows a user to enter a phone number, country code, area code, and local number. The program needs to print out, separately what each of those string variables will be. The catch is that in some countries, the length of each string can vary. Here is what I have so far and at this point i have smoke coming out of my ears and am about to give up:

import java.util.Scanner;

public class PhoneNumberDissector
{
	public static void main(String[] args)
	{
		Scanner stdIn = new Scanner(System.in);

		// Initialize the variables
		String phoneNumber;
		String countryCode;
		String areaCode;

// Begin the main Do - While loop to check for "q" to end the program
	do
	{
		// Get user phone number input or have user quit the program
		System.out.println("Please enter a phone number in the form cc-area-local,");
		System.out.println("where cc = country code digits, area = area code digits,");
		System.out.println("and local = local phone digits.");
		System.out.print("Or enter <q> to quit: ");
		phoneNumber = stdIn.nextLine();

	// Test to see if character in location zero is not equal to "q"
	if (phoneNumber.charAt(0) != 'q')
		{

		// Determine the String variable for countryCode
		countryCode = (phoneNumber.substring(0,phoneNumber.indexOf('-')));
		int sep = phoneNumber.lastIndexOf("-");

		char dash = phoneNumber.charAt(1);

		System.out.println("Country Code = " + countryCode);

		if (dash == '-')
		{

		// Determine the String variable for areaCode
		areaCode = phoneNumber.substring(2,5);
		System.out.println("Area Code = " + areaCode);
		}

		else
		{
		areaCode = phoneNumber.substring(3,6);
		System.out.println("Area Code = " + areaCode);
		}


		// Whatever is left over after the 2nd hyphen is now the local number
		System.out.println("Local Number = " + phoneNumber.substring(sep+1));
		}

	else
		{
		phoneNumber = "q";
		}

	}while (phoneNumber != "q");


	}
}

Thanks for any help you can give!
Thayland

Recommended Answers

All 4 Replies

char dash = phoneNumber.charAt(1);

Why do you seem to think the character at that index is 1? That will only be the case if you enter it in the form 1-whatever. Since you determined that your '-' is at phoneNumber.indexOf("-"), the next part of your phoneNumber is going to be at phoneNumber.indexOf("-") + 1.

I know what you are saying. I originally was trying to get the output that was given to us in class. Which I did! However, the Prof decided to test my program with three digits in the Country Code and everything went whacky from there.

What I was thinking of was making each character in the PhoneNumber string into a substring PhoneCharacter(x) and then run each character through a loop and assign CountryCode, AreaCode and LocalNumber the contents of whatever, and however many characters where between the dashes. This is what the Prof wants. Is there a command to find out the numerical length of the PhoneNumber String? Am I going about this the hard way? I just bet there is an easy simple way to code this problem and I'm just not catching on.

Thanks for your help!

Yeah, there are a lot of ways to do this. . you already have all the pieces to do it in front of you, you just need to put them together. Consider the following:

1. You already figured out how to get the countryCode.
2. Now you need to get the area code and "local", whatever that is.
3. You know the index that the area code starts at. That index is phoneNumber.indexOf('-') + 1.
4. You know the index that the area code ends at. That index, which you already calculated, is the variable "sep" in the code you posted, minus 1.
5. Use the substring method and the index the area code starts and ends at to get the area code.
6. The method to get the length of a String is length(). Since you know the index the area code ends at, (sep), sep + 1 is the first index of "local". The last index of local is the last index of the String, which is phoneNumber.length()-1. Minus one because the size of the string might be 10, but it will be indexed by 0-9.

good luck

Yes I figured out how to get the country code, but the problem is that the country code, area code and local numbers can be any length.

With the code written the way it is now, I do get the country code but the problem is that the length is confined to 1 or 2 digits. I could manually make it three or four, but the real solution is to:
1. make a substring for country code that is the first character to the character just before the dash,
2. then the area code substring needs to be composed of whatever is right after the first dash to the character before the second dash and
3. then the local number substring is whatever is leftover after the third dash.

I've worked on this all weekend and I'm determined to figure this out. I know the solution is probably obvious to you regular Javanauts, but I'm a beginner if you couldn't tell. :)

I totally appreciate your hints and help more than you can imagine. I just hope that your patience doesnt run out with me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.