3 Replies - 5563 Views - Last Post: 17 April 2009 - 07:27 AM Rate Topic: -----

#1 Thayland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-April 09

Phone number breakdown - almost have it

Post icon  Posted 17 April 2009 - 06:12 AM

Well I feel like I'm pretty close because what this program is supposed to do is tear apart a phone number string and break it down to the components of country code, area code, and then local phone number:
123-123-1234567
But the catch is that you can't assume that any of the three groups of numbers will be any number of characters long. The requirement is to make the output be whatever is from the first character to the dash, then whatever is between the first dash and the 2nd dash, and then whatever is left over, and these have to be separated out into three separate strings for output.
I thought I had it, and turned this project in but my program does not give the flexibility of more than two numbers for country code or more than three for the area code.
Any help would be greatly appreciated!

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


	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Phone number breakdown - almost have it

#2 Thayland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-April 09

Re: Phone number breakdown - almost have it

Posted 17 April 2009 - 06:19 AM

Needs to be moved to Homework
Was This Post Helpful? 0
  • +
  • -

#3 lifeOnTheLine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-April 09

Re: Phone number breakdown - almost have it

Posted 17 April 2009 - 07:21 AM

This is just a hint:

//areaCode = phoneNumber.substring(2,5);//

you are limiting the length of the Country code and the Area code, by passing numbers in the method parameters.

Instead pass the index of '-' by storing both indexes in two variables.
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Phone number breakdown - almost have it

Posted 17 April 2009 - 07:27 AM

Hint: Just use a scanner with a '-' delimiter
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1