im new to java and encountered this problem on homework. Im doing my best but cant find the error anywhere.
here is the code.
public class SEPrep7g
public static void main(String args) throws IOException
System.out.println("Program Results for CSCI 2911-A");
System.out.println("This program solves assignment SEPrep7g");
System.out.println("This program counts the number of words" +
" in a file and displays it\n");
int count = 0;
File file = new File("D:\\DICTION.TXT");
Scanner inputFile = new Scanner(file);
System.out.println("There are "+count+" words in the file D:DICTION.TXT");
Here is the output.. it doesnt show the result. just stays there.
--------------------Configuration: SEPrep7g - JDK version 1.6.0_12 <Default> - <Default>--------------------
Program Results for CSCI 2911-A
This program solves assignment SEPrep7g
This program counts the number of words in a file and displays it
<----- it stays here.. never shows the result
Process interrupted by user.
can any1 see an error in my code?
EDIT: DICTION.TXT file has 3 words in it and the path is right.
no error in code,
assume a pointer point to the starting of your file,
you call inputFile.hasNext(), then pointer say "yes"
pointer do not move in the file,
call inputFile.hasNext() again, then pointer say "yes"
pointer remain at the start of the file
loop never stop....
so how should i end it. my intention is to count the word in text file.
oh ok got it.. i just added "inputFile.nextLine" inside while loop. that should make the pointer go down one line since words in the text file are one at each line.
if you want to count word instead of line, user inputFile.next()