-3

Im trying to create a code where I read a double and print out its square, but I also want it to know when the user enters a negative or non double constant and make them enter a new number. Im having trouble with the InputMismatchException. My code does not work properly, it compiles but the compiler just runs forever. Any suggestions would be helpful.

import java.util.*;
class constants 
{
public static void main(String[] args)
{
    double constant = getConstant();
    System.out.println("Square of " + constant + " = " + constant*constant);
}
//-------------------------------------------------------------------------------
public static double getConstant()
{
Scanner kb = new Scanner(System.in);

System.out.println("Enter non-negative double constant");

double constant = kb.nextDouble();
try { 
        double selection = kb.nextDouble();
    }


    catch (InputMismatchException e) // where there is error. 
        {
            System.out.println("Not a double constant. Re-enter");

    }
    return constant;
  }

}

2 Answers2

0

I understand that you are looking for the user to input such values as 15.65, 145.95, etc, but that -5.85 (negative) and 11 (integer value) should be rejected. The fact is, in java any integer is also a double

Example:

double x = 100;   // is correct
double y = -15.85 // is correct

Therefore they will not generate an input mismatch exception. For that you have to check explicitely that these conditions are met and you will also have to explicitely throw the InputMismatchException.

It is also better to define your scanner once, for example as a global static variable (otherwise you may face issues if you use call getConstant() in a loop for example)

You don't need to define the selection double value. Here is an illustration that works

import java.util.InputMismatchException;
import java.util.Scanner;

class Constants 
{
    private static Scanner kb = new Scanner(System.in);
    public static void main(String[] args)
    {
        double constant = getConstant();
        if (constant >= 0) {
            System.out.println("Square of " + constant + " = " + constant*constant);
        }
    }
    //-------------------------------------------------------------------------------
    public static double getConstant()
    {

        System.out.println("Enter non-negative double constant");

        double constant=-1.0D;
        try { 
            constant = kb.nextDouble();
            // you don't want the negative value neither the integer value
            // so you reject them by throwing the InputMismatchException
            if (constant <0 || Math.floor(constant) == constant * 1.0D) {
                constant = -1.0D;
                throw new InputMismatchException("Not a double constant. Re-enter");

            }
        }

        catch (InputMismatchException e) // where there is error. 
            {
                System.out.println("Not a double constant. Re-enter");
        }
        return constant;
      }
}
alainlompo
  • 4,414
  • 4
  • 32
  • 41
0

Here is how it can be done, the exception you need to catch is NumberFormatException. One thing though, negative numbers can still have squares, they just can't have square roots.

 public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
      try { 
        double temp = kb.nextDouble();
        //If the input is not a double, catch the number format exception
      } catch (NumberFormatException e) {
        e.printStackTrace();
      }
      //If the number is in proper format, (can be negative) print its square.
         System.out.println("Square of " + temp+ " = " + temp*temp);
   }

For some reason if you don't want to print squares of negative numbers, just check that condition before printing the result.

Ishaan
  • 67
  • 4