I am a beginner in java programming
I am using NetBeans IDE 4.1
this code is supposed to do this :-
1- create a file "C:\\newTest.txt" . use PrintWriter for writing.
2- use Scanner input = new Scanner(System.in) to write from keyboard
3- take two values (int idNum, double bal) and add it to object (AccountRecord record)
4- use while loop to write a sequence of inputs and has a condition that check if (idNum > 0) then write to the file created, otherwise will not write to the file andl terminates
what is wrong with this point of code:
5- InputMismatchException is thrown if type other than int or double is entered.
What I need here is to allow a new input, dicarding the last invalid mismatch input without terminating the program
import java.io.*; import java.util.*; public class CreateClientsFile { private PrintWriter pr; public void openFile() { try { File f1 = new File("C:\\newTest.txt"); pr = new PrintWriter(f1); } catch(FileNotFoundException es) { System.out.println("Error: " + es); } } public void addRecords() { Scanner input = null; AccountRecord record = null; [COLOR=red]//AcountRecord has two fields int idNum and double bal[/COLOR] try { input = new Scanner(System.in); int idNum = input.nextInt(); double bal = input.nextDouble(); record = new AccountRecord(idNum, bal); int num = 0; while ((num = input.nextInt())>=0) [COLOR=red]//loop check the first field. if (idNum < 0) then terminate.[/COLOR] { pr.write("#" + idNum + " " + "Balance:$" + bal); System.out.println("to stop enter negative input for idNum"); input = new Scanner(System.in); idNum = input.nextInt(); bal = input.nextDouble(); record = new AccountRecord(idNum, bal); } } catch(InputMismatchException exx) { System.out.println("Error: You have entered invalid input " + exx); input = new Scanner(System.in); [COLOR=red]//What I need here is to allow a new input, dicarding the last invalid mismatch input without terminating the program.[/COLOR] } } public void closeFile() { pr.close(); } }