Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how does java scanner work?

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    79
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default how does java scanner work?

    if hava a code using a while loop and .hasnextmethod but it fails to work with one file the file starts with
    hello world!
    thisIsTheStart
     
    for Security
    OUR  List
     
    by country
     
         List
        Free  Lists
        List
        Servers Lists
        USA 
        Premium USA 
        UK 
        Premium UK 
        German 
        Premium German 
        Franšais
        Premium French
    if i remove above sectio it works does anyone know why this is ?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how does java scanner work?

    What do you mean "it fails to work"? ..What happens? What did you expect instead?
    What code? Your screen is hard to see from my house...
    If you remove what section?

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    79
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default Re: how does java scanner work?

    i have a number of files that i scan with scanner but on this file it fails due to the four spaces at the start of the lines. i assume the scanner checks a number of lines for chars at start of each line and if it doesint find any assumes the file is blank?

    the solution i found although i would still like to know more about how to scanner.hasNext() works is
    LineIterator it = IOUtils.lineIterator(new BufferedReader(new java.io.FileReader("path to file")));
                for (int i = 0; it.hasNext(); i++){//int i = 0; it.hasNext(); i++
                    dd.add((String) it.next());//casts and adds to ArrayList<String>
                    }
    which did read the file

    thanks for replys

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how does java scanner work?

    Quote Originally Posted by bean View Post
    i assume the scanner checks a number of lines for chars at start of each line and if it doesint find any assumes the file is blank?
    What did the documentation say?

  5. #5
    Member
    Join Date
    Apr 2013
    Posts
    79
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Default Re: how does java scanner work?

    A scanner can read text from any object which implements the Readable interface. If an invocation of the underlying readable's Readable.read(java.nio.CharBuffer) method throws an IOException then the scanner assumes that the end of the input has been reached. The most recent IOException thrown by the underlying readable can be retrieved via the ioException() method.

    but i get no errors thrown the programe reads a file and writes it to plain txt but the writen file is empty, the programe reads a numbr of files but it only fails to grab data from files that have these white spaces.
    i have tryed putting Strings at the start of the file but the Scanner doesint even read them which has me puzzled i did read through the doc but couldint find the answer. i have solved the problem but it still has me puzzled no doubth i will run into this problem again or will stumble onto the answer

Similar Threads

  1. Develop File Scanner in Java
    By cantonbean in forum Java Theory & Questions
    Replies: 5
    Last Post: May 8th, 2013, 02:17 AM
  2. HELP! java code without using Scanner
    By jooly61 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 23rd, 2013, 08:41 AM
  3. Scanner code won't work
    By r19ecua in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 21st, 2011, 04:49 PM
  4. java scanner programming help
    By socboy6579 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2010, 04:02 AM
  5. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM