Results 1 to 5 of 5

Thread: Unknown problem

  1. #1
    sanchir0805 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    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.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    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....

  3. #3
    sanchir0805 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    so how should i end it. my intention is to count the word in text file.

  4. #4
    sanchir0805 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    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.

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •