Makes multiple tries at accessing file possible @Override public void actionPerf
ID: 3690890 • Letter: M
Question
Makes multiple tries at accessing file possible @Override public void actionPerformed(ActionEvent e) { if (!MakeProgramRobust.getvalidNumberEntered() || ! MakeProgramRobust.getNumberInFileAccessed()) try{ theFile = new File(fileNameField.getText()); in = new Scanner(theFile); int num = in.nextlnt(); theNumberField.setText(num + " "); MakeProgramRobust.setNuwberlnFileAccessed(); } catch(FileNotFoundException ex) { jOptionPane.showMessageDialog(null, "File not found!"); fileNameField.setText("File name"); } }Explanation / Answer
Here in this actionPerformed, we are doing following things:
1. Initially in IF condition we are checking boolean values of methods "getValidNumberEntered" and "getNumberInFileAccessed". If any one of these two methods returns false then if condition becomes true.
2. In IF condition, we are taking file name from field "fineNameField" and then creating a file object named "theFile". Then we are passing this file object to Scanner. Then using this scanner object we are reading integer value from file in interger variable "num". Then setting the value of this num variable in "theNumberField".
3. And at the end of IF condition we are also calling method "setNumberInFileAccessed"
4. Suppose while creating a file object, if we get an error, due to file not found, then we are showing this error in popup.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.