Results 1 to 5 of 5
Thread: Unknown problem
- 03-19-2009, 10:22 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Unknown problem
im new to java and encountered this problem on homework. Im doing my best but cant find the error anywhere.
here is the code.
import java.io.*;
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);
while(inputFile.hasNext())
{
count++;
}
inputFile.close();
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.Last edited by sanchir0805; 03-19-2009 at 10:28 AM.
- 03-19-2009, 11:16 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
no error in code,
but...
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....
- 03-19-2009, 11:59 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
so how should i end it. my intention is to count the word in text file.
- 03-19-2009, 12:03 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
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.
Last edited by sanchir0805; 03-19-2009 at 12:05 PM.
- 03-20-2009, 03:56 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
if you want to count word instead of line, user inputFile.next()
Similar Threads
-
Constructor with unknown amount of objects?
By Bernard Robitaille in forum New To JavaReplies: 5Last Post: 03-01-2009, 05:00 AM -
Hi , Error in Jsp page is as "NumberFormatException.forInputString(Unknown Source)"
By lavanya82 in forum JavaServer Pages (JSP) and JSTLReplies: 9Last Post: 02-20-2009, 07:22 AM -
java.net.MalformedURLException: unknown protocol: ntp
By Nicholas Jordan in forum NetworkingReplies: 6Last Post: 12-24-2008, 03:35 AM -
Unknown publisher / Signing standalone tomcat.exe
By millross in forum New To JavaReplies: 1Last Post: 06-14-2007, 02:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks