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 2 of 2

Thread: input mismatch exception

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default input mismatch exception

    I need to use InputMismatchException and FileNotFoundExeption with a try catch. I got the file not found exception to work but my input mismatch exception wont work and I don't know why.

    i have two test files for this. the code essentially is supposed to add up all the numbers in a file and report the answer. my test file 1 is simple the numbers are 1.0 2.0 3.0 4.0 and 5.0, it works and adds up to 15 and gives me back 15 like its supposed top. If i put in a bad file name my first exception works and it says file does not exist.

    my second test file says 1.0 2.0 3f 4.0 and 5.0. when i use my second file its supposed to say only numbers are accepted, instead it just gives me back a 3, i guess it just stops at 3 once it sees the f and quits the program. i want it to say only numbers accepted.
    can someone help here is my code:
    import java.util.*;
    import java.io.*;
     
    public class Program2
    {
        public static void main(String []args)
        {
            try
            {
              Scanner i = new Scanner(System.in);
              System.out.println("Enter The File Name: ");
              String file = i.nextLine();
              File input = new File(file);
              Scanner out = new Scanner(input);
              double total = 0;
            while(out.hasNextDouble())
            {
                total += out.nextDouble();
            }
             System.out.println(total);
            }   
     
            catch (FileNotFoundException e)
            {
                System.out.println("File does not exist.");
            }
            catch (InputMismatchException e)
            {
                System.out.println("Only Numbers are accepted.");
     
            }
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: input mismatch exception

    while(out.hasNextDouble())
    Would this protect the nextDouble() from having a input mismatch?

    gives me back a 3
    1.0 + 2.0 = 3
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. File Input and Output and Exception Handling
    By MagicTricksKill in forum File I/O & Other I/O Streams
    Replies: 21
    Last Post: January 21st, 2013, 07:52 PM
  2. Exception error display with one input and not the other
    By worldchamp in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 4th, 2012, 01:45 PM
  3. Type mismatch: cannot convert from void to ClassX.MethodX
    By RevMoses77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2012, 12:05 PM
  4. user input mismatch problem
    By JavaN00b101 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2012, 07:02 AM
  5. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM