Results 1 to 9 of 9
  1. #1
    Fatz is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Reading only integers from text files.

    I'm writing a program which reads only integers from a text file. I have done the lions share of the program, however one thing is bothering me:

    When a non integer is in the text file, all the correct integers should be printed, with element not valid, replacing the element which is not valid. For example 10, 203, 2.02 10 (in text file) would print 10, 203, element not valid 10 . For this do i use an array to access the part which is not valid?

    Any suggestions?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Reading only integers from text files.

    Use an if test:
    Java Code:
    read next token
    if (if token is valid int)
      print the token
    else
      print the error message
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Fatz is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Reading only integers from text files.

    Thank you for replying,

    what would i reply the "token is valid" with?

    Thanks

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Reading only integers from text files.

    One way would be to use the Integer class's parse method and catch the exception for invalid ints.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: Reading only integers from text files.

    what about scanner? i don't know if it can be used for file streaming or not... it has hasNextInt() method which read the next integer if it does exist...

  6. #6
    Fatz is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Reading only integers from text files.

    Thanks ill try it out,

    i've used the parse method, my file reads one text form the file, and throws error if there is not an integer, but not all, ill give the has next int ago.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default Re: Reading only integers from text files.

    Quote Originally Posted by chipp View Post
    it has hasNextInt() method which read the next integer if it does exist...
    That method doesn't read anything; it just tells whether or not a nextInt() method call will succeed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default Re: Reading only integers from text files.

    throws error if there is not an integer,
    That's what the catch block would allow you to handle.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Fatz is offline Member
    Join Date
    May 2012
    Posts
    12
    Rep Power
    0

    Default Re: Reading only integers from text files.

    Thanks i've managed to solve the problem

Similar Threads

  1. Reading Text Files
    By javaman1 in forum New To Java
    Replies: 3
    Last Post: 02-27-2011, 02:13 AM
  2. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 05:45 PM
  3. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 4
    Last Post: 05-17-2010, 08:22 AM
  4. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 9
    Last Post: 05-17-2010, 05:45 AM
  5. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-04-2009, 11:53 PM

Tags for this Thread

Posting Permissions

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