Reference no: EM13165734
a java program where im supposed to print the number of lines the number of words and the longest line in a text file!
i just need help printing the longest line of my text...
and with the number of lines.. my keep printin 1 in each line :
insted of 1, 2, 3, 4 the number of words and the number of letter in the longest word are ok !
Beware the Jabberwock, my son,
the jaws that bite, the claws that catch,
Beware the JubJub bird and shun
the frumious bandersnatch.
my output should be
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,
im getting :
Line 1 has 5 tokens (longest = 11)
Line 1 has 8 tokens (longest = 6)
Line 1 has 6 tokens (longest = 6)
Line 1 has 3 tokens (longest = 13)
This is my cod e
import java.io.*;
import java.util.*;
public class InputS{
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;
int letter=0;
String maxword = "";
String maxLine= "";
while (input.hasNext()){
String next = input.next();// number of tokes
numwords++;
if (next.length() > maxword.length()) {// longest word
maxword = next;
}
}//end of while
while ( input.hasNextLine()){// line numbers
String Lines =input.nextLine();
numLines++;
}
System.out.print("Line " + numLines + " has ");
System.out.print( numwords + " tokens " );
System.out.print("(longest = " + maxword.length()+ ")");
System.out.println();
}//end of method
}// end of class