Hector
Hector

Reputation: 51

Retrieving certain strings from a text file in Java

Is there a possible way to find particular strings in lines of a text file and store them in variables?

For example:

2007-11-11 @South Alabama            72  Sam Houston St           54           
2007-11-11 Auburn                    91  Detroit                  47           
2007-11-11 @Missouri KC              65  Air Force                59           
2007-11-11  Ga Southern              67 @Stetson                  51   

For this text file, I would like to store the strings that have an @ symbol in them so that I can run a test later on in the program with String.contains.

In other words, is there way to get the either the second or the fourth or the fifth string and check if they have an @ symbol?

I want to do something like:

if(secondToken.contains("@") && int1 > int2){
stuff...
}else if(fourthToken.contains("@") || fifthToken.contains("@") && int1 < int2){
other stuff...
}

I already have all the int values in the lines stored.

Upvotes: 1

Views: 71

Answers (1)

doublesharp
doublesharp

Reputation: 27599

You can use the Pattern and Matcher classes with a regex of:

^\d{4}-\d{2}-\d{2}\s+([^\d]+)([\d]+)\s+([^\d]+)([\d]+)\s*$

This regex has matching groups for each of the team names and scores - the backslashes will need to be escaped in your string. You can then loop over all the matches and check the matching group values for a start string of "@".

Given the line:

2007-11-11 @South Alabama            72  Sam Houston St           54           

The regex pattern will match as follows:

  • \d{4}-\d{2}-\d{2}\s+ = "2007-11-11 "
  • ([^\d]+) = "@South Alabama " - First matching group, trim the match
  • ([\d]+) = "72" - Second matching group
  • \s+ = " "
  • ([^\d]+) = "Sam Houston St " - Third matching group, trim the match
  • ([\d]+) = "54" - Fourth matching group
  • \s* = " "

Example code:

public static void main(String[] args) {
    // Input string with \n newlines
    String input = "2007-11-11 @South Alabama            72  Sam Houston St           54           \n2007-11-11 Auburn                    91  Detroit                  47          \n2007-11-11 @Missouri KC              65  Air Force                59           \n2007-11-11  Ga Southern              67 @Stetson                  51   ";
    // The regex with four matching groups, backslashes escaped
    String regex = "^\\d{4}-\\d{2}-\\d{2}\\s+([^\\d]+)[\\d]+\\s+([^\\d]+)[\\d]+\\s*$";
    // Pattern with multiline and insensitive options
    Pattern p = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE );
    // Match each line in the input
    Matcher m = p.matcher(input);
    // Loop over the matches
    while (m.find()){
        // first team name
        String first = m.group(1).trim();
        // first team score
        Integer firstScore = new Integer(m.group(2).trim());
        // second team name
        String second = m.group(3).trim();
        // second team score
        Integer secondScore = new Integer(m.group(4).trim());

        // is the first team the home team?
        if ( first.startsWith("@") ){
            // did they win?
            if (firstScore > secondScore){
                System.out.println("The home team " + first + " won " + firstScore + " to " + secondScore + " over " + second);
            } else {
                System.out.println("The home team " + first + " lost to " + second + " " + firstScore + " to " + secondScore);
            }
        } else 
        // is the second team the home team?
        if ( second.startsWith("@") ){
            // did they win?
            if (secondScore > firstScore){
                System.out.println("The home team " + second + " won " + secondScore + " to " + firstScore + " over " + first);
            } else {
                System.out.println("The home team " + second + " lost to " + first + " " + secondScore + " to " + firstScore);
            }
        } else {
            System.out.println("Both teams - " + first + " and " + second + " - were away.");
        }
    }
}

Example output:

The home team @South Alabama won 72 to 54 over Sam Houston St
Both teams - Auburn and Detroit - were away.
The home team @Missouri KC won 65 to 59 over Air Force
The home team @Stetson lost to Ga Southern 51 to 67

Upvotes: 1

Related Questions