0

I have a text file in the following format:

0 (1, 10), (0, 5), (3, 40)  
1 (2, 15), (1, 23), (2, 18), (4, 5), (3, 50)  
2 (3, 100)  

The first number in every line is used for a specific purpose. Similarly, each of the integers in parenthesis is used for another specific purpose. Therefore, I am trying to extract each of these integers separately. This is what I have tried:

BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line = br.readLine();
while (line != null) {
     String[] separate = line.split(", ");
     for (int i = 0; i < patterns.length; i++) {
        String result = separate[i].replace("(", "").replace(")", "");
     }
}  

While this does extract each integer, there is no distinction between the integers in brackets and the first one in the line. Also, this solution does not separate each line in the file.
Could someone please assist me with a better solution to this?

Michael
  • 41,989
  • 11
  • 82
  • 128
craig-nerd
  • 718
  • 1
  • 12
  • 32
  • See here: https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets – bradimus Jun 19 '17 at 14:23
  • Use Regex and capture everything in parenthesis first, then you could `split` the matched pieces by `,` – QBrute Jun 19 '17 at 14:23
  • Can you post the desired output? – bradimus Jun 19 '17 at 14:44
  • @bradimus I need these values to pass into the constructor to create an object. The first initial lone integer is the ID, the first digit in each of the parenthesis is the branch number and the second one is the number of hours worked in each branch. So I need to extract these and create an object as: `Employee (int ID, int branchNo, int hoursWorked)`. The number branches can vary for each employee, so I'm finding it difficult to extract the values in parenthesis for each employee – craig-nerd Jun 19 '17 at 16:33

2 Answers2

0

It looks like in your case it does not matter if the integers are in parenthesis or not. If so, you can do it this way.

String [] separate = line.replaceAll("[()\\s]+", "").split(",");

The replace above will replace multiple appearance of any of the symbols listed in [] with nothing. After it you would have a clean comma-separated string without parens or spaces.

separate[0] will point to the first element in the line.

Serge
  • 11,616
  • 3
  • 18
  • 28
0

I'd use regular expressions for this.

The pattern ^\d+ will get you an integer at the start of the line.

The pattern ((?<=\()\d+)|(\d+(?=\))) will get you any integers directly preceded by or followed by parentheses. It looks scarier than it is. You can break it down like so:

(A)|(B)

Which is simply A or B.

A =   (?<=\()\d+

Which is one or more digits \d+ preceded by a literal '(' (?<=\().

B =   \d+(?=\))

Which is one or more digits \d+ followed by a literal ')' (?=\))

Here's a little Java program which works on a single line. It could be easily adapted to work on all lines:

String line = "345 (1, 10), (0, 5), (3, 40)";

// Get the number at the start of the line:
final Matcher m = Pattern.compile("^\\d+").matcher(line);
if (m.find())
{
    System.out.println(m.group());
}

// Loop over any numbers in parentheses:
final Matcher mm = Pattern.compile("((?<=\\()\\d+)|(\\d+(?=\\)))").matcher(line);
boolean firstInPair = true;
while(mm.find())
{
    String name = firstInPair ? "First" : "Second";
    System.out.println(name + " " + mm.group());
    firstInPair = !firstInPair;
}

Sample output:

345
First 1
Second 10
First 0
Second 5
First 3
Second 40
Michael
  • 41,989
  • 11
  • 82
  • 128