java - Need to use Scanner to read a file but don't know how to compare -
i trying write method reads text file using scanner compare them see if characters ('a' - 'z') binary operators can't used (compilation error). ideas how work around it?
i need convert uppercase letters lowercase, , have counter keep track of how many times each letter appeared in text file.
i need ignore symbols , numbers in text file.
after reading comments, changed code into:
import java.util.scanner; public class letterinventory { int counter = 0; private int[] inventory; char[] alphabet = "abcdefghijklmnopqrstuvwxyz".tochararray(); public letterinventory () { inventory = new int[26]; } public void countoccurrences(scanner file) { while (file.hasnextline()) { // read line line , make lowercase string line = file.nextline().tolowercase(); // character of line (char c :line.tochararray()) { if (c >= 'a' && c <= 'z'){ // check character counter++; } } } } public void displaytable () { (int = 0; < alphabet.length; i++) { system.out.println(alphabet[i] + ": " + inventory[i]); } } public void resetinventory () { counter = 0; }
i still not sure how make thing work. program supposed able read text file, make count each alphabet read, ignore symbol/number, , output table each letter followed how many times in text file.
as pointed out in comments there problems code. first: every time call file.next() try read next character. in loop is: read characters, convert lower case ignore new value , carry on.
the compilation problem due fact try compare string character.
what want this:
while(file.hasnext()) { string currenttokes = file.next().tolowercase(); //save current token lower text in variable //check each character of string for(char c : currenttoken.tochararray()) { if(c <= ....) //check etc. } }
another way use regular expressions.
Comments
Post a Comment