2
\$\begingroup\$

I'm new to Java and coding in general. I'm about a week or two into learning Java, and I decided to make this simple four function calculator. Any feedback/tips?

import java.util.Scanner;

class Main {

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


    System.out.println("What is your first number?");
    float num1 = input.nextFloat();
    System.out.println("What is your second number?");
    float num2= input.nextFloat();
    System.out.println("What operation would you like to perform?");
    String Operation = input.next();
    switch (Operation) {
        case "addition":
            System.out.println(num1 + num2);
            break;
        case "subtraction":
            System.out.println(num1 - num2);
            break;
        case "multiplication":
            System.out.println(num1 * num2);
            break;
        case "division":
            if (num1 == 0 || num2 == 0) ;
        {
            System.out.println("You can not divide by or with a zero!");
        }

        System.out.println(num1 / num2);
        break;
        default:
            System.out.println("Invalid Operation!");
    }



   }
}
\$\endgroup\$
1
  • \$\begingroup\$ That is a lot to type in \$\endgroup\$
    – paparazzo
    Commented Jul 27, 2017 at 18:50

2 Answers 2

4
\$\begingroup\$

Style

Your structure for this program is fine, except one bug highlighted below. However, from first glance, you can make some style adjustments to increase the readability of your program. Coding conventions are extremely important - and although things like "do I put brackets on the same line or a new line?" are controversial, it is essential that you write clean and readable code. A little spacing never hurt anybody, but it should be consistent and clean.

float num2= input.nextFloat();

Put a space before the equal sign.

String Operation = input.next();

Variable names should typically be lower camel case. Operation is actually a class from the java.rmi.server library, so you should rename this to simply operation.

case "division":
    if (num1 == 0 || num2 == 0) ;
{
    System.out.println("You can not divide by or with a zero!");
}

There should not be a semi-colon after the if-statement. This is causing wrong output! Line up your brackets. Also, why can't you divide 0? 0 / 5 is a perfectly valid operation.

You can clean this up simply by using the following

case "division":
    if (num2 == 0)
        System.out.println("You cannot divide by zero!");
    else
        System.out.println(num1 / num2);
    break;

You could also keep the brackets if you wish. If the statement contains only one inner statement, you don't need brackets, but some people prefer it for the readability. This is a personal style.

    case "division":
        if (num2 == 0) {
            System.out.println("You cannot divide by zero!");
        } else {
            System.out.println(num1 / num2);
        }
        break;

If you really want to trim it down, you can use the ternary operator ?.

    case "division":
        System.out.println(num2 == 0 ? "You cannot divide by zero!" : (num1 / num2));
        break;

Once you've done that, challenge yourself by getting yourself familiar with reading input files, so you don't have to type your input every time! You could try experimenting with a BufferedReader for example.

\$\endgroup\$
2
\$\begingroup\$

This is wrong in several ways:

$ java Main
What is your first number?
3
What is your second number?
8
What operation would you like to perform?
division
You can not divide by or with a zero!
0.375

First of all, neither of the operands was zero, but you printed out the error message anyway. (How did that happen? Check the placement of your semicolons! Also, while you are at it, fix the indentation of your code.)

Second, even if the stray semicolon were removed, I see no reason at all to object to 0 as the dividend (i.e. num1). Zero divided by any non-zero number should produce 0 as the result.

Third, you should consider allowing 0 as divisor (i.e. num2). Dividing by 0.0f, in floating-point arithmetic, actually produces a rather sensible result: Float.POSITIVE_INFINITY (which is rendered as Infinity when printed as a string). Why not just let it happen?

$ java Main
What is your first number?
5
What is your second number?
0
What operation would you like to perform?
division
You can not divide by or with a zero!
Infinity

Avoid using variable names that start with an upper-case letter, as you did with Operation. That looks like the name of a class, by convention.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.