0

Create a java program that will do the following:

a) Read three inputs from the keyboard,

• two input numbers each being a single digit (0…9)

• one character representing one of five operations : + (addition), - (subtraction), * (multiplication), / (division), and ^ (exponentiation)

b) output the description of the operation in plain English as well as the numeric results

import java.util.Scanner;

public class EnglishCalc {

       public static void main(String[] args)
       {
            Scanner input = new Scanner(System.in);

            System.out.println("Enter first number");
            int number1 = input.nextInt();          

            System.out.println("Enter second number");
            int number2 = input.nextInt();

            System.out.println("Enter operation: +,-,*,/,^");
            String operation = input.next();

            int output = 0;

           if(number1 < 0 || number1 > 9 || number2 < 0 || number2 > 9) {
                System.out.println("Number should be between 0 and 10");
           } 
              else if (operation.equals("+"))
           {
               output = number1 + number2;
               System.out.println("Sum of "+number1+" and "+number2+" is: " +output);
           }
               else if (operation.equals("-"))
           {
               output = number1 - number2;
               System.out.println("Subtraction of "+number2+" from "+number1+" is: " +output);
           }
               else if (operation.equals("*"))
           {   
              output = number1 * number2;
              System.out.println("Product of "+number1+" and "+number2+" is: " +output);
           }
               else if (operation.equals("/"))
           {
               if(number2 == 0) {
                   System.out.println("You cannot divide by 0");
                } else {
                output = number1/number2;
                System.out.println("Division of "+number1+" by "+number2+" is: " +output);
                }
           }
               else if(operation.equals('^'))
           {
                output = Math.pow((double)number1 , (double)number2);
                System.out.println("Value of "+num1+" raised to power of "+num2+" is: " +output);
           } else {
               System.out.println("Invalid input");
           }
    }
}

for pow. i tried casting wont work. and if i dont cast, it wont accept int. must be double.

Sam
  • 85
  • 8
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Makoto Oct 26 '13 at 18:53
  • @Sam Could you please change the question to include the old code? If you post a question then fix the code in it based on an answer, it invalidates all of the answers that were dealing with that error. (I'd rollback myself, but the revision is intermingled with some other stuff.) – Dennis Meng Oct 27 '13 at 04:34

1 Answers1

3

Use

s1.equals(s2)

to compare strings, instead of using:

s1 == s2

This happens because == is used to compare object references (if the are the same object), so it doesn't compare the 'contain' of that object, in this case a String.

Edit

To print each number in 'words', you could use an array:

String[] numbers = {"zero", "one", "two", ... };

and then use them as:

System.out.println(numbers[2] + " plus " + numbers[5] + ...);
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • Thank you! Such a stupid fault done by me ;/. another question how can i make it print the numbers in words. for example 5 * 3 = It should print "five multipled by 3 is 15." – Sam Oct 26 '13 at 18:53
  • cannot use arrays here. prof. asked us to do it in another way. – Sam Oct 26 '13 at 18:56
  • Not using arrays? Really? I don't see another way to do it, than hardcoding it, which will be awful. – Christian Tapia Oct 26 '13 at 19:02
  • christian, sorry but last question when i use math.pow for a power function. it asks for a double but im using int here. what is the solution to this? – Sam Oct 26 '13 at 19:05
  • I've tried it, and it auto-casts the `int`to `double`, but you could do it manually: Math.pow((double)number, 2); – Christian Tapia Oct 26 '13 at 19:07
  • i tried it, it wont accept int. and when i cast (double) doesnt work. – Sam Oct 26 '13 at 19:13
  • Can you edit your post, so you show your code where the problem is and the error? – Christian Tapia Oct 26 '13 at 19:14
  • I see, you must not cast the second parameter `number2`to double, it must be an integer. For your problem use: `output = (int) Math.pow(number1 , number2);` – Christian Tapia Oct 26 '13 at 19:19