Extracting groups of chraracters from a string

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2351 Views - Last Post: 16 April 2009 - 08:05 PM Rate Topic: -----

#1 javaNoob2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-February 09

Extracting groups of chraracters from a string

Post icon  Posted 10 April 2009 - 07:17 AM

The assignment consists of creating a program that will allow the user to input a phone number and then java extracts the country code, area code and local number and lists them as seperate components. The catch is that the instructions will not let you assume that the number of digits in particular group are fixed.

I kind of know what direction I want to go with this but am not sure how exactly to get there. The phone number format must be entered as ( CC-AREA-LOCALPHONE ). So couldn't you somehow use the dashes to help you find the groups? Maybe use the substring method? Just not sure how to start out tackeling this one. I'm really new to java so please forgive me.

Any suggestions?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Extracting groups of chraracters from a string

#2 Thayland   User is offline

  • New D.I.C Head

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

Re: Extracting groups of chraracters from a string

Posted 10 April 2009 - 07:49 AM

View PostjavaNoob2, on 10 Apr, 2009 - 06:17 AM, said:

The assignment consists of creating a program that will allow the user to input a phone number and then java extracts the country code, area code and local number and lists them as seperate components. The catch is that the instructions will not let you assume that the number of digits in particular group are fixed.

I kind of know what direction I want to go with this but am not sure how exactly to get there. The phone number format must be entered as ( CC-AREA-LOCALPHONE ). So couldn't you somehow use the dashes to help you find the groups? Maybe use the substring method? Just not sure how to start out tackeling this one. I'm really new to java so please forgive me.

Any suggestions?

Thanks.



Wouldn't you start out by figuring out how many characters overall there were in the user input and then figure out how far over the dashes were? I'm not sure what the command would be for that. I'm a noob to Java too. Researching.

Anybody have any ideas?

Thay
Was This Post Helpful? 0
  • +
  • -

#3 SayMoi   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Extracting groups of chraracters from a string

Posted 10 April 2009 - 07:55 AM

Absolutely right, you'd want to use the dashes. Strings have a special method called indexOf(char), which is what you'll use to find the -.

Call phoneNumber.substring(0, indexOf('-') - 1). This will give you the substring from 0 to the digit before the first dash. Note the single quotes (' ')…you have to have these to find the char.

Then you would save the index of the first '-' in an int firstDash, and repeat what you did before except use firstDash + 1 for 0 in the substring. So like this: phoneNumber.substring(firstDash + 1, indexOf('-') - 1. Keep doing that for each set of numbers. Good luck! :)

Edit: My typo, sorry, each time you have "indexOf" in the substring methods it needs to be "phoneNumber.indexOf". (ie, phoneNumber.substring(0, phoneNumber.indexOf('-') - 1). Sorry if that makes it more confusing, I'm trying to keep it as simple as possible. :) )

This post has been edited by SayMoi: 10 April 2009 - 07:59 AM

Was This Post Helpful? 1
  • +
  • -

#4 javaNoob2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-February 09

Re: Extracting groups of chraracters from a string

Posted 10 April 2009 - 08:00 AM

View PostSayMoi, on 10 Apr, 2009 - 06:55 AM, said:

Absolutely right, you'd want to use the dashes. Strings have a special method called indexOf(char), which is what you'll use to find the -.


Thanks so much, SayMoi. I will try applying what you have recommended and post my code back after I create it. :)
Was This Post Helpful? 0
  • +
  • -

#5 SayMoi   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Extracting groups of chraracters from a string

Posted 10 April 2009 - 08:06 AM

No problem! :) Hope it works for you!
Was This Post Helpful? 0
  • +
  • -

#6 javaNoob2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-February 09

Re: Extracting groups of chraracters from a string

Posted 10 April 2009 - 11:31 AM

Here is what I have so far. The "area code" part of the coding makes it not run properly. When I remove all "areaCode" coding , then the program will compile and run, displaying country code and the local phone number at the last.Still not sure what to do to get the program to recognize the second dash for the area code group. I'm doing something wrong.

Any help is appreciated.

Thanks,

import java.util.Scanner;
public class PhoneNum
{
	public static void main(String[] args)
	{
	 Scanner stdIn = new Scanner(System.in);
	 String phoneNum;
	 String countryCode;
	 String areaCode;

	 System.out.println("Enter a phone number in the format " +
					 "(CC-AREA-LOCALPHONE): ");
	  phoneNum = stdIn.nextLine();

	  countryCode = (phoneNum.substring(0,phoneNum.indexOf('-')-0)); //-1 didn't work for me.
	
	  int firstDash = (phoneNum.indexOf('-')); //not sure if this is correct
				  int sep = phoneNum.LastIndexOf("-");
	 areaCode = (phoneNum.substring(firstDash + 1,phoneNum.indexOf('-')-0));

	 System.out.println("Country Code = " + countryCode);
	 System.out.println("Area Code = " + areaCode);
				 System.out.println("Local Number = " + phoneNum.substring(sep+1));//displays any digits after the last dash.	 
	  }
}


