Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-11-2008, 02:41 AM
Member
 
Join Date: Nov 2008
Posts: 9
Rep Power: 0
ookie833 is on a distinguished road
Default 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;





}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-11-2008, 03:10 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,195
Rep Power: 5
Fubarable is on a distinguished road
Default
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]
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-11-2008, 06:33 AM
miss.meli's Avatar
Member
 
Join Date: Nov 2008
Location: Austin, TX
Posts: 17
Rep Power: 0
miss.meli is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-11-2008, 06:51 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-11-2008, 07:01 AM
miss.meli's Avatar
Member
 
Join Date: Nov 2008
Location: Austin, TX
Posts: 17
Rep Power: 0
miss.meli is on a distinguished road
Default
Yeah, they're not, but it looks like the instructions want it used.
__________________
miss.meli (-.-)zzZZ
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-11-2008, 07:03 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,525
Rep Power: 9
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-11-2008, 10:53 AM
Member
 
Join Date: Dec 2008
Location: Enschede, Overijssel, the Netherlands
Posts: 19
Rep Power: 0
Sven is on a distinguished road
Default
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;
    }
}
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-12-2008, 04:15 AM
Member
 
Join Date: Nov 2008
Posts: 7
Rep Power: 0
Sidmyre is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-12-2008, 06:13 AM
Member
 
Join Date: Nov 2008
Posts: 9
Rep Power: 0
ookie833 is on a distinguished road
Default 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???
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-12-2008, 07:49 AM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
CJSLMAN is on a distinguished road
Default 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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-12-2008, 04:03 PM
Member
 
Join Date: Nov 2008
Posts: 7
Rep Power: 0
Sidmyre is on a distinguished road
Default
Originally Posted by CJSLMAN View Post
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"};
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-12-2008, 04:50 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
CJSLMAN is on a distinguished road
Default 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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 12-13-2008, 07:14 PM
Member
 
Join Date: Nov 2008
Posts: 9
Rep Power: 0
ookie833 is on a distinguished road
Default
Thank you!

Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 12-14-2008, 04:09 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
CJSLMAN is on a distinguished road
Default No problem
Welcome.

Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
StringTokenizer carderne New To Java 1 01-26-2008 08:19 PM
Performance Issues (StringTokenizer) JavaForums Java Blogs 0 11-29-2007 01:22 PM
How to use StringTokenizer for multiple tokens javaplus New To Java 2 11-29-2007 09:38 AM
StringTokenizer Java Tip Java Tips 0 11-08-2007 08:48 AM
StringTokenizer Java Tip Java Tips 0 11-03-2007 09:24 PM


All times are GMT +2. The time now is 01:56 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org