![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Location: Poway, CA
Posts: 13
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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]);
}
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2006
Location: Poway, CA
Posts: 13
Rep Power: 0
![]() |
but how could i get three integers so i could pass them through my constructor
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
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]); |
|
|
|
|
|
#5 |
|
Professional Programmer
|
damn .. a few seccond too late with my response :p
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jan 2006
Location: Poway, CA
Posts: 13
Rep Power: 0
![]() |
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]);
}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. |
|
|
|
|
|
#7 | |
|
Professional Programmer
|
Quote:
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 ! |
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jan 2006
Location: Poway, CA
Posts: 13
Rep Power: 0
![]() |
is there a way i could do it with out using arrays, and just using parseInt
|
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
If you wish, you could take the array variables and convert them into named variables: first = num[1]; second = num[2]; third = num[3]; |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|