This post has been edited by javaNoob2: 10 April 2009 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#7 SayMoi   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Extracting groups of chraracters from a string

Posted 10 April 2009 - 12:50 PM

Alright, first, you can drop the -0's (sorry about that, I forgot it was exclusive of the last "to" index).

The line you marked as not sure about is good. In fact it's all perfect, except one little bug: integers don't like being made into Strings. You need to attach an empty String to your areaCode, like this:

areaCode = ("" + phoneNum.substring(firstDash + 1,phoneNum.indexOf('-')-0));



Alternatively, make areaCode an int, because you attach it to a String farther down. I think that should do it; I don't see anything else. :)

Edit: Thanks for hitting the "thanks" button, you get to be my very first one! :D

This post has been edited by SayMoi: 10 April 2009 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#8 cowguru2000   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 05-April 09

Re: Extracting groups of chraracters from a string

Posted 10 April 2009 - 01:09 PM

View PostjavaNoob2, on 10 Apr, 2009 - 06:17 AM, said:

The assignment consists of creating a program that will allow the user to input a phone number and then java extracts the country code, area code and local number and lists them as seperate components. The catch is that the instructions will not let you assume that the number of digits in particular group are fixed.

I kind of know what direction I want to go with this but am not sure how exactly to get there. The phone number format must be entered as ( CC-AREA-LOCALPHONE ). So couldn't you somehow use the dashes to help you find the groups? Maybe use the substring method? Just not sure how to start out tackeling this one. I'm really new to java so please forgive me.

Any suggestions?

Thanks.


There is a VERY simple way to do this that turns your CC-AREACODE-LOCALPHONE string into a 3-string array.

Just do this:

// assuming the input is a string named "inputString"
String[] brokenUp = [b]inputString.split("-");[/b]


brokenUp[0] will be the CC, brokenUp[1] will be the areacode, and brokenUp[2] will be the local phone number.


Good luck!
Was This Post Helpful? 1
  • +
  • -

#9 SayMoi   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: Extracting groups of chraracters from a string

Posted 10 April 2009 - 11:30 PM

Haha, what's the fun of that?? Then javaNoob doesn't get to learn how to pick out little bits of strings char by char! (No cowguru's right, noob, that would be a much easier way to approach it…) :)
Was This Post Helpful? 0
  • +
  • -

#10 javaNoob2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-February 09

Re: Extracting groups of chraracters from a string

Posted 12 April 2009 - 11:25 AM

View PostSayMoi, on 10 Apr, 2009 - 10:30 PM, said:

Haha, what's the fun of that?? Then javaNoob doesn't get to learn how to pick out little bits of strings char by char! (No cowguru's right, noob, that would be a much easier way to approach it…) :)


I seem to agree, however we haven't reached the arrays section in our class yet so I was trying to do this the other way. But i really appreciate cowguru's input. For some reason I am still unable to get the code that you recommended to work for area code.
areaCode = ("" + phoneNum.substring(firstDash + 1,phoneNum.indexOf('-')-0));

It compiles but then gives me a weird error message after entering a phone number.

"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1938)
at PhoneNum.main(PhoneNum.java:36)"

import java.util.Scanner;

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


		System.out.println("Enter a phone number (cc-area-localphone): ");
		phoneNum = stdIn.nextLine();

		countryCode = (phoneNum.substring(0,phoneNum.indexOf('-')));
		int sep = phoneNum.lastIndexOf("-");
		int firstDash = (phoneNum.indexOf("-"));

		areaCode = ("" + phoneNum.substring(firstDash + 1,phoneNum.indexOf("-")));

		System.out.println("Country Code = " + countryCode);
		System.out.println("Area Code = " + areaCode);
		System.out.println("Local Number = " + phoneNum.substring(sep+1));

	}
}




Hoping to get this figured out today. I will keep working at it, however, any input is greatly appreciated.
Thanks :)
Was This Post Helpful? 0
  • +
  • -

#11 sl4ck3r   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Extracting groups of chraracters from a string

Posted 12 April 2009 - 12:22 PM

honestly you dont even need to find the dashes.... if you know the phone number is formatted like:
417-234-3412
You know 0 through 2 are the first 3 of the number (area code) then you skip a slot for the dash and you know 4 through 6 are the local exchange. If you want to see if there is an area code or not an area code you could look at the length. kinda pointless to find the dashes if you know the input form. Also you could just string tokenize it based on dashes and that be easier to, but i assume you haven't learned that either. Split is the optimal solution though imo.
Was This Post Helpful? 0
  • +
  • -

#12 javaNoob2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-February 09

Re: Extracting groups of chraracters from a string

Posted 12 April 2009 - 01:53 PM

