0

The program needs to count and display the number of times that the specified charector appears in the text file.

Currently turning up zero for the total. I'm not if I should be using a different loop, I've also tried using a 'for' loop.

// Hold user input and sum
String fileName;    // Holds the name of the file
String letter;      // Letter to search for in the file
int total = 0;      // Holds the total number of characters in the file


// Get the name of the file and character from the user
fileName = JOptionPane.showInputDialog("Please enter the name of a file:");
letter = JOptionPane.showInputDialog("Please enter a letter contained in the string");


// Open the file for reading
File file = new File(fileName);         
Scanner inputFile = new Scanner(file);  // Declare new scanner object for file reading


// Set accumulator to zero
int count = 0;

if (inputFile.nextLine().equalsIgnoreCase(letter)) {                                

    count++;          // add letter occurrence

    total += count;   // add the letter occurrence to the total
}
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
Kyle
  • 1,153
  • 5
  • 28
  • 56

4 Answers4

5
    BufferedReader reader = new BufferedReader(new FileReader("somefile.txt"));
    int ch;
    char charToSearch='a';
    int counter=0;
    while((ch=reader.read()) != -1) {
        if(charToSearch == (char)ch) {
            counter++;
        }
    };
    reader.close();

    System.out.println(counter);

Is this of any help?

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33
1

There is bug in your code.Correct code below-

   String fileName;    // Holds the name of the file
    String letter;      // Letter to search for in the file

    // Get the name of the file and character from the user
    fileName = "C:\\bin\\GWT.txt";
    letter = "X";


    // Open the file for reading
    File file = new File(fileName);         
    Scanner inputFile = new Scanner(file);  // Declare new scanner object for file reading


    // Set accumulator to zero
    int count = 0;
    while(inputFile.hasNext()) {
      if (inputFile.nextLine().toLowerCase().contains(letter.toLowercase())) { 
           count++;          // add letter occurrence
       }
    }
    System.out.println(count);
Jaydeep Rajput
  • 3,605
  • 17
  • 35
  • This is also case sensitive – Kyle Feb 22 '13 at 10:14
  • blnr, Edited the code above for case insensitive check. You can also refer following - http://stackoverflow.com/questions/5054995/how-to-replace-case-insensitive-literal-substrings-in-java – Jaydeep Rajput Feb 22 '13 at 10:34
0

This answer is assuming each line in your text file contains just one letter as your question suggests.

You need to wrap your if statement in a loop as currently you are only checking the first line of the file:

     while(inputFile.hasNext()) {
         if (inputFile.nextLine().equalsIgnoreCase(letter))    {                                

             count++;          // add letter occurrence

             total += count;   // add the letter occurrence to the total
         }
     }

Also you can replace:

count++;
total+= count;

with just

total++;
cowls
  • 24,013
  • 8
  • 48
  • 78
0
String line=""
while(inputFile.hasNext()) {
  line = inputFile.nextLine();
  for(int i=0; i<line.length(); i++ ){
     if (line.charAt(i)== letter)                                    
         count++;          

 }
}
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39