Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: String Tokenizer

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default String Tokenizer

    I am trying to read a file and extract all the tokens but im having a hard time programming it correctly

    heres the file:

    Jane Rivers, A-902, 05/16/2001, 1, 16.25
    Bob Cox, S-823, 06/21/1990, 2, 17.50
    Ann Ramsey, A-715, 02/12/1998, 1, 16.25
    Joseph Chandler, P-723, 12/22/2000, 3, 14.35
    Arnold Kennedy, S-133, 08/10/1999, 2, 18.20
    Larry Huber, P-198, 02/12/2000, 3, 17.25
    Annette Wilson, H-501, 04/04/1995, 1, 20.25
    Robert Ferguson, H-674, 04/10/2002, 2, 16.50
    Ava Gaines, H-434, 01/05/2000, 3, 15.65


    and heres my code

     
    import java.io.*; 
    import java.util.StringTokenizer; 
    import java.util.Scanner;
     
     
    public class EmployeeDemo
    {
       public static void main(String[] args) throws IOException
       {
     
          String line, fileIn = "Information.txt", fileOut = "Department.txt"; 
          StringTokenizer tokens; 
          String name;
          String number;
     
          int hire;     
          int shift; 
          double pay;
     
          File fr = new File (fileIn); 
          Scanner inFile = new Scanner (fr); 
     
     
          PrintWriter outFile = new PrintWriter (fileOut); 
     
          line = inFile.nextLine(); 
          while (inFile.hasNext()) 
          { 
          tokens = new StringTokenizer(line, ",-/"); 
          name = null;
          number = null;
          hire = 0;
          shift = 0;
          pay = 0.0;
     
     
          if (tokens.hasMoreTokens()) 
             name = tokens.nextToken();
          if (tokens.hasMoreTokens()) 
             number = tokens.nextToken();
          if (tokens.hasMoreTokens())
             hire = Integer.parseInt(tokens.nextToken());
          if (tokens.hasMoreTokens())
             shift = Integer.parseInt(tokens.nextToken());
          if (tokens.hasMoreTokens()) 
          pay = Double.parseDouble(tokens.nextToken());
     
          System.out.println(name + number + hire + shift + pay);
     
          line = inFile.nextLine();
     
          }
          inFile.close();
          outFile.close();  
     
       }
     
    }
    Last edited by Freaky Chris; May 7th, 2013 at 11:46 AM. Reason: Added syntax highlighting


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: String Tokenizer

    Hi m49er704,

    Could you please outline what you have tried before this, and perhaps what is wrong with this? What does it produce? How does this differ from what you expect?

    My uniformed guess would be that you should only be splitting by a comma, not a comma, hyphen and slash

    Regards,

    Chris

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: String Tokenizer

    yes that's what I initially thought because I want each token as its own

    -name
    -employee number
    -hire date
    -shift
    -pay

    but when I get to the date it tells me this :

    Exception in thread "main" java.lang.NumberFormatException: For input string: " 05/16/2001"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:527)
    at EmployeeDemo.main(EmployeeDemo.java:42)

    --- Update ---

    should I just read it in as an integer all together? the "/" is throwing me off?

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: String Tokenizer

    Since it is a date why not read it in as a String and the parse it to a date rather than an integer?

    Have a look here Java - DateFormat

    Pay particular attention to the parse() function!

    Chris

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: String Tokenizer

    so should it look like this :

    myDate = df.parse(hire);

    --- Update ---

    ok so figured a way to read the file. Only now it wont read the very last line??

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: String Tokenizer

    Look at the way your for loop works, you check if there is another line available then process it. But it was read at the end of the previous loop. Is this the way to do it?

    This means that you read the last line don't process it, and then the while loop says oh there is no more lines, so it never gets to process it


    Chris

Similar Threads

  1. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  2. ughhh!!!! I need help (Tokenizer)
    By Jennyxoxo in forum Java SE APIs
    Replies: 1
    Last Post: December 6th, 2011, 08:38 PM
  3. [SOLVED] Confused on what tokenizer to use and how to use them.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 24
    Last Post: November 11th, 2011, 11:20 PM
  4. String Tokenizer Help
    By BuhRock in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 19th, 2011, 10:49 AM
  5. Stream Tokenizer
    By x3rubiachica3x in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2010, 01:05 PM