finding the amt of words and chars in a give file
So my code compiles fine, but I get a run-time error
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
at Hw13.main(Hw13.java:36)
Code :
import java.util.*;
import java.io.*;
class Main
{
public static void main ( String [] args ) throws Exception
{
Scanner kb = new Scanner ( System.in );
System.out.print("Filename? ");
String filename = kb.nextLine();
Scanner sc = new Scanner ( new File ( filename ));
StringTokenizer strTokenizer = new StringTokenizer(filename,".");
int chars = 0;
int words = 0;
int charA = 0;
int the = 0;
while ( sc.hasNext())
{
String s = sc.next();
words++;
}
while ( strTokenizer.hasMoreTokens())
{
if ( strTokenizer.nextToken().equalsIgnoreCase("the")) the++;
if ( strTokenizer.nextToken().equalsIgnoreCase("a") ||
strTokenizer.nextToken().equalsIgnoreCase("an")) charA++;
strTokenizer.nextToken(); chars++;
}
System.out.println("chars " + chars);
System.out.println("words " + words);
}
}
What's wrong with my code?
Re: finding the amt of words and chars in a give file
Each time around the loop you read multiple tokens (3 or 4). If you only have 1 token it is read by the first if statement. Then there are no more tokens and when the second if statement tries to read a token the exception is thrown. Perhaps you should only read 1 token each time around the loop and store it in a variable.
Re: finding the amt of words and chars in a give file
how would i go about putting the strings read into an array?
Re: finding the amt of words and chars in a give file
Simple. Read a String, insert into array. Rinse and repeat.
Re: finding the amt of words and chars in a give file
Ok I've redone the code but I have another issue. How do I read more than one line?
Code :
import java.io.*;
import java.util.*;
import java.util.StringTokenizer;
class Main
{
public static void main ( String [] args ) throws Exception
{
Scanner kb = new Scanner ( System.in );
System.out.print("Filename? ");
String filename = kb.nextLine();
Scanner sc = new Scanner ( new File ( filename ));
int chars = 0;
int words = 0;
int charA = 0;
int the = 0;
String str = sc.nextLine();
StringTokenizer strTokenizer = new StringTokenizer (str," ,.");
while ( strTokenizer.hasMoreTokens())
{
String s = strTokenizer.nextToken();
if ( s.equalsIgnoreCase("the")) the++;
if ( s.equalsIgnoreCase("a") || s.equalsIgnoreCase("an")) charA++;
words++;
chars+=s.length();
System.out.println(s);
}
System.out.println("chars " + chars);
System.out.println("words " + words);
System.out.printf("average word length %3.2f\n",(1.0*chars/words));
System.out.printf("a/an %2d frequency %4.3f\n", charA,(1.0*charA/words));
System.out.printf("the %2d frequency %4.3f", the, (1.0*the/words));
}
}
Re: finding the amt of words and chars in a give file
Depends upon what you are trying to do. One option:
var1 = read line
var2 = read line
do stuff with var1 and var2
repeat if needed.
Note that you are going to run into problems if the amount of input is odd.