Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: allow a new input, dicarding the last mismatch input without terminating the program

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default allow a new input, dicarding the last mismatch input without terminating the program

    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();
        }
     
     
     
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: allow a new input, dicarding the last mismatch input without terminating the prog

    change where your try catch block is so that it is withing the loop and encompasses the input section

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    voltaire (April 9th, 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    23
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: allow a new input, dicarding the last mismatch input without terminating the prog

    Quote Originally Posted by Freaky Chris View Post
    change where your try catch block is so that it is withing the loop and encompasses the input section
    that was a university homework
    sigh. I thought I couldn't do it
    Thank you ..THANK YOoooooooU Freaky Chris

Similar Threads

  1. Simple Input/Output program Acting weird
    By drexasaurus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 02:15 PM
  2. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM
  3. Difference between input.next and findInLine(".")charat(0)
    By lotus in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 6th, 2009, 05:10 AM
  4. Replies: 9
    Last Post: June 27th, 2009, 04:05 PM
  5. Program to mask input
    By sah in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2009, 06:43 PM