Results 1 to 18 of 18
  1. #1
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Selective text reformatting

    I am scanning in text from txt file and i want to be able to select parts of it print it out to a different txt file or even better a csv file to open in excel.
    this is a part of the file text.


    09/18/14 1008:20 ALL PROCESS ALARMS ACKNOWLEDGED
    09/18/14 1008:26 ALL MAINTENANCE ALARMS ACKNOWLEDGED
    09/18/14 1008:26 ALL MAINTENANCE ALARMS ACKNOWLEDGED

    *** MESSAGE FROM SSP1 ***


    STANDARDIZE REPORT TIME = 1009 9/18/14
    1ST COAT BASIS WT

    Options: - KCM - DIRT - DYN - PROFILE
    F/A RATIO 0.7601 NET AIR CNTS 73867.5 NET FLAG CNTS 56143.5 BKGROUND CNTS 483.5
    BW OF FLAG 49.760 DFRAC -0.1984 ZFRAC 0.0000
    UP AIR TEMP 0.00 LO AIR TEMP 0.00 SOURCE TEMP 0.00 RCVR TEMP 0.00
    UP HEAD TEMP 2616.71 LO HEAD TEMP 401.11


    i only want the text in blue, and would be great to have the output in csv formatted like


    time 1st coat f/a ratio net air counts net flag counts
    1009 9/18/14 Basis WT 0.7601 73867.5 56143




    is this even possible through a scanner, or do i need to read it in as a string and split it?

    any help would be great, thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Selective text reformatting

    Yes, you could use a Scanner class method to read in lines from the file into a String.
    Once in a String the code would need to extract the fields it wants.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Selective text reformatting

    To get just the blue text you may also need to parse the text. Either only grab the text that matches a known pattern or ignore text that matches a known pattern. In the above example, you may want to ignore lines that begin with the word Options:.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Selective text reformatting

    can you give me an example?
    i can scan the file in, and print it, delimit it, but i have no idea how to parse just the parts i want.
    thanks again

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Selective text reformatting

    Norm suggested using the Scanner class. So just grab the first token of each line as a String and compare it to whatever you want to ignore/keep. If it matches, then ignore/keep the line and continue to the next. More extensive parsing can be done using regular expressions. See Pattern and Matcher classes. But they can be very tricky to use and can cause additional headaches.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Selective text reformatting

    how to parse just the parts i want.
    You need to define the parts of a line that you want to skip and the parts you want to process.
    Also there may be whole lines you want to skip.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Selective text reformatting

    ok, i think my best bet lies in the scanner class, as it does break it into the correct size tokens.
    how would i go about choosing the tokens i want to keep?
    i cannot find anything like .contains that i was using with stringBuffers.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Selective text reformatting

    Quote Originally Posted by mrlarue77 View Post
    i cannot find anything like .contains that i was using with stringBuffers.
    In what? I hope you're not looking in the scanner class for the answer - it is your responsibility to decide which tokens you want to keep, thus you look at the tokens themselves which will be String objects with a great number of value comparison methods.


    But is it the decision really token based? In the example you give it seems the data is given in several lines and you want to process every line except the one that starts with "Options:". That is a filter you might do before you start to tokenize the data.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Selective text reformatting

    It depends on what you want to do. String can search for substrings. And ArrayList has a contains method. If you can't find the appropriate methods to do what you want, then use the methods that do exist to write you own.

    Regards,
    Jim
    Last edited by jim829; 11-19-2014 at 05:52 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Selective text reformatting

    sorry for being so vague.
    the whole document has test results from 3 machines, 1st coat, 2nd coat and 3rd coat.
    i read about how the scanner class separated text by white space and it seems to treat each test as a token.
    so i thought i could pull in all the "1st coat" tokens, then reread them in line by line and drop the OPTIONS line that i don't need.
    is this not possible to do with a scanner?

  11. #11
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Selective text reformatting

    Quote Originally Posted by jim829 View Post
    It depends on what you want to do. String can search for substrings. And ArrayList has a contains method. If you can't find the appropriate methods to do what you want, then use the methods that do exist to write you own.

    Regards,
    Jim
    thanks Jim, am reading up on subStrings now.....i am pretty new to this so again, thanks for all your help

  12. #12
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Selective text reformatting

    I am making progress.
    I have cleaned out the unecesarry lines, and now in a separate class i have the txt broken into arrays.
    i also have it search the arrays for the machine and telling me if it is in the array.
    can anyone tell me how, instead printing yes or no, to print that array?
    here is what i have to get confirmation of the search.

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.regex.Pattern;
    
    public class FileManipulationScanner {
    
        public static void main(String[] args) throws IOException {
    
            Scanner s = null;
    
            try {
                s = new Scanner(new BufferedReader(new FileReader("/test.txt")));
                s.useDelimiter("\\*");
                String total = (s.next());
                Pattern p = Pattern.compile("\\n[\\n]+");
    
                String[] parts = p.split(total);
    
                boolean searchArrayResult = false;
                for (String str : parts) {
                    searchArrayResult = str.contains("1ST COAT");
                    if (searchArrayResult) {
                        System.out.println("Its Here!!!");
                    } else {
                        System.out.println("No!!!!");
                    }
    
                }
                while (s.hasNext()) {
    
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FileManipulationScanner.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (s != null) {
                    s.close();
                }
            }
        }
    }
    thanks

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Selective text reformatting

    Think about the requirement in isolation. How do you print out the contents of any array?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Selective text reformatting

    i added the java.util.Arrays import,
    and with that i can print the array as

    Java Code:
    System.out.println(Arrays.toString(parts));
    but, that is bad....prints the entire array for every time "true" is returned.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Selective text reformatting

    I thought that's what you wanted. Or are you saying that in some cases you only want certain pieces of a given line?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Selective text reformatting

    no, i am trying to sort the input text file that has results from 3 different machines.
    so the array search works when i tell it to print "Its here" or "No".
    the document has several test (i can upload if it helps)
    and currently it will output

    Its Here!!!
    Its Here!!!
    Its Here!!!
    No!!!!
    No!!!!
    No!!!!
    No!!!!
    Its Here!!!

    i would like it to print the array part it just searched if it found it.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Selective text reformatting

    Then simply save it in another array. Ignore the parts you don't want.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Selective text reformatting

    Got it

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.regex.Pattern;
    
    public class FileManipulationScanner {
    
        public static void main(String[] args) throws IOException {
    
            Scanner s = null;
    
            try {
                s = new Scanner(new BufferedReader(new FileReader("/Cleaned.txt")));
                s.useDelimiter("\\*");
                String total = (s.next());
                Pattern p = Pattern.compile("\\n[\\n]+");
    
                String[] parts = p.split(total);
    
                int i = 0;
                boolean searchArrayResult = false;
                for (i = 0; i < parts.length; i++) {
                    if (parts[i].contains("1ST COAT")) {
                        searchArrayResult = true;
                    }
                    if (searchArrayResult) {
                        System.out.println(parts[i]);
                    } else {
                        System.out.println("No!!!!");
                    }
                }
                while (s.hasNext()) {
    
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FileManipulationScanner.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (s != null) {
                    s.close();
                }
            }
        }
    }
    thank you guys for you help!

Similar Threads

  1. selective image encryption
    By anjalirag in forum New To Java
    Replies: 1
    Last Post: 04-20-2013, 07:28 AM
  2. Creating a selective InputVerifier
    By oinca in forum Advanced Java
    Replies: 3
    Last Post: 08-15-2011, 07:53 PM
  3. Replies: 3
    Last Post: 04-05-2011, 03:46 PM
  4. Send selective objects in a class over network
    By RDReavis in forum Advanced Java
    Replies: 2
    Last Post: 01-18-2010, 09:45 AM
  5. Go-back-N & Selective repeat protocol
    By Engineera in forum Networking
    Replies: 3
    Last Post: 01-15-2009, 12:18 AM

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
  •