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!
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.
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?
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.
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:
Code java:
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.
Code java:
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.
Re: Cannot figure this out at all... Someone mind giving me a helping hand?
Quote:
Originally Posted by
Norm
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.
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.
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.