View Postsl4ck3r, on 12 Apr, 2009 - 11:22 AM, said:

honestly you dont even need to find the dashes.... if you know the phone number is formatted like:
417-234-3412
You know 0 through 2 are the first 3 of the number (area code) then you skip a slot for the dash and you know 4 through 6 are the local exchange. If you want to see if there is an area code or not an area code you could look at the length. kinda pointless to find the dashes if you know the input form. Also you could just string tokenize it based on dashes and that be easier to, but i assume you haven't learned that either. Split is the optimal solution though imo.



Thanks for the input sl4ck3r. The problem actually requires me to assume that no group of digits are fixed, so thats why i was bouncing off the dashes. And you're right, haven't goten into tokenize yet, lol. Your comments are greatly appreciated however.
Was This Post Helpful? 0
  • +
  • -

#13 sl4ck3r   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 285
  • Joined: 22-September 07

Re: Extracting groups of chraracters from a string

Posted 12 April 2009 - 09:58 PM

you're welcome :)
also another thought i had since you cant assume stuff about the input is you could use the replace function to replace - or other separators and remove them. Then you can count the length and that would tell you if you have just a localphone or ccareacodelocalphone
ie
4171234321 length: 10
you know its country based
1234321 length: 7
you know it is local phone only
014171234321 length: 12
you know its international (could include 11 as international as well)

I'll stop bugging you now :) lol just thought id present an alternate view then looking for dashes.

View PostjavaNoob2, on 12 Apr, 2009 - 12:53 PM, said:

View Postsl4ck3r, on 12 Apr, 2009 - 11:22 AM, said:

honestly you dont even need to find the dashes.... if you know the phone number is formatted like:
417-234-3412
You know 0 through 2 are the first 3 of the number (area code) then you skip a slot for the dash and you know 4 through 6 are the local exchange. If you want to see if there is an area code or not an area code you could look at the length. kinda pointless to find the dashes if you know the input form. Also you could just string tokenize it based on dashes and that be easier to, but i assume you haven't learned that either. Split is the optimal solution though imo.



Thanks for the input sl4ck3r. The problem actually requires me to assume that no group of digits are fixed, so thats why i was bouncing off the dashes. And you're right, haven't goten into tokenize yet, lol. Your comments are greatly appreciated however.

Was This Post Helpful? 1
  • +
  • -

#14 cowguru2000   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 05-April 09

Re: Extracting groups of chraracters from a string

Posted 14 April 2009 - 08:55 PM

View PostjavaNoob2, on 12 Apr, 2009 - 10:25 AM, said:

View PostSayMoi, on 10 Apr, 2009 - 10:30 PM, said:

Haha, what's the fun of that?? Then javaNoob doesn't get to learn how to pick out little bits of strings char by char! (No cowguru's right, noob, that would be a much easier way to approach it…) :)


I seem to agree, however we haven't reached the arrays section in our class yet so I was trying to do this the other way. But i really appreciate cowguru's input. For some reason I am still unable to get the code that you recommended to work for area code.
areaCode = ("" + phoneNum.substring(firstDash + 1,phoneNum.indexOf('-')-0));

It compiles but then gives me a weird error message after entering a phone number.

"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1938)
at PhoneNum.main(PhoneNum.java:36)"

import java.util.Scanner;

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


		System.out.println("Enter a phone number (cc-area-localphone): ");
		phoneNum = stdIn.nextLine();

		countryCode = (phoneNum.substring(0,phoneNum.indexOf('-')));
		int sep = phoneNum.lastIndexOf("-");
		int firstDash = (phoneNum.indexOf("-"));

		areaCode = ("" + phoneNum.substring(firstDash + 1,phoneNum.indexOf("-")));

		System.out.println("Country Code = " + countryCode);
		System.out.println("Area Code = " + areaCode);
		System.out.println("Local Number = " + phoneNum.substring(sep+1));

	}
}




Hoping to get this figured out today. I will keep working at it, however, any input is greatly appreciated.
Thanks :)



First of all, you never defined firstDash. If you did, phoneNum.indexOf("-") is also going to be firstDash, so you're looking for a negative substring.... which can't exist
Was This Post Helpful? 0
  • +
  • -

#15 javaNoob2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 25-February 09

Re: Extracting groups of chraracters from a string

Posted 15 April 2009 - 06:22 AM

View Postcowguru2000, on 14 Apr, 2009 - 07:55 PM, said:

First of all, you never defined firstDash. If you did, phoneNum.indexOf("-") is also going to be firstDash, so you're looking for a negative substring.... which can't exist


We haven't covered arrays yet but I've decided to go that direction as it will only be simpler to create. Hopefully my professor will be ok with me using an array. Just seems like it would make for shorter, cleaner code that way.

Thanks again for your input.

This post has been edited by javaNoob2: 15 April 2009 - 06:23 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2