Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 11th, 2006, 6:27 PM   #1
dchankhour
Newbie
 
Join Date: Jan 2006
Location: Poway, CA
Posts: 13
Rep Power: 0 dchankhour is on a distinguished road
parsing a string

i'm trying to pase a string into three integers that are seperated by the integers. then i'm taking the three integers and passing them into a ssn constructor that would create a ssn object. but my problem is how could i parse the string that in this form ###-##-####.
string ssn;
i used int s = Integer.parseInt(ss), but how would i take care for the dashes.
dchankhour is offline   Reply With Quote
Old Feb 11th, 2006, 7:39 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
I haven't run this through a compiler, but this should do the trick:

String [] ssn = ss.split("-");
int [] nums = new int[3];

for(int i = 0; i<3; i++)
{
	 nums[i] = Integer.parseInt(ssn[i]);
}
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 12th, 2006, 12:30 AM   #3
dchankhour
Newbie
 
Join Date: Jan 2006
Location: Poway, CA
Posts: 13
Rep Power: 0 dchankhour is on a distinguished road
but how could i get three integers so i could pass them through my constructor
dchankhour is offline   Reply With Quote
Old Feb 12th, 2006, 12:41 AM   #4
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
Using the code above, you could modify your constructor to take an int array, or you could pass the three ints like this:
Ssn s = new Ssn(nums[0], nums[1], nums[2]);
I think this is a cumbersome way to pass the numbers, though.
jaeusm is offline   Reply With Quote
Old Feb 12th, 2006, 12:42 AM   #5
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
damn .. a few seccond too late with my response :p
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Feb 12th, 2006, 1:19 AM   #6
dchankhour
Newbie
 
Join Date: Jan 2006
Location: Poway, CA
Posts: 13
Rep Power: 0 dchankhour is on a distinguished road
i tried that but it did not work, and that is because, the method is suppose to pass a string, not an array of string.
String [] ssn = ss.split("-");
int [] nums = new int[3];

for(int i = 0; i<3; i++)
{
	 nums[i] = Integer.parseInt(ssn[i]);
}
[/quote]
i have tried this, but still did not work.
num = Integer.parsInt(ssn);
num2= integer.parsInt(ssn);
but could i pass parseInt another paramter, telling it where to stop.
dchankhour is offline   Reply With Quote
Old Feb 12th, 2006, 1:24 AM   #7
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Quote:
num = Integer.parsInt(ssn);
You have to do it like : num = Integer.parseInt(ssn[0]);
then ssn[1] and ssn[2].
you can't parse the entire array , you have to do it element by element.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Feb 12th, 2006, 1:39 AM   #8
dchankhour
Newbie
 
Join Date: Jan 2006
Location: Poway, CA
Posts: 13
Rep Power: 0 dchankhour is on a distinguished road
is there a way i could do it with out using arrays, and just using parseInt
dchankhour is offline   Reply With Quote
Old Feb 12th, 2006, 4:18 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by dchankhour
is there a way i could do it with out using arrays, and just using parseInt
Why? What would be the point? If you tried to do it with three variables, the code would wind up being a lot more complicated (as you couldn't use split() or for loops). I suppose you could use a List instead, but there wouldn't be any point.

If you wish, you could take the array variables and convert them into named variables:
first = num[1];
second = num[2];
third = num[3];
But the point of doing this eludes me.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC