2 Attachment(s)
Some help with regex and loop
Hi All
I have a bit of a problem with my program.
The program that I create lets you search for a file and choose a specific .csv file.
Then using generateResult() method that I have defined, it should be able to recognize any occurence of Pattern "[a-zA-Z]\\d{5}" and return as many match as there is in the input file.
My code is as follows:
Code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.util.regex.*;
public class Exam extends JFrame implements ActionListener{
public JFileChooser jfc = new JFileChooser(".");
JFrame frame = new JFrame();
public JButton findButton = new JButton("Find");
public JButton resultButton = new JButton("Generate result");
public JButton generateFileButton = new JButton("Generate new result file");
public JTextField filePathField = new JTextField();
public File input = null;
public File output = null;
public String result;
public Exam(){
JPanel panelTop = new JPanel();
panelTop.add(findButton);
panelTop.add(filePathField);
panelTop.setLayout(new FlowLayout());
JPanel panelBottom = new JPanel();
panelBottom.add(resultButton);
panelBottom.add(generateFileButton);
panelTop.setLayout(new GridLayout());
this.getContentPane().add(panelTop,BorderLayout.CENTER);
this.getContentPane().add(panelBottom,BorderLayout.SOUTH);
findButton.setToolTipText("Click to find the result sheet input");
resultButton.setToolTipText("Generate result from the input");
findButton.addActionListener(this);
resultButton.addActionListener(this);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Examination Decision Support Tool");
this.setResizable(false);
this.setVisible(true);
this.pack();
}
public void getFile(){
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = jfc.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
input = jfc.getSelectedFile();
filePathField.setText(input.getPath());
}
}
public void generateResult(){
if (input == null) //may need to change to if (input == null || output null) later on
return;
try{
BufferedReader reader = new BufferedReader (new FileReader(input));
//Scanner in = new Scanner(reader);
Pattern candidateNumberPattern = Pattern.compile("[a-zA-Z]\\d{5}");
Matcher matcher = candidateNumberPattern.matcher(reader.readLine());
while(matcher.find()){
for (int i = 1; i <= matcher.groupCount(); i++ ){
result = matcher.group();
}
}
reader.close();
}
catch(Exception e){
}
System.out.println("The candidate number found is" + " " + result);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == findButton)
getFile();
else if (e.getSource() == resultButton)
generateResult();
}
public static void main(String[] args) throws IOException{
new Exam();
}
}
When tested against this test input file:
Attachment 274
it returns
"The candidate number found is null"
when it's supposed to return:
"The candidate number found is Z11236"
"The candidate number found is Z11243"
"The candidate number found is Z54362"
using one click of the "Generate Result" button.
1. I think the problem is with my loop, but I already ran out of idea how to do it.
2. Also, I'm not sure if it's also caused by me using the FileReader (since the input file contains a space character). Any method to read the next line after you've finished with the previous ones?
3. Can anyone help me with this, preferably giving a working example. I have spent 5 hours on this to no avail.
Really appreciate any help on this.