I've got a text file with this format: image (Excluding the headers) I need to take the account number and pin as input from the user and then match it against the values in the text. That is, check if the pin is correct for the given account number. How would I do that?
You can parse the text file and fetch data as records using CSVParser and then you can match it with the user input.
Here is the sample code:
File dataFile = new File("dataFile.txt");
//delimiter is the character by which the data is separated in the file.
//In this case it is a '\t' tab space
char dataDelimiter = '\t';
try(InputStream is = new FileInputStream(dataFile);
InputStreamReader isr = new InputStreamReader(is);
//Initializing the CSVParser instance by providing delimiter and other configurations.
CSVParser csvParser = new CSVParser(isr, CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader())
)
{
for (CSVRecord csvRecord : csvParser)
{
long accNumber = Long.parseLong(csvRecord.get("A/C No."));
long pinNumber = Integer.parseInt(csvRecord.get("Pin"));
//-----------------
}
}
catch (IOException e)
{
e.printStackTrace();
}