1

Just would like to say this program has defeated me in almost everyway. What I need to do is input a .txt file which contains this

There should be 59 characters 13 words and 6 lines in this file.

dont know how to format that to 6 lines.

The program counts the lines but says there are 78 words in the text file, not 13. Which is 6*13 = 78.

How can I get it to only read the line once.. hence reading 13 words not 13*6. The program seems to be counting the words that many times. (comments are put in for guidance by the proffesor).

package inputoutput;

import java.util.*;
import java.io.*;

public class input {

public static void main(String[] args) throws FileNotFoundException {
    Scanner s = new Scanner(System.in);
    String name;
    int lineCount = 0;
    int wordCount = 0;


    System.out.println("Please type the file you want to read in: ");
    name = s.nextLine();



    // create a Scanner object to read the actual text file
    File input = new File("C:\\Users\\Ceri\\workspace1\\inputoutput\\src\\inputoutput\\" + name + ".txt");
    Scanner in = new Scanner(input);


    while (in.hasNextLine()){ // enter while loop if there is a complete line available

        // increase line counter variable
        lineCount++;


        // read in next line using Scanner object, inFile
        in.nextLine();

        // create another Scanner object in order to scan the line word by word
        Scanner word = new Scanner(input); 


        // use while loop to scan individual words, increase word counter
        while(word.hasNext()){
            // increase word counter
            wordCount++;    
            // Scanner move to next word
            word.next();
        }
        word.close();
        // count number of chars including space but not line break by using the length() method of the String class and increment character counter

    }

    in.close();
    System.out.print("Lines " + lineCount + " "+ "Words " +  wordCount);

}
ceri Westcott
  • 45
  • 2
  • 8
  • I guess using two scanners for `input` is creating problem . You can do this : take the. String oneline = in.nextLine . Use your scanner on that word in the same way – frunkad Feb 11 '15 at 19:31
  • You need to move your word scanner outside of the while loop for the line scanner. As you have it right now you're declaring the word scanner and scanning the whole file for each line in the file. – mstbaum Feb 11 '15 at 19:31
  • @mstbaum you are my saviour. I wanted to bloody rip my hair out doing this. – ceri Westcott Feb 11 '15 at 19:33
  • Another approach would be to take in the whole file as a string and use regular expressions to find words and lines very easily. – AndrewB Feb 11 '15 at 19:35

3 Answers3

2

In your code, you have the following:

// read in next line using Scanner object, inFile
in.nextLine();

// create another Scanner object in order to scan the line word by word
Scanner word = new Scanner(input); 

instead of that, change to this:

// read in next line using Scanner object, inFile
String nextline = in.nextLine();

// create another Scanner object in order to scan the line word by
// word
Scanner word = new Scanner(nextline);
Ascalonian
  • 14,409
  • 18
  • 71
  • 103
vianna77
  • 495
  • 1
  • 7
  • 17
1

Check it out : How to use multiple Scanner objects on System.in?

What you shall do : take one line from first scanner then create a new Scanner with that line

String s = in.nextLine();
Scanner sc = new Scanner(s);

Now iterate in the same manner as you second loop

Community
  • 1
  • 1
frunkad
  • 2,433
  • 1
  • 23
  • 35
-1

Ready and tested.

public class input {

 public static void main(String[] args) throws FileNotFoundException {
   Scanner s = new Scanner(System.in);
   String name;
   int lineCount = 0;
    int wordCount = 0;


System.out.println("Please type the file you want to read in: ");
name = s.nextLine();



  // create a Scanner object to read the actual text file
File input = new File("C:\\Users\\Ceri\\workspace1\\inputoutput \\src\\inputoutput\\" + name + ".txt");
s.close();

//--------------------
   //Count how many lines
Scanner in = new Scanner(input);
while (in.hasNextLine()){ 

    // increase line counter variable
    ++lineCount;
    in.nextLine();
} 
in.close(); //Close [in] Scanner



//------------------------------    
 //Count how many words
Scanner word = new Scanner(input); 
while(word.hasNext()){

 // increase word counter variable
    ++wordCount;
    word.next();
}
 word.close(); //close [word] Scanner



System.out.print("Lines " + lineCount + " "+ "Words " +  wordCount);

 }//end of main Method


}//end of Class

Make a Comment [if] it doesn't work for you.

crAlexander
  • 376
  • 1
  • 2
  • 12