2

I want my program to keep asking the user to enter the numerator and if the input was a number it asks for the denominator it checks if it is 0 or not if 0 it shows error message and if not it completes division

but if the numerator was a string it sends an error message but if it is E or e it exits the program

but the problem is that it asks me to enter the numerator twice and it doesn't ask me to enter the denominator and it devises the first number I enter over the second even if I enter a string it asks me to enter the string twice

package Division;
import java.util.Scanner;
public class Division{

    public static void main(String[] args) {
    Object n,d;


    Scanner input = new Scanner(System.in);


        do{ 
            System.out.println("Enter the numerator: ");
            n = input.next();
            System.out.println("");

            if(input.hasNextDouble())
            {

                System.out.println("Enter the denominator: ");
                d = input.nextDouble();
                if (d==0){
                    System.out.println("You can't divide "+n+" by 0");
                }
                else{
                    System.out.println(n+"/"+d+" is"+Double.valueOf((String) n)/Double.valueOf((double) d));
                }
            }
            else if(input.hasNextLine()) {
                if(n.equals("e")||n.equals("E")){
                    System.out.println("Good Bye");
                }
                else{
                    System.out.println("You entered bad data. Please try again.");
                }
            }
        }while(n.equals("e")==false||n!=n.equals("E")==false);

  }
}
replica
  • 23
  • 5
  • WTF does this do ---> `while(n.equals("e")==false||n!=n.equals("E")==false);` !!! – Am_I_Helpful Nov 13 '15 at 21:33
  • it keeps the statement running in the loop as long as it does not equal E or e. It works but I think that my problem is with the main if statements though I don't know why – replica Nov 13 '15 at 22:05
  • You shouldn't use Object; you should stick to Double/Integer, and check the same using if-block, throw exception and quit the program. That'd do; using Objects is a pathetic one, also the last part of your while loop also appears pretty confusing to me. – Am_I_Helpful Nov 14 '15 at 05:21
  • I tried using double and throw statements but then the program didn't exit when I entered "e" also it didn't show the error message for when I divide over 0. Every time I fix something in this program something else turns out wrong. My while statement is not wrong and it works well, I don't feel it's complicated or anything but I'm still working on this program so I might change it. – replica Nov 14 '15 at 16:36
  • It didn't exit that time because your while-loop condition is wrong as I've already pointed out. – Am_I_Helpful Nov 14 '15 at 16:53

0 Answers0