Results 1 to 9 of 9
  1. #1
    Pat
    Pat is offline Member
    Join Date
    Oct 2012
    Location
    Dallas
    Posts
    5
    Rep Power
    0

    Default throw new InputMismatchException

    I think I understand what this error is saying but I dont understand how to correct it. This is a program we were asked to write in beggining Java class.
    any help would be aprreciated.

    The error happens after enter to real numbers prints in the console.

    Thanks for your time
    Pat
    ************************************************** ****************

    Java Code:
    package util;
    
    import java.util.*;
    import javax.swing.JOptionPane;
    
    public class IO {
       
        
        public static void showMsg(String s) {
        JOptionPane.showMessageDialog(null, s);
        System.out.println(s);
        }
        
        public static Scanner getInput(String prompt) {
        String s = JOptionPane.showInputDialog(prompt);   
        return new Scanner(s);
        }
        
        public static Scanner getConsoleInput (String s) {
        System.out.println(s);
        return new Scanner(s);   
        }
    }
    ************************************************** *****************
    Java Code:
    package assignment4;
    
    import java.util.*;
    import javax.swing.JOptionPane;
    import util.IO;
    
    public class Assignment4 {
        
        public static void main(String[] args) {
        Scanner scan = IO.getInput("Enter 2 integers");
       
        int x = scan.nextInt();
        int y = scan.nextInt();
        if (x > 0 && y > 0)
        {IO.showMsg(String.format("The sum is %5d", x+y)); }
        else
        {JOptionPane.showMessageDialog(null, "At least one of the numbers is negative");}
       
       
       
        Scanner input = IO.getConsoleInput("Enter 2 real numbers");
        Double a = input.nextDouble();
        Double b = input.nextDouble();
        if (x > 0 && y < 0)
        {IO.showMsg(String.format("the product is: %7.2f", a*b));}
        
        else if (x < 0 && y < 0)
        {IO.showMsg(String.format("the quotient is: %7.2f", a/b));}
        
        System.exit(0);
        
        
        }
        }
    Last edited by Pat; 10-08-2012 at 04:43 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: throw new InputMismatchException

    Please use [code] tags [/code] when posting code.
    Also you need to give the full exception plus stack trace and highlight the line of code it occurs on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Pat
    Pat is offline Member
    Join Date
    Oct 2012
    Location
    Dallas
    Posts
    5
    Rep Power
    0

    Default Re: throw new InputMismatchException

    I am new to the forum and Java, I have looked for what you mean by code tags in the FAQ's still not sure what you want, also I copied and paste the exception from netbeans IDE, so need assistance for what you want. Not sure about stack trace as we hav not covered it. we jumped from if's to methods. I really am new to Java started class 4 weeks ago so I am partially overwhelmed.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: throw new InputMismatchException

    wrap your code in code tags.
    [code] before it and [/code] after it.
    Without them it's almost unreadable.

    Is this problem an exception or compilation error?
    If you cannot answer that then I suggest not using the IDE and using the command line to compile with as you'll pretty quickly figure out the difference then.

    If you can answer that then what line is causing the error?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Pat
    Pat is offline Member
    Join Date
    Oct 2012
    Location
    Dallas
    Posts
    5
    Rep Power
    0

    Default Re: throw new InputMismatchException

    Please Ignore this I am checking to see if I labeled the code proerly.
    package util;

    import java.util.*;
    import javax.swing.JOptionPane;

    public class IO {


    Java Code:
    public static void showMsg(String s) {
     JOptionPane.showMessageDialog(null, s);
     System.out.println(s);
     }
    public static Scanner getInput(String prompt) {
    String s = JOptionPane.showInputDialog(prompt);
    return new Scanner(s);
    }

    public static Scanner getConsoleInput (String s) {
    System.out.println(s);
    return new Scanner(s);
    }
    }

  6. #6
    Pat
    Pat is offline Member
    Join Date
    Oct 2012
    Location
    Dallas
    Posts
    5
    Rep Power
    0

    Default Re: throw new InputMismatchException

    I coded the orginal post. The mismatch is at line 22 of the secand part of code.
    Here is what is at the bottom of the console after running:

    The sum is 5
    Enter 2 real numbers
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextDouble(Scanner.java:2456)
    at assignment4.Assignment4.main(Assignment4.java:26)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: throw new InputMismatchException

    Why are you using a Scanner with a JOptionPane?
    Why not simply take the returned String and parse it into a Double (eg Double.parseDouble())?
    That, I would argue, is the root of your problem.
    Either use the command line for data entry, or a GUI. By mixing things up you are probably hitting strange problems with interpreting the input.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Pat
    Pat is offline Member
    Join Date
    Oct 2012
    Location
    Dallas
    Posts
    5
    Rep Power
    0

    Default Re: throw new InputMismatchException

    i am using both because it is a requirement of the teacher. I used the parse and dropped the scanner and it works and dropped the gui and just used scanner this weekend and it works but both together do not.

    thank you

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: throw new InputMismatchException

    The Scanner has (to my mind) some quirks, and I suspect it was one of those quirks (largely around how it handles new lines) that might have been causing the problem with parsing the double. There probably is a workaround, but I just don't see the advantage of digging it out when a simple parse does the job.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. InputMismatchException
    By gnbradford in forum New To Java
    Replies: 3
    Last Post: 09-22-2012, 09:45 PM
  2. Replies: 1
    Last Post: 09-20-2012, 09:54 AM
  3. InputMismatchException
    By jihad in forum New To Java
    Replies: 5
    Last Post: 12-17-2011, 03:10 PM
  4. Replies: 2
    Last Post: 02-11-2011, 09:03 AM
  5. try-catch InputMismatchException in a while loop
    By themulator in forum New To Java
    Replies: 17
    Last Post: 10-12-2010, 04:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •