1

I'm trying to write a program that reads two numbers from the standard input and finds whether they are in the golden ratio and to print an error message if the input is not numeric. But the if/else with "instanceoff" isnt working properly, it comes up as an error if input is not numeric and the says its not a golden ratio even if it is.

Thanks

import java.util.Scanner;

public class GoldenRatio {

 public static void main(String[] args) {
 Scanner key = new Scanner(System.in);
 System.out.print("Enter two numbers: ");
 Double a = key.nextDouble();
 Double b = key.nextDouble();
 Double x;
 Double y;
 
 //Makes sure the bigger number becomes numerator
 if(a <= b){
  x= b;
  y= a;
 } else {
  x = a;
  y = b;
 }
 
 //Rounding decimal to 3 figures
 Double left = (x+y)/x;
 Double right = x/y;
 
 String leftS = String.format("%.3f", left);
 String rightS = String.format("%.3f", right);
 
 Double leftD = Double.parseDouble(leftS);
 Double rightD = Double.parseDouble(rightS);
 
 // meant to make sure arguments are doubles   
 if (a instanceof Double && b instanceof Double) {
  if (leftS == rightS) {
   System.out.println("Golden ratio!");
  } else {
   System.out.println(leftS);
   System.out.println(rightS);
   System.out.println("Maybe next time");
   System.exit(0);
  }
 }else {
  System.out.println("Invalid input");
  System.exit(0);
 }
 }
}
  • Your code is `Double a` so why do you need to do `if (a instanceof Double` ? – Scary Wombat Aug 16 '17 at 02:01
  • Please read [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) -- and replace `if (leftS == rightS)` with `if (leftS.equals(rightS))`. Also `a` and `b` are already of type `Double` so there is no point in doing the `instanceof` check on them. – Erwin Bolwidt Aug 16 '17 at 02:03
  • 1
    Do not post `Java` code as `HML`/`CSS`/`JavaScript` snippet... – Usagi Miyamoto Aug 16 '17 at 02:09
  • Is your problem catching input is numeric or not. I think that point is give error – Supun Kavinda Aug 16 '17 at 02:18
  • I need to do the instanceof because one of the requirements is making sure the input is a numeric character, for example if the user just typed "foo" and "bar" then its meant to print invalid input. Unless I'm using instanceof wrong and its meant for something else? –  Aug 16 '17 at 03:28
  • also how do i post it as java code? it only lets me post as html/css/js –  Aug 16 '17 at 03:32

2 Answers2

0

It's because you contruct A and B as doubles.

if ((a == Math.floor(a)) && !Double.isInfinite(a)) {
        // integer type
   }

This checks if the rounded-down value of the double is the same as the double.

Your variable could have an int or double value and Math.floor(a) always has an int value, so if your variable is equal to Math.floor(a) then it must have an int value.

This also doesn't work if the value of the variable is infinite or negative infinite hence adding 'as long as the variable isn't inifinite' to the condition.

Cardinal System
  • 2,749
  • 3
  • 21
  • 42
0

you should check Given Object belongs to Number or not then check for Double. Check below check for `Number'.

 if (a instanceof Number && a instanceof Double && b instanceof Double) {
            if (leftS == rightS) {
                System.out.println("Golden ratio!");
            } else {
                System.out.println(leftS);
                System.out.println(rightS);
                System.out.println("Maybe next time");
                System.exit(0);
            }
        } else {
            System.out.println("Invalid input");
            System.exit(0);
        }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43