Results 1 to 6 of 6
Thread: string.split help
- 03-05-2012, 04:03 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
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"); } }
- 03-05-2012, 04:10 PM #2
Re: string.split help
Why do they call it rush hour when nothing moves? - Robin Williams
- 03-05-2012, 06:54 PM #3
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 06:58 PM.
- 03-05-2012, 07:21 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 31
- Rep Power
- 0
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 07:34 PM.
- 03-05-2012, 10:38 PM #5
Re: string.split help
Like, not incrementing the counter variable?Something wrong in the for-loop maybe?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-06-2012, 05:37 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 04:08 PM -
String split
By soccer_kid_6 in forum New To JavaReplies: 3Last Post: 10-29-2010, 07:51 PM -
Using String Split
By JonJacobs in forum New To JavaReplies: 5Last Post: 07-27-2010, 07:43 PM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks