Next, copy a file containing some (fictional) credit card numbers into the directory you just created. The file is available on Unix in the file
/home/gtowell/Public/CS113/HW6/cc.txt
Note that the "/" at the beginning of this file is important. Be sure to get everything in this name or the copy will fail. Use cp or scp to copt this file to your Unix account or laptop, respectively.
import java.io.FileReader;
public class VerifyOne {
public static int[] readANum(FileReader fr) {
try {
if (!fr.ready()) { // there is nothing more to read.
return new int[0];
}
int[] res = new int[1];
res[0] = fr.read();
return res;
} catch (Exception ee) {
return new int[0];
}
}
public static void main(String[] args) {
try {
FileReader fr = new FileReader("cc.txt");
int[] card = readANum(fr);
if (card.length > 0) {
System.out.println(card[0]);
}
} catch (Exception ioe) {
System.out.println("Error " + ioe);
}
}
}
In the code above you open the FileReader in the main method, but then use it in the readANum method. Java, being Java, requires that opening a File reader be surrounded by a try..catch because the opening could fail if, for example, the file did not exist.
In the readANum function we again need to wrap everything in a try..catch because the reading could fail (for example, the file was deleted between the time it was opened and the time you read from it). So then, in readANum, first check to see if there is anything to read; if not return an array of length 0. The method signature requires that an array be returned. (Arrays of length zero are perfectly legal, if usually really stupid because they cannot contain anything.) If the read is successful, put the thing read into an array and return that array. Finally, if something horribly wrong happens, and the catch occurs return an array of length 0, exactly as if there was nothing more to read.
The first thing you need to do is adapt the readANum method so that instead of reading one character as an integer, it reads 16 characters (as integers) and stores those 16 into an array. One challenge is that the integer you get from "fr.read()" is the ASCII value of the character. The ASCII value of the character '0' is NOT 0. But the ASCII value of '1' is one greater than the ASCII value of '0'. Similarly, the ASCII value of '2' is one greater than '1'. (etc)
Also there is one invisible character at the end of every line. So, when you read the 16 digits of a credit card, be sure to read one extra character (and throw it away).To make your lives at little simpler, the data file is guaranteed to have 16 digits on each line. So you do not need to do any data validation on the input file.
Once you have your method for reading a single credit card number working correctly, adjust the main function so that it can read all of the credit card numbers in the file. (Use a loop.) The biggest challenge here will be recognizing in the main function that there are no more credit cards to be read. Suggestion, study the readANum method above; in particular what does it do when "fr" is not "ready"? You can use this idea to signal back to the main function that there are no more cards.
[1, 2, 2, 4, 5, 4, 4, 2, 6, 5, 5, 2, 7, 2, 2, 6]
a "nice" representation might look like
1224 5442 6552 7226
1224 5442 6552 7226 invalid
1234 5678 9012 3456 invalid
1111 2222 3333 4444 invalid
2505 1988 0544 2775 valid
2598 1988 1844 2775 valid
You should have at least 3 files in your HW5 directory: Verify.java, cc.txt and Readme. (You might also have .class files.)
cd
cd CS113
mkdir HW6
Once you have made the HW6 directory in Unix, open a terminal on you own computer and in that terminal use "cd" to navigate to the directory containing your work for this assignment. Assuming you use the same directory structure on your own computer and in the lab, this process can be accomplished with the following commands
cd
cd CS113
cd HW6
Then use the scp command to copy each of the files you want to submit from your computer to the lab. For example:
scp Readme UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW5/Readme
As always, when you read "UNIX_NAME" put in your UNIX user name. Also, with each scp command you will need to enter your UNIX password.
cd
cd CS113
/home/gtowell/bin/submit -c 113 -d HW6 -p 6
In response to the submit command you should see a series of messages ending with:
Submitting archive...
Submission complete! Submission timestamp is 2023-08-08-15-30-28-EDT.