I have to write a program that reads the input from a text file!
Beware the Jabberwock, my son,
the jaws that bite, the claws that catch,
Beware the JubJub bird and shun
the frumious bandersnatch.
I should print the number of lines, the longest line, the number of tokens on each line and the length of the longest token on each line.
I was able to find the number of tokes in the line but im having having problems reading the longest word ,, my program gives me the number of letter of each line instead of only the number of letter of the longest word!!
can someone please help !
this should be my output:
Line 1 has 5 tokens (longest = 11)
Line 2 has 8 tokens (longest = 6)
Line 3 has 6 tokens (longest = 6)
Line 4 has 3 tokens (longest = 13)
Longest line : the jaws that bite, the claws that catch,
Quote:
import java.io.*;
import java.util.*;
public class InputStats{
public static void main (String [] args )
throws FileNotFoundException{
Scanner console = new Scanner ( System.in);
System.out.println(" ");
System.out.println();
System.out.println("Please enter a name of a file " );
String name = console.nextLine();
Scanner input = new Scanner ( new File (name));
while (input.hasNextLine()) {
String line = input.nextLine();
inputStats(new Scanner(line));
}
}//end of amin
public static void inputStats (Scanner input)
throws FileNotFoundException{
int numLines=0;
int numwords=0;
String maxLine = "";
while (input.hasNextLine()){
String next = input.nextLine();
numwords += next.split("\\s+").length;
numLines++;
if (next.length() > maxLine.length()) {
maxLine = next;
}
}//end of while
System.out.print("Line " + numLines + " has ");
System.out.print(numwords + "tokens " );
System.out.print("(longest = " + maxLine.length() + ")");
System.out.println();
}//end of method
}// end of class