
12-11-2008, 02:41 AM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 9
Rep Power: 0
|
|
Help with StringTokenizer!
Instructions: Use a loop to process each of the strings in the array and display the last name for each employee followed by a comma, followed by the first name, followed by the weekly pay (see example below. To accomplish this, consider what you must do each time the loop repeats, Each repetition should process the next string in the array, using a StringTokenizer instance that to parse the current array element based on the colon. After extracting the four values from the string. You will need to convert the hours worked to an int and the hourly wage to a double before calculating the weekly wage. Here is how your output should appear:
John Smith: $446.25
any hints greatly appreciated!!
My Code:
import java.text.*;
import java.util.*;
import java.util.StringTokenizer;
public class Exam2_3
{
public static void main (String [] args)
{
DecimalFormat currency = new DecimalFormat("$#,##0.00");
String [] employees = {"John:Smith:35:12.75",
"Mary:King:20:22.75",
"Chris:Kennedy:40:15.75",
"Angela:Jones:35:10.75",
"Mark:Smith:35:18.75"};
String firstName, lastName;
int hoursWorked;
double hourlyRate, weeklyPay;
}
}
|
|

12-11-2008, 03:10 AM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 3,195
Rep Power: 5
|
|
hint 1: show us your first attempt. It's much easier knowing what mistakes in code and mistakes in assumptions you are making if we can see what you've tried.
Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:
|
Code:
|
[code]
// your code block goes here.
// note the differences between the tag at the top vs the bottom.
[/code] |
|
|

12-11-2008, 06:33 AM
|
 |
Member
|
|
Join Date: Nov 2008
Location: Austin, TX
Posts: 17
Rep Power: 0
|
|
|
google search StringTokenizer in java and click the Java 2 Platform SE v1.4.2 page (heh, I don't have enough posts to put the link here myself)
Once you understand what StringTokenizer can do for you, use it to grab the information you want from each String in the Array.
__________________
miss.meli (-.-)zzZZ
|
|

12-11-2008, 06:51 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
|
|
|
Better if you can deal with the regular expressions. StringTokernizers are not adviced to use.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

12-11-2008, 07:01 AM
|
 |
Member
|
|
Join Date: Nov 2008
Location: Austin, TX
Posts: 17
Rep Power: 0
|
|
|
Yeah, they're not, but it looks like the instructions want it used.
__________________
miss.meli (-.-)zzZZ
|
|

12-11-2008, 07:03 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
|
|
|
I think same thing is possible using regular expressions as well.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

12-11-2008, 10:53 AM
|
|
Member
|
|
Join Date: Dec 2008
Location: Enschede, Overijssel, the Netherlands
Posts: 19
Rep Power: 0
|
|
With code tags:
|
Code:
|
import java.text.*;
import java.util.*;
import java.util.StringTokenizer;
public class Exam2_3
{
public static void main (String [] args)
{
DecimalFormat currency = new DecimalFormat("$#,##0.00");
String [] employees = {
"John:Smith:35:12.75",
"Mary:King:20:22.75",
"Chris:Kennedy:40:15.75",
"Angela:Jones:35:10.75",
"Mark:Smith:35:18.75" };
String firstName, lastName;
int hoursWorked;
double hourlyRate, weeklyPay;
}
} |
|
|

12-12-2008, 04:15 AM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 7
Rep Power: 0
|
|
im a bit stumped on this one as well, I tried
|
Code:
|
StringTokenizer timeSheet = new StringTokenizer(employees, ":", true);
firstName = timeSheet.nextToken();
lastName = timeSheet.nextToken();
hoursWorked = integer.parseInt(hoursWorked);
hourlyRate = Double.parseDouble(hourlyRate);
weeklyPay = Double.parseDouble(weeklyPay);
while (timeSheet.hasMoreTokens())
{
System.out.println(timeSheet.nextToken());
} |
but it gives me the error:
"cannot find symbol
symbol : constructor StringTokenizer"
Last edited by Sidmyre; 12-12-2008 at 04:17 AM.
|
|

12-12-2008, 06:13 AM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 9
Rep Power: 0
|
|
hmm...
ok I can parse the array with the StringTokenizer.
But now I am stumped on how to iterate through the array with a loop.
This is my code but it only displays the first string 5 times instead of all five strings.
I added to more variables...
String strHoursWorked;
String strHourlyRate;
for (int index=0; index < employees.length; index++)
{
StringTokenize employee=new StringTokenizer(employee[0]);
firstName=(employee.nextToken(":");
lastName=(employee.nextToken(":");
strHoursWorked=(employee.nextToken(":");
strHourlyRate=(employee.nextToken(":");
hoursWorked=Integer.parseInt(strHoursWorked);
hourlyRate=Double.parseDouble(strHourlyRate);
weeklyPay=hoursWorked * hourlyRate;
}
System.out.println(firstName + " " + lastName + ":" + currency.format(weeklyPay));
Any ideas on what I am doing wrong with this loop???
|
|

12-12-2008, 07:49 AM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
|
|
Asking the wrong thing
You're getting the first employee because your're asking for the first employee everytime you go through the loop
|
Code:
|
StringTokenize employee=new StringTokenizer(employee[0]); |
Try replacing the "0" with the "index" variable.
Also, look at the "for" statement: - is it index < employees.length ? or
- is it index < employee.length?
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
|
|

12-12-2008, 04:03 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 7
Rep Power: 0
|
|
Originally Posted by CJSLMAN
|
You're getting the first employee because your're asking for the first employee everytime you go through the loop
|
Code:
|
StringTokenize employee=new StringTokenizer(employee[0]); |
Try replacing the "0" with the "index" variable.
Also, look at the "for" statement: - is it index < employees.length ? or
- is it index < employee.length?
Luck,
CJSL
|
its employees
|
Code:
|
String [] employees = {"John:Smith:35:12.75",
"Mary:King:20:22.75",
"Chris:Kennedy:40:15.75",
"Angela:Jones:35:10.75",
"Mark:Smith:35:18.75"}; |
|
|

12-12-2008, 04:50 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
|
|
Employee
OK, then shouldn't the StringTokenizer in the "for" be using the employees array?
|
Code:
|
StringTokenize employee=new StringTokenizer(employee[0]);<-should this be employees[index] ? |
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
|
|

12-13-2008, 07:14 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 9
Rep Power: 0
|
|
Thank you!
|
|

12-14-2008, 04:09 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
|
|
No problem
Welcome.
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 01:56 AM.
|
|