I have a bufferedReader
which should read one line of a text file (which is encoded), decode it, and check whether it contains a particular set of numbers. The part where it checks to see whether it contains a particular set of numbers is fine, however, I have a problem that it reads through the entire file, my code below, as well as the output from what is read from the file as well as how it should be read is provided below:
Code:
prviate static final String UTF8_BOM = "\uFEFF";
String cardNumberStr = "106382076";
String lineFromFile = null;
lineFromFile = bufferedReaderToWrite.readLine();
if (lineFromFile.startsWith(UTF8_BOM)) {
lineFromFile = lineFromFile.substring(1);
}
lineFromFile = lineFromFile.substring(1, lineFromFile.length()-1); //Convert bytes read back to String
for(String s: lineFromFile.split(", ")){ //Split every byte on ,
sb.append((char) Integer.parseInt(s)); //Convert byte to char
}
String textToCheck = sb.toString(); //Convert StringBuilder to String
System.out.println(textToCheck);
System.out.println();
System.out.println(cardNumberStr);
if(textToCheck.contains(cardNumberStr)){
System.out.println(lineFromFile);
}
Output from what is read from file:
106382076
78, 97, 109, 101, 32, 116, 101, 115, 116, 32, 99, 97, 114, 100, 78, 111, 32, 52,
48, 52, 54, 48, 52, 57, 51, 57, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 9
7, 108, 97, 110, 99, 101, 32, 51, 49, 48, 32, 111, 118, 101, 114, 100, 114, 97,
102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102,
116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 50, 50, 50
Name test cardNo 404604939 Current Balance 310 overdraft false overDraftLimit 0
pin 2222Name account cardNo 106382076 Current Balance 132 overdraft false overDr
aftLimit 0 pin 4444Name test cardNo 404604939 Current Balance 310 overdraft fals
e overDraftLimit 0 pin 2222Name account cardNo 106382076 Current Balance 132 ove
rdraft false overDraftLimit 0 pin 4444`
It should output:
Name test cardNo 404604939 Current Balance 310 overdraft false overDraftLimit 0
pin 2222
Name account cardNo 106382076 Current Balance 132 overdraft false overDr
aftLimit 0 pin 4444
The contents of the text file is as folows:
[78, 97, 109, 101, 32, 116, 101, 115, 116, 32, 99, 97, 114, 100, 78, 111, 32, 49, 56, 57, 52, 57, 51, 50, 56, 52, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 97, 108, 97, 110, 99, 101, 32, 57, 52, 32, 111, 118, 101, 114, 100, 114, 97, 102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102, 116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 51, 50, 51]
[78, 97, 109, 101, 32, 116, 101, 100, 116, 32, 99, 97, 114, 100, 78, 111, 32, 55, 48, 49, 55, 50, 53, 50, 51, 49, 32, 67, 117, 114, 114, 101, 110, 116, 32, 66, 97, 108, 97, 110, 99, 101, 32, 50, 54, 48, 32, 111, 118, 101, 114, 100, 114, 97, 102, 116, 32, 102, 97, 108, 115, 101, 32, 111, 118, 101, 114, 68, 114, 97, 102, 116, 76, 105, 109, 105, 116, 32, 48, 32, 112, 105, 110, 32, 50, 50, 50, 50]`