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 8 of 8

Thread: Getting an input from a difficult to read file

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    22
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Getting an input from a difficult to read file

    My question is:

    Is it in any way, possible to go into a file and then when it comes to a line you specified it takes the input you want?

    Like this:
    192,160,2275,1,0,
    |
    I want this number.

    How could i get that number?

    The first 2 numbers at the example line could be 2 digits or 3 digits.
    I am not a file I/O pro or something like that.



    What needs to be done:
    Read a file specified; (Not done)
    Go to a specified line number; (Done)
    go to the third number in the line; (Not done)
    Read the number; (Not done)
    Put the number in a IntList; (Done)



    Could anyone please help me with opening the file and taking the 3rd number out?

    Thanks in advance!

    Niels

    --- Update ---

    I want the third number from the example! not the first one .


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting an input from a difficult to read file

    You can either look into regular expressions, or you can parse the String yourself.

    If you want to go the regular expression route, check out the Pattern class.

    If you want to parse the String yourself, then take a step away from the computer. How would you do this by hand, with a piece of paper and a pencil? Say you have a stack full of index cards, each with a line from the file on it. What *exactly* do you want to do? Write out instructions (not code) that a complete stranger (who has no idea what your real goal is) could follow to accomplish your goal. When you have that written out, you'll have an algorithm that you can think about implementing in code.

    Which part of this are you stuck on? Can you post an MCVE showing *just that step* and where you're stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    22
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Getting an input from a difficult to read file

    Since I dont know about the Pattern class (and no good tutorials on it imo) i would need to go to that line and go to the second comma and start reading untill i find another comma? how would you set that up? if i know this i could find how to open a file and a go to that number and start doing this code untill the file ends.

    Could you tell me how i find these 2 commas? that would finish my whole post really .

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting an input from a difficult to read file

    Sorry, I'm not going to do your homework for you.

    This is the first result for googling "java regular expressions": Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    The Pattern API is full of documentation that's worth reading.

    If you don't want to go with regular expressions, then the String class has several methods that could come in handy here.

    I highly suggest breaking this down into smaller steps. Can you create a small example program that hardcodes a String and extracts the relevant data from it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    22
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Getting an input from a difficult to read file

    btw this is not even homework... this is for timing in a program im making.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Getting an input from a difficult to read file

    Cool. I'm still not going to do your work for you. :p
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    22
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Getting an input from a difficult to read file

    But if i make a pattern (what i just read about) then it doesnt work. So that leaves that option (it doesnt work because we need the number we are trying to find but we havent found it yet)
    which leaves us at just the String class. Which im not going to dig in just yet. When i have more time ill try and finish this. Thanks for your help!

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Getting an input from a difficult to read file

    1) You can read in the entire file and save it, line by line, into a String array. This is rather simple and you should find plenty of tutorials on how to do this.
    2) Then you can go to the desired line by indexing into the array
    3) You can split the String using the "split(pattern)" method from the String class. For example split(",") will split into substrings devided by a ',' character.
    4) Split returns an array, so you can simply take the 3rd entry from the splitted String.

Similar Threads

  1. Why won't it read my Input file?
    By squirrelmind in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 14th, 2014, 07:07 PM
  2. Trying to input a text file but the program won't read it.
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 27th, 2013, 11:50 AM
  3. convert excel to xml and read the input from xml file
    By rahulruns in forum Object Oriented Programming
    Replies: 5
    Last Post: April 3rd, 2012, 11:13 AM
  4. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  5. [SOLVED] How to read from GUI input?
    By KingOfClubs in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 21st, 2011, 01:32 PM

Tags for this Thread