1

I reccently got some homework which has me doing this weird task. The teacher wants us to split various sentences into words. The teacher has put these in a file which was imported through the scanner.

The teacher wants us to then with these words, to count the length, the number of words should increase with each iteration of the loop along with the number of words.

The file always ends with the character "#" so that is where i begun.

Here is what i have so far

  class Assignmentfive
  {
 private static final String String = null;

 public static void main(String[] args) throws FileNotFoundException
 {
    Scanner scan = new Scanner(new File("asgn5data.txt"));

    String fileRead = " ";
    System.out.print(fileRead);
    double educationLevel = 0;
    double wordCount = 0;

   while (fileRead != "#")
   {
     fileRead = scan.nextLine();    

     int firstIndex = fileRead.indexOf(" ");
     String strA = fileRead.substring(0,firstIndex);
     System.out.print(strA);
     int strLength = strA.length();
     wordCount++;   
     }

Now, There is more at the bottom, that is my calculations, I cannot figure out how to extract word for word from the file

Any tips?

Thanks``

Jon Roy
  • 53
  • 1
  • 11
  • you're on the right track. You just need to do more with 'fileRead'. After you find the first word in a line, you need to check for more in the same line. – Cruncher Nov 02 '15 at 23:08
  • 2
    `fileRead != "#"` -> [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Nov 02 '15 at 23:09

1 Answers1

0

Never test String equality with == (that's reference identity, not value identity with Object types you want .equals). You can use the Scanner(String) constructor which Constructs a new Scanner that produces values scanned from the specified string. Also, you never closed your Scanner (backed by a File, that's a resource leak). You can explicitly call close, but I'd prefer a try-with-resources Statement. Something like,

try (Scanner scan = new Scanner(new File("asgn5data.txt"))) {
  int wordCount = 0;
  while (true) {
    String fileRead = scan.nextLine();
    if (fileRead.equals("#")) {
      break;
    }
    Scanner wordScanner = new Scanner(fileRead);
    while (wordScanner.hasNext()) {
      String word = wordScanner.next();
      System.out.println(word);
      int wordLength = word.length();
      wordCount++;
    }
  }
} catch (Exception e) {
  e.printStackTrace();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Hi man! Thanks! is there any possible way to make this go through the lines one at a time? There are different lines in the file. – Jon Roy Nov 02 '15 at 23:34
  • @Jon This does go through the lines one at a time. Then it goes through each word in the line one at a time. – Elliott Frisch Nov 03 '15 at 00:02
  • Yes, however im looking at it sentence by sentence, Like sentence one, Get all of the words average lengths, Then move onto sentence number two, and ideas? – Jon Roy Nov 03 '15 at 00:04