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: Cannot figure this out at all... Someone mind giving me a helping hand?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot figure this out at all... Someone mind giving me a helping hand?

    I'm trying to make a textfile reader, which reads a text file line by line, and then copys everything past the fourth : and stops before the last : and then makes a list of what it copys in another file. I know this uses regex but I don't understand it at all and it's very confusing to me. Would someone please help me out? Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot figure this out at all... Someone mind giving me a helping hand?

    Can you post the code you are working on? I'm not sure what you are trying to do. Can you post the logic you think is needed to do what you want?
    I don't see how regex is involved in determining what lines have been read or copied to another file.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot figure this out at all... Someone mind giving me a helping hand?

    Well I was told by someone that regex is what I needed.. but I have no idea what to do so I havn't even started yet. All i'm trying to do is 1. Read a text file line by line 2. On each line, copy what's after the 4th : and stop before the last : then put every thing it copys in another file pasting things line by line exactly how the file it copied from was if that makes sense?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot figure this out at all... Someone mind giving me a helping hand?

    I'm not any good with regex.
    Is this what the program is supposed to do:
    find the fourth ":" and the last ":" on each line it reads, extract the substring that is between them and write that substring to a new file.
    One easy approach would be to use the String class's indexOf() and lastIndexOf() methods to find the locations of the ":"s.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Cannot figure this out at all... Someone mind giving me a helping hand?

    I don't know much about writing to files from Java, let alone creating one, I do however, know how to read them. For what you seem to want to do, I would use a scanner with a String variable to to read the contents of the whole file into:

    Scanner in = new Scanner(new File("drive\\path\\file.extension"));

    Remember to use two backslashes in java for the "\" character. Then look around in the Scanner API, I think I remember a method having to do with the next line .

    With another String variable, in one line you can copy from one mark to the next, as long as they are the only marks or if the the first mark is where you want to start and the last mark is where you want to end. In your case, that would be ":" line.

    String toCopy = file.________(_________, __________);

    Fill in the blanks to get what you want. That line will copy from where ever you want to start plus one, to wherever you want to end into the toCopy variable. Look around in the index Of the String API. That's all the help I can give you my friend. Like Norm, I don't know much about regex, and I don't think you really need to for this conundrum.

  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot figure this out at all... Someone mind giving me a helping hand?

    Quote Originally Posted by Norm View Post
    I'm not any good with regex.
    Is this what the program is supposed to do:
    find the fourth ":" and the last ":" on each line it reads, extract the substring that is between them and write that substring to a new file.
    One easy approach would be to use the String class's indexOf() and lastIndexOf() methods to find the locations of the ":"s.
    Can you please show an example of how i'd do that? and yes that's EXACTLY what I want, it will read each line which consists of characters/characters/characters:/copy this//copythis:stopcopying here

    then paste that to the corresponding line in another folder
    then it moves to the next line and does the same thing till its copied every section of every line in pasted and saved it in that other file.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot figure this out at all... Someone mind giving me a helping hand?

    Use indexOf() in a loop to fine the fourth ":" and use lastIndexOf() to find the last ":"
    then use substring to get the part of the String in between.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Cannot figure this out at all... Someone mind giving me a helping hand?

    I would not recommend using a regex for this particular application. Yes, it is possible however norm's suggestion of using indexOf() and lastIndexOf() is more efficient and likely more maintainable/readable (as suggested by other users lack of experience with regex).

    To write to a file:
    The simplest method is to use a PrintStream. This is the same class that System.out and System.err use so the interface will be similar.

    Simply pass the file name string. Note that this constructor will create a blank file to write to. If the file already exists, it's first deleted.

    PrintStream fout = new PrintStream("C:\\temp\\output.txt");
    fout.println("hello world!");
    fout.close();

Similar Threads

  1. gain of helping someone
    By luka316 in forum Member Introductions
    Replies: 1
    Last Post: February 6th, 2012, 07:53 AM
  2. hey everyone - fancy helping a newbie
    By dave11791 in forum Member Introductions
    Replies: 1
    Last Post: February 2nd, 2012, 02:48 PM
  3. I need a hand in editing this game
    By FEU_4thyear in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 6th, 2012, 08:14 AM
  4. The Concept of Entities -- Can't wrap my mind around it.
    By RaustBD in forum Java Theory & Questions
    Replies: 2
    Last Post: November 21st, 2011, 08:42 PM
  5. [SOLVED] helping hand required
    By arvindbis in forum Web Frameworks
    Replies: 4
    Last Post: October 7th, 2011, 12:29 PM