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

Thread: Best Method to Extract Part of a Text File?

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Best Method to Extract Part of a Text File?

    I have a text file where I need to search and extract a portion of it (sample file attached). This is a little more complicated than most examples I have seen e.g. using the scanner class. This file is for a microwave antenna containing several sets of data depending on the polarity combination e.g. HH, VV etc..

    The search sequence should be as follows:
    1. find the line containing the polarity combination
    2. find the line containing -180.0 and start the extraction (to an arraylist ?)
    3. find the line containing 180.0 and end the extraction

    Any guidance in this would be helpful.
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Best Method to Extract Part of a Text File?

    1. find the line containing the polarity combination
    2. find the line containing -180.0 and start the extraction (to an arraylist ?)
    3. find the line containing 180.0 and end the extraction
    The logic seems simple enough. What problems are you having writing the code to do it?
    One technique would be to use boolean variables to remember the state of the search.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Best Method to Extract Part of a Text File?

    OK, I have got this working for the most part, however instead of capturing the -180.0 and 180.0 portions of the first and last lines in the range to the arraylist, it prints zero values for these instead. Has anyone come across this before? The source code and test file are attached.
    Attached Files Attached Files

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Best Method to Extract Part of a Text File?

    If you have any questions about your code, post it here with the questions.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Best Method to Extract Part of a Text File?

    As requested, here is the code to extract a portion of a text file. It however, on the first and last lines of the extracted text, returns 0.0 instead of -180.0 and 180.0

    package test;
     
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author gary
     */
    public class TEST {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            try (Scanner extractpolinfoscanner = new Scanner(new File("/home/gary/NetBeansProjects/TEST/2818B.ADF"))) {
                String polcomb = "H/H";
     
                searchforpolcombloop:
                while(extractpolinfoscanner.hasNextLine()){
                    String str = extractpolinfoscanner.findInLine(polcomb);  
                    if(str != null)
                    {
                        // if polcomb is found, then search for -180.0 and start extracting the lines to an arraylist, searching
                        //for 180.0, then when found skip back to the next row of the spreadsheet
                        String startstr = String.valueOf(-180.0);
                        String endstr = String.valueOf(180.0);
                        ArrayList antpattern = new ArrayList();
                        // search for the line containing -180.0, save it to the arraylist, then skip to the loop that searches for 180.0
                        while(extractpolinfoscanner.hasNextLine()){
                            String startpatt = extractpolinfoscanner.findInLine(startstr);
                            if(startpatt != null){ // if it finds -180.0, add that line to the arraylist
                                antpattern.add(extractpolinfoscanner.nextLine());
                                break;
                            }
                            extractpolinfoscanner.nextLine();
                        }
                        // search for the line containing 180.0, meanwhile saving each line to the arraylist then skip out of the loop
                        while(extractpolinfoscanner.hasNextLine()){
                            String endpatt = extractpolinfoscanner.findInLine(endstr);
                            antpattern.add(extractpolinfoscanner.nextLine());
                            if(endpatt != null){ // if it found 180.0, break out of the search loop 
                                System.out.println("Contents of the antpattern arraylist:"); //this is only for testing
                                for(int j = 0; j < antpattern.size(); j++) {   
                                System.out.println(antpattern.get(j));
                                }
                                break searchforpolcombloop;
                            }
                        }
                    }
                    extractpolinfoscanner.nextLine();
     
                }
            } 
            catch (FileNotFoundException ex) {
                Logger.getLogger(TEST.class.getName()).log(Level.SEVERE, null, ex);
            }    
        }
     
    }

    This is the input file:

    REVNUM:,NSMA WG16.99.050
    REVDAT:,19990520
    ANTMAN:,ANDREW CORPORATION
    MODNUM:,HP6-71W
    PATNUM:,2818B
    FEDORN:,NA
    DESCR1:,
    DESCR2:,
    DESCR3:,
    DESCR4:,
    DESCR5:,
    DTDATA:,19911216
    LOWFRQ:,7125
    HGHFRQ:,8500
    GUNITS:,DBI/DBR
    LWGAIN:,39.7
    MDGAIN:,40.3
    HGGAIN:,41.1
    AZWIDT:,1.50
    ELWIDT:,1.50
    ATVSWR:,1.06
    FRTOBA:,66.0
    ELTILT:,0
    PATTYP:,ENVELOPE
    NOFREQ:,NA
    PATFRE:,NA
    NUMCUT:,4
    PATCUT:,AZ
    POLARI:,H/H
    NUPOIN:,41
    -180.00,-66.00,
    -100.00,-66.00,
    -61.72,-46.09,
    -55.00,-43.00,
    -30.90,-39.98,
    -26.43,-38.00,
    -15.00,-37.00,
    -13.67,-34.03,
    -9.75,-33.20,
    -5.08,-24.94,
    -2.33,-24.94,
    -2.33,-19.99,
    -2.17,-15.03,
    -1.92,-9.91,
    -1.75,-7.93,
    -1.58,-5.95,
    -1.25,-3.96,
    -1.00,-2.15,
    -0.75,-1.16,
    -0.50,-0.33,
    0.00,0.00,
    0.50,-0.33,
    0.75,-1.16,
    1.00,-2.15,
    1.25,-3.96,
    1.58,-5.95,
    1.75,-7.93,
    1.92,-9.91,
    2.17,-15.03,
    2.33,-19.99,
    2.33,-24.94,
    5.08,-24.94,
    9.75,-33.20,
    13.67,-34.03,
    15.00,-37.00,
    26.43,-38.00,
    30.90,-39.98,
    55.00,-43.00,
    61.72,-46.09,
    100.00,-66.00,
    180.00,-66.00,
    PATCUT:,AZ
    POLARI:,H/V
    NUPOIN:,19
    -180.00,-72.00,
    -100.98,-72.00,
    -27.42,-51.21,
    -15.00,-47.00,
    -11.00,-47.00,
    -10.00,-46.00,
    -3.42,-46.00,
    -2.17,-42.79,
    -1.58,-30.00,
    0.00,-30.00,
    1.58,-30.00,
    2.17,-42.79,
    3.42,-46.09,
    10.25,-46.09,
    11.00,-47.00,
    15.00,-47.00,
    27.42,-51.21,
    100.98,-72.00,
    180.00,-72.00,
    PATCUT:,AZ
    POLARI:,V/V
    NUPOIN:,45
    -180.00,-66.00,
    -95.00,-66.00,
    -61.72,-52.04,
    -37.36,-45.10,
    -19.97,-36.01,
    -15.00,-33.00,
    -12.50,-31.00,
    -9.00,-31.00,
    -8.00,-28.00,
    -7.50,-26.50,
    -6.00,-26.50,
    -5.00,-25.00,
    -3.17,-25.00,
    -2.17,-24.94,
    -2.17,-19.82,
    -2.17,-14.87,
    -1.92,-9.91,
    -1.67,-6.77,
    -1.25,-3.96,
    -1.00,-1.98,
    -0.83,-0.99,
    -0.50,-0.33,
    0.00,0.00,
    0.50,-0.33,
    0.83,-0.99,
    1.00,-1.98,
    1.25,-3.96,
    1.67,-6.77,
    1.92,-9.91,
    2.17,-14.87,
    2.17,-19.82,
    2.17,-24.94,
    3.17,-25.00,
    5.00,-25.00,
    6.00,-26.50,
    7.50,-26.50,
    8.00,-28.00,
    9.00,-31.50,
    12.50,-31.50,
    15.00,-33.00,
    19.97,-36.01,
    37.36,-45.10,
    61.72,-52.04,
    95.00,-66.00,
    180.00,-66.00,
    PATCUT:,AZ
    POLARI:,V/H
    NUPOIN:,29
    -180.00,-72.00,
    -101.97,-72.00,
    -61.72,-56.00,
    -50.00,-56.00,
    -45.00,-55.00,
    -28.00,-55.00,
    -21.96,-50.72,
    -15.00,-50.06,
    -9.08,-50.06,
    -6.92,-46.09,
    -3.50,-46.09,
    -2.75,-39.98,
    -2.00,-39.98,
    -1.58,-30.00,
    0.00,-30.00,
    1.58,-30.00,
    2.00,-39.98,
    2.75,-39.98,
    3.50,-46.09,
    6.92,-46.09,
    9.08,-50.06,
    15.00,-50.06,
    21.96,-50.72,
    28.00,-55.00,
    45.00,-55.00,
    50.00,-56.00,
    61.72,-56.00,
    101.97,-72.00,
    180.00,-72.00,
    ENDFIL:,EOF
    This is the output:

    Contents of the antpattern arraylist:
    0,-66.00,
    -100.00,-66.00,
    -61.72,-46.09,
    -55.00,-43.00,
    -30.90,-39.98,
    -26.43,-38.00,
    -15.00,-37.00,
    -13.67,-34.03,
    -9.75,-33.20,
    -5.08,-24.94,
    -2.33,-24.94,
    -2.33,-19.99,
    -2.17,-15.03,
    -1.92,-9.91,
    -1.75,-7.93,
    -1.58,-5.95,
    -1.25,-3.96,
    -1.00,-2.15,
    -0.75,-1.16,
    -0.50,-0.33,
    0.00,0.00,
    0.50,-0.33,
    0.75,-1.16,
    1.00,-2.15,
    1.25,-3.96,
    1.58,-5.95,
    1.75,-7.93,
    1.92,-9.91,
    2.17,-15.03,
    2.33,-19.99,
    2.33,-24.94,
    5.08,-24.94,
    9.75,-33.20,
    13.67,-34.03,
    15.00,-37.00,
    26.43,-38.00,
    30.90,-39.98,
    55.00,-43.00,
    61.72,-46.09,
    100.00,-66.00,
    0,-66.00,

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Best Method to Extract Part of a Text File?

    Are you saying this line
    0,-66.00,
    should contain something else?

    What happened to the value that you want printed?

    Can the input file be minimized to the smallest possible contents?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Best Method to Extract Part of a Text File?

    The first line of the arraylist should be -180.0,-66.00, and the last line should be 180.0,-66.00, to agree with the information extracted from the input file.

    At this point I don't want to print anything. There will be further code to process/use the information in the arraylist.

    I thought it would be redundant to save a portion of the input file rather than to just create a temporary arraylist and use it from that point.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Best Method to Extract Part of a Text File?

    Try more debugging by printing out ALL the Strings that are read and processed by the code to see where the data goes missing. Read the lines into a String and print the String before adding it to the arraylist.
    Make sure to add id Strings to the print out so you can tell which println printed the String.
    For example:
    System.out.println("an ID "+ theString +"<");  // print with a unique id and delimiter
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Best Method to Extract Part of a Text File?

    Thanks, I'll have to dig in further.

  10. #10
    Junior Member
    Join Date
    Feb 2014
    Location
    Finland
    Posts
    25
    My Mood
    Bored
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Best Method to Extract Part of a Text File?

    Your first input file doesn't have the same as the input data you have given.

    If your conditions are as simple as you given in the original post, i think you are over-complicating the algorithm. I know we aren't encouraged to paste complete code but
    how about something like this:

    package main;
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class ParseTest {
     
    	public static void main(String[] args) {
     
    		// flag to store
    		boolean  isToBeSaved = false;	
    		//Storage
    		  ArrayList<String> lines = new ArrayList<String>();
    		//keys 
    		  String startStr = "-180.0";
    		  String endStr = "180.0";
    		//Data file
    		String filePath = "C:\\Temp\\data.txt";
     
    		System.out.println("START OF READ LINES");
     
    		Scanner in;
    		try {
    			in = new Scanner(new File(filePath));
     
    			while ( in.hasNextLine() ) { // go trough all the lines in the data file
    				String line = in.nextLine();
    				System.out.println("\t" + line); //for debugging
    				if ( isToBeSaved ) { // is the line between start and end strings ( the -180 and 180 )
    					lines.add(line); // save the data
    					if ( line.contains(endStr)) 
    						isToBeSaved = false;
    				} else { 
    					if ( line.contains(startStr)) { 
    						isToBeSaved = true;
    						lines.add(line);
    					}
    				}
    			}
    			in.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
     
    		System.out.println("END OF READ LINES\n\n");
     
     
    		printStoredLines(lines);
     
    	}
     
            //method to print out the stored data.
    	public static void printStoredLines(ArrayList<String> lines) {
    		System.out.println("START OF THE STORED LINES");
    		for ( String storedLine : lines ) 
    			System.out.println(storedLine);
    		System.out.println("END OF THE STORED LINES");		
    	}
     
    }

Similar Threads

  1. how to extract specific part of a string in java
    By bhoots21304 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2014, 01:03 PM
  2. extract specific text from a string
    By smemamian in forum Android Development
    Replies: 2
    Last Post: January 18th, 2014, 05:42 AM
  3. Editing a Text File in Java, a specific part.
    By sakonpure6 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2013, 03:32 PM
  4. extract selected text out of a pdf file using java
    By ashish.sharma in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 2nd, 2013, 04:01 AM
  5. How to extract text from web
    By HelloAll in forum Java Theory & Questions
    Replies: 4
    Last Post: January 10th, 2011, 05:50 AM