Results 1 to 6 of 6
  1. #1
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default string.split help

    Hi

    I am writing a program that will read a file, and then count the number of words, vowels, and the average number of words per line. So far, whenever I run the program, it reads the first line, then freezes. I read on another thread that this is caused by the tokenizer, and that it should be replaced by the string split statement. The only issue is that I don't know how to write a string split statement, or how to use it. Could someone help?

    Thanks,
    name

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class file_reader3
    {
    	public static void main(String[] args) throws IOException
    	{
    		//Start input stream
    		BufferedReader in;
    		in = new BufferedReader(new InputStreamReader(System.in));
    		
    		//Greet user
    		System.out.println(" ");
    		System.out.println("Welcome to File Reader!");
    		System.out.println(" ");
    		
    		//Ask for file
    		System.out.println("Please Insert File Location");
    		String file = in.readLine();
    		System.out.println(" ");
    		
    		System.out.println("*[Reading...]*");
    		System.out.println(" ");
    		
    		String line;
    		in = new BufferedReader(new FileReader(file));
    		line = in.readLine();
    		
    		//find length
    		int length = 10;
    		int i = 0;
    		int j;
    		
    		//Word and vowels count
    		int countw=0;
    		int countv=0;
    		
    		//Read file
    		while (line != null)
    		{
    			System.out.println(line);
    			
    			//find words
    			StringTokenizer stk=new StringTokenizer(line," ");
    			while(stk.hasMoreTokens())
    			{
    				String token=stk.nextToken();
    				countw++;
    			}
    			
    			//find vowels
    			for (j = 0; j < line.length(); i++) 
    			{
    				char c = line.charAt(j);
    				if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') 
    				{
    					countv++;
    				}
    			}
    			line = in.readLine();
    		}
    	System.out.println(" ");
    	System.out.println("*[End of File]*");
    	System.out.println(" ");
    	System.out.println("There are " + countw + " words in the text");
    	System.out.println("There are " + countv + " vowels in the text");
    	
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: string.split help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: string.split help

    Try debugging the code by adding println statements to show what was read and what tokens were returned.
    The print out should show you what the code is doing.

    What do you mean by "freezes"? Is the program looping or ?
    Last edited by Norm; 03-05-2012 at 07:58 PM.

  4. #4
    JeffThomas is offline Member
    Join Date
    Dec 2011
    Posts
    31
    Rep Power
    0

    Default Re: string.split help

    I just tested your code, and took norms suggestion of testing println statements in certain areas (very useful in many situations).

    I'll tell you that your code freezes after line 59. Something wrong in the for-loop maybe?

    If you comment out the for loop, you will realize that the entire program works from then on. Just re-do the vowel detection.
    Last edited by JeffThomas; 03-05-2012 at 08:34 PM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: string.split help

    Something wrong in the for-loop maybe?
    Like, not incrementing the counter variable?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: string.split help

    I solved it by making a new program. thanks for your help.

Similar Threads

  1. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 05:08 PM
  2. String split
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 10-29-2010, 08:51 PM
  3. Using String Split
    By JonJacobs in forum New To Java
    Replies: 5
    Last Post: 07-27-2010, 08:43 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 09:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:32 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
  •