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 7 of 7

Thread: Java string tockenizer

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java string tockenizer

     
    import java.io.*;
    import java.util.*;
    import java.util.StringTokenizer;
     
    class read
    {
     
    	public static void main(String[]args)
    	{
          populateFilmVector();
     }
     
     
          static void populateFilmVector(){
         	String movie;
         	  Vector filmList = new Vector();
           try
           {
    	     File data       = new File("input.txt");
    	     FileReader fr = new FileReader(data);
    	     BufferedReader in = new BufferedReader(fr);
     
    	     movie = in.readLine();//read first line
    		 while(movie!=null)
    		 {
    			System.out.println(movie);
     
    			StringTokenizer tokens = new StringTokenizer(movie,",");
    			String title = tokens.nextToken();
    			String lead = tokens.nextToken();
                String length = tokens.nextToken();
    			String rating = tokens.nextToken();
    			String id = tokens.nextToken();
     
              movie = in.readLine();
     
    		 }
    		 in.close();
     
    		}
    		catch(IOException e)
    		{
    			System.out.print("Non-Existant File");
    		}
     
     
    }
     
    	}


    my code compiles and prints the text contained in the file but the tockenizer isnt working any help would be appreciated


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Java string tockenizer

    the tockenizer isnt working
    Can you expand on that? How is it not working? What is it doing which it shouldn't? What isn't it doing which it should?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java string tockenizer

    the output of my java program is

    Pain & Gain,Dwayne Jhonson,123,4.5,23415
    2br/1ba,Spencer Grammer,113,3.2,57324
    Training Day,Denzel Washington,136,5.0,23244
    Lone Surivoe,Mark Wahlberg,141,4.8,45355
    Grown ups 2,Adam Sandeler,98,4.2,13564
    Press any key to continue . . .


    where as i want it like

    Pain & Gain,
    Dwayne Jhonson,
    123,
    4.5,
    23415

    and so on for the rest of the information in the input

  4. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Java string tockenizer

     
    import java.io.*;
    import java.util.*;
    import java.util.StringTokenizer;
     
    class read
    {
     
    	public static void main(String[]args)
    	{
          populateFilmVector();
     }
     
     
          static void populateFilmVector(){
         	String movie;
         	  Vector filmList = new Vector();
           try
           {
    	     File data       = new File("input.txt");
    	     FileReader fr = new FileReader(data);
    	     BufferedReader in = new BufferedReader(fr);
     
    	     movie = in.readLine();//read first line
    		 while(movie!=null)
    		 {
    			System.out.println(movie);
     
    			StringTokenizer tokens = new StringTokenizer(movie,",");
    			String title = tokens.nextToken();
    			String lead = tokens.nextToken();
                String length = tokens.nextToken();
    			String rating = tokens.nextToken();
    			String id = tokens.nextToken();
     
              movie = in.readLine();
     
    		 }
    		 in.close();
     
    		}
    		catch(IOException e)
    		{
    			System.out.print("Non-Existant File");
    		}
     
     
    }
     
    	}


    my code compiles and prints the text contained in the file but the tockenizer isnt working any help would be appreciated


    text file contents
    {
    Pain & Gain,Dwayne Jhonson,123,4.5,23415
    2br/1ba,Spencer Grammer,113,3.2,57324
    Training Day,Denzel Washington,136,5.0,23244
    Lone Surivoe,Mark Wahlberg,141,4.8,45355
    Grown ups 2,Adam Sandeler,98,4.2,13564
    }

    --- Update ---

    {
    Pain & Gain,Dwayne Jhonson,123,4.5,23415
    2br/1ba,Spencer Grammer,113,3.2,57324
    Training Day,Denzel Washington,136,5.0,23244
    Lone Surivoe,Mark Wahlberg,141,4.8,45355
    Grown ups 2,Adam Sandeler,98,4.2,13564
    }

    I woluld like the out put to be

    {
    Pain & Gain,
    Dwayne Jhonson,
    123,
    4.5,
    23415

    and so on as aposed to what is above which is what im getting now

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Java string tockenizer

    You are printing out the line before using the String tokenizer on it, and then you are not doing anything with the parts that you get from the tokenizer.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    dubs4sam (April 16th, 2014)

  7. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java string tockenizer

    thanks

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java string tockenizer

    Please keep all discussion about this issue in this thread rather than creating new threads - I have merged both thread topics into this one.

Similar Threads

  1. String in java
    By ragunath90 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 5th, 2014, 11:43 AM
  2. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  3. java.lang.String cannot be cast to java.util.Hashtable
    By ashin12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 1st, 2012, 06:17 AM
  4. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM