Results 1 to 14 of 14
  1. #1
    ookie833 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    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;





    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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:

    Java Code:
    [code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    [/code]

  3. #3
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    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

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Better if you can deal with the regular expressions. StringTokernizers are not adviced to use.

  5. #5
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    Default

    Yeah, they're not, but it looks like the instructions want it used.
    miss.meli (-.-)zzZZ

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default

    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;
        }
    }

  8. #8
    Sidmyre is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    im a bit stumped on this one as well, I tried
    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());
    }
    but it gives me the error:
    "cannot find symbol
    symbol : constructor StringTokenizer"
    Last edited by Sidmyre; 12-12-2008 at 04:17 AM.

  9. #9
    ookie833 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    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???:confused:

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    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
    Java Code:
    StringTokenize employee=new StringTokenizer(employee[[B][COLOR="Red"]0[/COLOR][/B]]);
    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.

  11. #11
    Sidmyre is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote 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
    Java Code:
    StringTokenize employee=new StringTokenizer(employee[[B][COLOR="Red"]0[/COLOR][/B]]);
    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

    Java 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
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Employee

    OK, then shouldn't the StringTokenizer in the "for" be using the employees array?
    Java Code:
    StringTokenize employee=new StringTokenizer(employee[0]);<-[B][COLOR="Red"]should this be employees[index] ?[/COLOR][/B]
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  13. #13
    ookie833 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    Thank you!

    :)

  14. #14
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default No problem

    Welcome.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. StringTokenizer
    By carderne in forum New To Java
    Replies: 1
    Last Post: 01-26-2008, 08:19 PM
  2. How to use StringTokenizer for multiple tokens
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-29-2007, 09:38 AM
  3. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:48 AM
  4. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •