Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-19-2009, 11:22 AM
Member
 
Join Date: Mar 2009
Posts: 3
Rep Power: 0
sanchir0805 is on a distinguished road
Default 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 11:28 AM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-19-2009, 12:16 PM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
Default
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....
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-19-2009, 12:59 PM
Member
 
Join Date: Mar 2009
Posts: 3
Rep Power: 0
sanchir0805 is on a distinguished road
Default
so how should i end it. my intention is to count the word in text file.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-19-2009, 01:03 PM
Member
 
Join Date: Mar 2009
Posts: 3
Rep Power: 0
sanchir0805 is on a distinguished road
Default
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 01:05 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-20-2009, 04:56 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
Default
if you want to count word instead of line, user inputFile.next()
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructor with unknown amount of objects? Bernard Robitaille New To Java 5 03-01-2009 06:00 AM
Hi , Error in Jsp page is as "NumberFormatException.forInputString(Unknown Source)" lavanya82 JavaServer Pages (JSP) and JSTL 9 02-20-2009 08:22 AM
java.net.MalformedURLException: unknown protocol: ntp Nicholas Jordan Networking 6 12-24-2008 04:35 AM
Unknown publisher / Signing standalone tomcat.exe millross New To Java 1 06-14-2007 03:25 PM


All times are GMT +2. The time now is 09:06 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org