简体   繁体   中英

How to print only certain sections of a string?

First off - please forgive me, for I am an amateur.

For a project, I am taking an excel file as input containing a list of kittens and I want to output the addresses these kittens have been found at.

I have implemented the code so that a kitten is an object with a name, ID, and notes (kitten was assigned these attributes from evaluating each cell in the excel doc). The notes section contains info about where the kitten was found.

Excel document: 

name | ID | Notes
--------------------
Kit  | 5  | Great animal! Haha! Found at 1234 east 
     |    |   street. Incredibly ugly. 
---------------------
Kat  |  2 | Wow, what a charmer. from location 3456 
     |    | Dusk road
    .
    .
    .

Currently, my program converts the excel doc to a string and prints the entire 'Notes' section for each kitten. I want it to extract the address (trying to get as many of the addresses as possible) from the rest of the string, so the output would look something like this:

    1234 east street, 3456 Dusk Road, ... 

All I could find online was about String delims, etc, but I am not sure how to start thinking about extracting specific phrases from a long varied string. Is there some way to record info at some key word, like "Found at" or "from location", and then stop at a period?

Would it be easier not to convert each address to one long string, but instead, print out the extracted address for each kitten?

My Code (for reference):

 public class Kitten {
     private String name;
     private String animalID;
     private String addressFound;


     public Kitten() {
        super();
        this.name = name;
        this.animalID = animalID;
        this.addressFound = addressFound;
     }

    //getters and setters

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getAnimalID() {
        return animalID;
    }

    public void setAnimalID(String animalID) {
        this.animalID = animalID;
    }

    public String getAddress() {
        return addressFound;
    }

    public void setAddress(String addressFound) {
        this.addressFound = addressFound;

    }
 }

INPUT: excel file with kitten info. Prints the "Notes" section for each kitten

 public class ReadExcel {

     public void printer() {
        try {

            FileInputStream kittenFile = new FileInputStream(new 
                                    File("./IntakeNotesSimple.xlsx"));

            XSSFWorkbook wb = new XSSFWorkbook(kittenFile);
            XSSFSheet sheet = wb.getSheetAt(0);

            ArrayList<Kitten> kittenList = new ArrayList<>();
            for (int i= sheet.getFirstRowNum() + 1; i<= sheet.getLastRowNum(); 
                                i++) {
                Kitten k = new Kitten();
                Row r = sheet.getRow(i);

                for (int j = r.getFirstCellNum(); j<= r.getLastCellNum(); j++) 
                {
                        Cell c = r.getCell(j);

                        if (j==0) {
                            k.setName(c.getStringCellValue());
                        }


                        if (j==1) {
                            k.setAnimalID(c.getStringCellValue());
                        }
                        if (j==2 && (c != null)) {
                            k.setAddress(c.getStringCellValue());
                        }

                }
                kittenList.add(k);

            }

            for (Kitten kit: kittenList) {
                 System.out.println(kit.getAddress() +"\n" +);
            }

            wb.close();

        }
        catch(Exception e) { 
            e.printStackTrace();
        }
     }
 }



public class PrintOut {
    public static void main(String[] args) throws FileNotFoundException {
        ReadExcel addresses = new ReadExcel();
        addresses.printer();
    }
}

Let's say you have a list of words that will give you the start of the address (practically it won't there are so many possibilities, but let's imagine it as you suggest this would work in your OP).

The string you search in will start with some characters, then either "found at" or "from location" and it will end at the next , . ! or ? character. Finally some last part will contain some other characters.

The solution you should be using here is Regex, aka best pattern matching tool you could find. The regex for the pattern described above would be :

^.*?(found at|from location) (.*?)([\\.,!?].*+|)$

This regex is not that easy so we might not get into details, I'd better link you to some visual tool for this regex : https://regex101.com/r/q1w428/1

So now, how to use it within java app ?

  private static final String KITTEN_PATTERN_STRING = "^.*?(found at|from location) (.*?)([\\.,!?].*+|)$";

  private static final Pattern KITTEN_PATTERN = Pattern.compile(KITTEN_PATTERN_STRING);

  public String extractKittenAddress(String kittenString) {
       Matcher m = KITTEN_PATTERN.matcher(kittenString);

       if(m.matches()) 
          return m.group(2);
       return null;

  }

And there you go!

I assumed that you have a string contains some text and address. And your delimiter word is :

Found at

So you can split your text and extract the address while iterating your data as below :

public class Main {
    public static void main(String[]args) throws JsonProcessingException {
        String textContaintsAddress = "Great animal! Haha! Found at 1234 east street. Incredibly ugly.";
        String address[] = textContaintsAddress.split("Found at");

        if (address.length > 1) {
            System.out.println(address[1].trim());
        }else{
            System.out.println(textContaintsAddress);;
        }
    }
}

It prints :

1234 east street. Incredibly ugly.

Edit your code as below :

public class ReadExcel {

    public void printer() {
        try {

            FileInputStream kittenFile = new FileInputStream(new
                    File("./IntakeNotesSimple.xlsx"));

            XSSFWorkbook wb = new XSSFWorkbook(kittenFile);
            XSSFSheet sheet = wb.getSheetAt(0);

            ArrayList<Kitten> kittenList = new ArrayList<>();
            for (int i= sheet.getFirstRowNum() + 1; i<= sheet.getLastRowNum();
                 i++) {
                Kitten k = new Kitten();
                Row r = sheet.getRow(i);

                for (int j = r.getFirstCellNum(); j<= r.getLastCellNum(); j++)
                {
                    Cell c = r.getCell(j);

                    if (j==0) {
                        k.setName(c.getStringCellValue());
                    }
                    if (j==1) {
                        k.setAnimalID(c.getStringCellValue());
                    }
                    if (j==2 && (c != null)) {
                        // here we add the logic
                        String textContaintsAddress = c.getStringCellValue();
                        String address[] = textContaintsAddress.split("Found at");

                        if (address.length > 1) {
                            k.setAddress(address[1].trim());
                        }else{
                            k.setAddress(textContaintsAddress);;
                        }

                    }

                }
                kittenList.add(k);
            }

            for (Kitten kit: kittenList) {
                System.out.println(kit.getAddress() +"\n" +);
            }

            wb.close();

        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM