Results 1 to 14 of 14
Thread: Help with StringTokenizer!
- 12-11-2008, 02:41 AM #1
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;
}
}
-
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:
Java 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 #3
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 #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Better if you can deal with the regular expressions. StringTokernizers are not adviced to use.
- 12-11-2008, 07:01 AM #5
Yeah, they're not, but it looks like the instructions want it used.
miss.meli (-.-)zzZZ
- 12-11-2008, 07:03 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I think same thing is possible using regular expressions as well.
- 12-11-2008, 10:53 AM #7
Member
- Join Date
- Dec 2008
- Location
- Enschede, Overijssel, the Netherlands
- Posts
- 19
- Rep Power
- 0
With code tags:
Java 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 #8
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
im a bit stumped on this one as well, I tried
but it gives me the error:Java 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()); }
"cannot find symbol
symbol : constructor StringTokenizer"Last edited by Sidmyre; 12-12-2008 at 04:17 AM.
- 12-12-2008, 06:13 AM #9
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???:confused:
- 12-12-2008, 07:49 AM #10
Asking the wrong thing
You're getting the first employee because your're asking for the first employee everytime you go through the loop
Try replacing the "0" with the "index" variable.Java Code:StringTokenize employee=new StringTokenizer(employee[[B][COLOR="Red"]0[/COLOR][/B]]);
Also, look at the "for" statement:
- is it index < employees.length ? or
- is it index < employee.length?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-12-2008, 04:03 PM #11
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
- 12-12-2008, 04:50 PM #12
Employee
OK, then shouldn't the StringTokenizer in the "for" be using the employees array?
Luck,Java Code:StringTokenize employee=new StringTokenizer(employee[0]);<-[B][COLOR="Red"]should this be employees[index] ?[/COLOR][/B]
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-13-2008, 07:14 PM #13
Member
- Join Date
- Nov 2008
- Posts
- 9
- Rep Power
- 0
Thank you!
:)
- 12-14-2008, 04:09 PM #14
Similar Threads
-
StringTokenizer
By carderne in forum New To JavaReplies: 1Last Post: 01-26-2008, 08:19 PM -
How to use StringTokenizer for multiple tokens
By javaplus in forum New To JavaReplies: 2Last Post: 11-29-2007, 09:38 AM -
StringTokenizer
By Java Tip in forum Java TipReplies: 0Last Post: 11-08-2007, 08:48 AM -
StringTokenizer
By Java Tip in forum Java TipReplies: 0Last Post: 11-03-2007, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks