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: How to fix InputMismatchException?

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default How to fix InputMismatchException?

    I made this code which has got two separate classes
    this code is a calculator program which i am practicing on
    it takes user input and then converts them into arrays
    I think the user input is out of range when we are supposed to enter either 1,2,3,4 for selecting the calculation type
    take your time to explain why is it out of range, which part of the program is causing this and how do i solve it
    sorry if it's getting annoying or stupid, I am new and learning all by myself
    here is the main class
     
    import java.util.*;
     
     
    public class main {
    	public static void main(String args[]) {
    		//creation of objects
    		Scanner input = new Scanner(System.in);
    		Scanner input1 = new Scanner(System.in);//this takes input
    		List <Double> quantity = new ArrayList<Double>(); //list cant work without array object to convert into array
    		formula formula = new formula();                        //creating object of class "formula" to acess its methods aka 
                                                                                       formulas
    		                   //creation of variables
    		int digit;
    		                 //taking of user input(numbers)
    		System.out.println("enter your numbers");
     
    		while (input.hasNextDouble()) {                      //reads as while input has
    			quantity.add(input.nextDouble());          //quantity will store all input in list and now we need to cinvert it 
                                                                                  to aray
    		}
        	     Double []quantityarray = new Double[quantity.size()];
                                                                          /*size of array is fixed so we dceclare. array no. needs to 
    		                                                      we need to wriet the full name of the variable type.
    		                                                      the size of array after user input
    		                                                      The size of this array = size of arraylist(quantity)*/
    	    quantity.toArray(quantityarray);              /* now we transfered all the values inside arraylist to array 
                                                                         "quantityarray*/
    	   for (Double x:quantityarray) { System.out.print(x+", "); }
     
    	  int lenght = quantityarray.length;             /*we took the legth of the array to check if its 2 number or 1 or more*/
    	  if (lenght<=2) {
    		if (lenght==1) {
    			System.out.println("since you entered only one number, we only support power");
    			System.out.println("enter the power of your number");
    			digit = input.nextInt();
    			System.out.println("the answer is"+formula.power(quantityarray[0], digit));
    		} 
     
    			System.out.println("press 1 for addition, 2 for subtraction, 3 for multiplication, 4 for division");
     
    	        }
                input.close();
     
    	    digit = input1.nextInt();
     
     
    		          /*now we create a switch and if digit = switch case no. it will
                                                 execute that case*/
    			switch (digit) {
    			case 1 : 
    				formula.addition(quantityarray); 
    			case 2 : 
    				formula.subtraction(quantityarray[0],quantityarray[1] );
    			case 3 :
    				formula.multiplication(quantityarray);
    			case 4 :
    				formula.division(quantityarray);
    		}
     
     
    		if (lenght<2) {
    			System.out.println("press 1 for addition, 2 for multipication");
    			digit = input1.nextInt();
    			switch (digit) {
    			case 1 : 
    				formula.addition(quantityarray); 
    			case 2 : 
    				formula.multiplication(quantityarray);
    		}
     
    	    	}
    		input1.close();
    	        }
     
                     }
    and here is the secondary class i made to store methods
    public class formula {
    	public double power (double a, double b) {
    		double power = Math.pow(a, b);
    		 return power ;
     
    }
    public double subtraction(double a, double b) {
    	double result = a - b;
    	return result;
    }
    public double multiplication(Double... d)           /*"..." is needed cuz array has a lot  of numbers inside it not one*/
    {
    	double sum = 1;
    	for (double x:d)                                /*using enhanced for loop instead of regular loop*/
    		sum = sum * x;
    	return sum;
    }
     
    public double addition(Double... d)             //capital Double when using arrrays with unknown no. of arguments
    {
    	double sum=0;
    	for (double i: d) 
    {
    		sum = sum + i;
     
    }
    	return sum;
    }
    public double division(Double...d) {
       double sum = 1;
    	for (double i:d)                                      /* "int i" wont work as "d" is also double*/
    {
    		 sum = i/sum;
    } return sum;
    }
     
    }


    --- Update ---

    Update :- I think Ifound that it is because when it" asks for number " test if it's wrong and correct me. I am at my my office so I have no way to test it right now
    System.out.println("enter your numbers");
     
    		while (input.hasNextDouble()) {                      //reads as while input has
    			quantity.add(input.nextDouble());
    I entered numbers which aren't double
    Eg :-
    1
    5
    3
    6
    Which doesn't have any decimal points.. But in calculator we never really know when user's input will be integer or double.
    Is there any way to solve this
    For example my numbers are
    32 and 21.3
    The 32 will cause the mismatch I guess
    Since it's integer. Is there anyway for the integer to be converted to 32.0 automatically by any pre made method?

    --- Update ---

    Update :- I think Ifound that it is because when it" asks for number " test if it's wrong and correct me. I am at my my office so I have no way to test it right now
    System.out.println("enter your numbers");
     
    		while (input.hasNextDouble()) {                      //reads as while input has
    			quantity.add(input.nextDouble());
    I entered numbers which aren't double
    Eg :-
    1
    5
    3
    6
    Which doesn't have any decimal points.. But in calculator we never really know when user's input will be integer or double.
    Is there any way to solve this
    For example my numbers are
    32 and 21.3
    The 32 will cause the mismatch I guess
    Since it's integer. Is there anyway for the integer to be converted to 32.0 automatically by any pre made method?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,320
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: How to fix InputMismatchException?

    anyway for the integer to be converted to 32.0 automatically
    Assign the int's value to a double variable.

    The Scanner class has hasNext... methods that can be used to test for int or double values.
    If you don't understand my answer, don't ignore it, ask a question.

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

    kukupie123 (March 30th, 2019)

Similar Threads

  1. [SOLVED] InputMismatchException
    By mrivera85 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2014, 11:49 AM
  2. Solve InputMismatchException
    By idkwtp100 in forum Exceptions
    Replies: 0
    Last Post: February 28th, 2013, 12:28 AM
  3. using java.util.InputMismatchException with try
    By itayj in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 26th, 2011, 06:24 PM
  4. [SOLVED] InputMisMatchException
    By jmack in forum Exceptions
    Replies: 3
    Last Post: January 26th, 2011, 09:30 AM
  5. INPUTMISMATCHEXCEPTION
    By james in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 15th, 2010, 12:02 AM

Tags for this Thread