16 Replies - 2351 Views - Last Post: 16 April 2009 - 08:05 PM
#1
Extracting groups of chraracters from a string
Posted 10 April 2009 - 07:17 AM
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.
Replies To: Extracting groups of chraracters from a string
#2
Re: Extracting groups of chraracters from a string
Posted 10 April 2009 - 07:49 AM
javaNoob2, on 10 Apr, 2009 - 06:17 AM, said:
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
#3
Re: Extracting groups of chraracters from a string
Posted 10 April 2009 - 07:55 AM
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
#4
Re: Extracting groups of chraracters from a string
Posted 10 April 2009 - 08:00 AM
#5
Re: Extracting groups of chraracters from a string
Posted 10 April 2009 - 08:06 AM
#6
Re: Extracting groups of chraracters from a string
Posted 10 April 2009 - 11:31 AM
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
#7
Re: Extracting groups of chraracters from a string
Posted 10 April 2009 - 12:50 PM
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!
This post has been edited by SayMoi: 10 April 2009 - 12:52 PM
#8
Re: Extracting groups of chraracters from a string
Posted 10 April 2009 - 01:09 PM
javaNoob2, on 10 Apr, 2009 - 06:17 AM, said:
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!
#9
Re: Extracting groups of chraracters from a string
Posted 10 April 2009 - 11:30 PM
#10
Re: Extracting groups of chraracters from a string
Posted 12 April 2009 - 11:25 AM
SayMoi, on 10 Apr, 2009 - 10:30 PM, said:
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
#11
Re: Extracting groups of chraracters from a string
Posted 12 April 2009 - 12:22 PM
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.
#12
Re: Extracting groups of chraracters from a string
Posted 12 April 2009 - 01:53 PM
sl4ck3r, on 12 Apr, 2009 - 11:22 AM, said:
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.
#13
Re: Extracting groups of chraracters from a string
Posted 12 April 2009 - 09:58 PM
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
javaNoob2, on 12 Apr, 2009 - 12:53 PM, said:
sl4ck3r, on 12 Apr, 2009 - 11:22 AM, said:
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.
#14
Re: Extracting groups of chraracters from a string
Posted 14 April 2009 - 08:55 PM
javaNoob2, on 12 Apr, 2009 - 10:25 AM, said:
SayMoi, on 10 Apr, 2009 - 10:30 PM, said:
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
#15
Re: Extracting groups of chraracters from a string
Posted 15 April 2009 - 06:22 AM
cowguru2000, on 14 Apr, 2009 - 07:55 PM, said:
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

New Topic/Question
This topic is locked



MultiQuote



|