dimanche 7 décembre 2014

Create word-count application that accounts for multiple lines of text in an input file


Vote count:

0





public static void main(String[] args) throws IOException {
String dir = "/Volumes/TRAVELDRIVE/2014-15 Programs/Chapter06/build/classes";// folder of input
Scanner in = new Scanner(new FileReader(dir + "/input5.txt"));//file read
BufferedReader is = new BufferedReader(new FileReader(dir + "/input5.txt"));
int wordcount = 0;//counter

int i = 0;//each character of input
boolean hasChar = true;//flag for has Character
while (in.hasNextLine()) { // while input exists
String a = in.nextLine();//get next line
while (i < a.length()) {// get each char

if (a.charAt(i) == ' ') {// blank space does not count in word count
hasChar = false;
}// does not work
else {

wordcount++;// count words
}
i++;//get each char
}
}

System.out.println("The word count is " + wordcount);// print wordcount
System.out.println("");
String line;// echo print the input file
while ((line = is.readLine()) != null) {
System.out.println("Echo print of the file: " + line);
}
in.close();

}


That's my code.


The Input file is this


Aiwegfu24r;q0912j


129puehilhwueildgyuol


The second line of code is not counted for in the word count. How may I fix this with while loops only?


Please do not give me the answer but rather guide me.



asked 50 secs ago







Create word-count application that accounts for multiple lines of text in an input file

Aucun commentaire:

Enregistrer un commentaire