Senior Papas Senior Papas - 3 months ago 7
Java Question

Where is my intent error coming from?

Everything in my program seems to work fine, but for some odd reason whenever it gets to the input section where you choose multiply or divide it then terminates the program as if it had finished.
Whats wrong with this?
Ive tried changing if(Choice.equals(Multiply))
to "Multiply"
and the same for the other as well. But no luck.

import java.util.Scanner;
public class APJAVAPROGRAM {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner numberinput = new Scanner(System.in);
Scanner stringinput = new Scanner(System.in);

System.out.print("Input a numerator >>>"); // inputs first Numerator
double Numerator = numberinput.nextDouble();

System.out.print("Input a Denominator >>>"); // Inputs first denominator
double Denominator = numberinput.nextDouble();


System.out.print("Input a second numerator >>>"); //Inputs second Numerator
double Numerator2 = numberinput.nextDouble();

System.out.print("Input a second Denominator >>>"); // Inputs second denominator
double Denominator2 = numberinput.nextDouble();


System.out.println("Your entered fractions are " + Numerator + "/" + Denominator + " and "+ Numerator2 + "/" + Denominator2);
// Will print out the entered fractions

System.out.println("What would you like to do with these?"); //asks user what should be done
System.out.print("Multipy, Divide, Add, or Subtract >>>");
String Choice = stringinput.nextLine();

double Multiply = Numerator * Numerator2;
double Multiply2 = Denominator * Denominator2;
if(Choice.equals("Multiply")){
System.out.println(Multiply + "/" + Multiply2 + " Is your answer.");
// If the input is multiply it will do as such
}
else{
double Divide = Numerator * Denominator2;
double Divide2 = Numerator2 * Denominator;
if(Choice.equals("Divide"))
System.out.println(Divide + "/" + Divide2 + " Is your answer.");
}
}
}

Answer

.equals(Object obj) is case sensitive, if you type in "Multiply" or "Divide" with a capital letter it runs correctly.

Use the .equalsIgnoreCase(Object obj) for String to String comparison, ignoring case considerations

Comments