简体   繁体   中英

Finding exact string using line.contains

Apologies if some of this information comes across as lacking knowledge, just started learning Java.

In this working the user searches for both road and town. The problem being that when searching for something like 'Cabramatta' the result 'Cabramatta West' will also appear in the results.

The format of the information being read is as follows:

William Street^3^3503^Collins Street^Cabramatta West

William Street^3^3503^Collins Street^Cabramatta

 while(fileName.hasNext())
     {
        String line =fileName.nextLine();
        {
        if(line.contains(suburbInput) && line.contains(roadInput))
           {
              String tramDetails[] = line.split("\\^");
              String crossStreet = tramDetails[0];
              String stopNumber = tramDetails[1];
              int stopNumberInt = Integer.parseInt(stopNumber);
              String trackerID = tramDetails[2];
              int trackerIDInt = Integer.parseInt(trackerID);
              String roadName = tramDetails[3];
              String suburbName = tramDetails[4];

System.out.print("'Suburb': " + suburbName + " 'Road': " + roadName + " 'Cross Street': " + crossStreet + " 'Stop': " + stopNumberInt + " 'Tracker ID': " + trackerIDInt + "\n");

How do I go about getting it to just find results for 'Cabramatta' when it's searched but also find results for 'Cabramatta West' when that's searched?

Split before and then just use equals method of the String . Here is a test code:

        String line = "William Street^3^3503^Collins Street^Cabramatta West";
        String suburbInput = "Cabramatta";
        String roadInput = "Collins Street";

        String tramDetails[] = line.split("\\^");
        String crossStreet = tramDetails[0];
        String stopNumber = tramDetails[1];
        int stopNumberInt = Integer.parseInt(stopNumber);
        String trackerID = tramDetails[2];
        int trackerIDInt = Integer.parseInt(trackerID);
        String roadName = tramDetails[3];
        String suburbName = tramDetails[4];

        if (suburbInput.equals(suburbName) && roadInput.equals(roadName))
            System.out.print("'Suburb': " + suburbName + " 'Road': " + roadName + " 'Cross Street': " + crossStreet
                    + " 'Stop': " + stopNumberInt + " 'Tracker ID': " + trackerIDInt + "\n");

        suburbInput = "Cabramatta West";

        if (suburbInput.equals(suburbName) && roadInput.equals(roadName))
            System.out.print("'Suburb': " + suburbName + " 'Road': " + roadName + " 'Cross Street': " + crossStreet
                    + " 'Stop': " + stopNumberInt + " 'Tracker ID': " + trackerIDInt + "\n");

And output:

'Suburb': Cabramatta West 'Road': Collins Street 'Cross Street': William Street 'Stop': 3 'Tracker ID': 3503

Hope this helps!

You could uses String#endsWith() instead of String#contains() for the suburb:

if (line.endsWith(suburbInput) && line.contains(roadInput))

Of course, this is only a band-aid. 'Cabramatta' would still match 'West Cabramatta' The problem is the if (...) statement, as implemented, is only able to find probable matches. You need to parse the line into the exact fields you want to match against, and then test those fields explicitly.

Alternately (sledge hammer approach), you could implement a regular expression matcher that will match everything exactly in one go.

You're going to have to split up your inputs before your check so you can use .equals instead of .contains.

while(fileName.hasNext())
 {
    String line =fileName.nextLine();
    String tramDetails[] = line.split("\\^");
    String suburbName = tramDetails[4];
    String roadName = tramDetails[3];

    if(suburbName.equals(suburbInput) && roadName.equals(roadInput))
    {

          String crossStreet = tramDetails[0];
          String stopNumber = tramDetails[1];
          int stopNumberInt = Integer.parseInt(stopNumber);
          String trackerID = tramDetails[2];
          int trackerIDInt = Integer.parseInt(trackerID);

          System.out.print("'Suburb': " + suburbName 
                            + " 'Road': " + roadName 
                            + " 'Cross Street': " + crossStreet   
                            + " 'Stop': " + stopNumberInt 
                            + " 'Tracker ID': " + trackerIDInt + "\n");
    }

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