HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Simple Console Calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
consolesimplecalculator

Problem

This is my Java 8 code for a console calculator. I just want to know if it can be improved in any way. Just so you know i am a real beginner just got into java about a couple of weeks ago.

import java.util.Scanner;

public class Algorithm1 {
public static int FirstNumber, SecondNumber;
public static float TheAnswer;
public static String Action;
public static Scanner ScanInt1, ScanString;
public static boolean hasString;
public static boolean hasString2, hasInt;
public static void main(String args[]){
hasString = false;//sets up the booleans
hasString = false;
hasInt = false;
ScanInt1 = new Scanner(System.in);
ScanString = new Scanner(System.in);
System.out.println("Give your First input please.");
while  (FirstNumber < 1){
FirstNumber = ScanInt1.nextInt();   
}
System.out.println("Give your Second input please.");
hasString = true;
if (SecondNumber < 1){
SecondNumber = ScanInt1.nextInt();      
}
else {
System.out.println("Now * or / or + or -");
hasString2 = true;
}
Action = ScanString.nextLine();
System.out.println(Answer());
}
public static float Answer(){
//This method is for the math
    if (Action.equals("*")){
        TheAnswer = FirstNumber * SecondNumber;

    } 
if (Action.equals("/")){
    TheAnswer = FirstNumber / SecondNumber;

}
if (Action.equals("+")){
    TheAnswer = FirstNumber + SecondNumber;

}
if (Action.equals("-")){
    TheAnswer = FirstNumber - SecondNumber;

}
return TheAnswer;
}

}

Solution

A CLI-based calculator is often a good way to prepare one for bigger programming tasks, as there are just so many ways of doing it, catering for beginner to advanced implementations. I happen to have one myself about two months ago, for reference. :)

Indentation

There's no other way of putting this, but your indentation is extremely poor. Nested code blocks inside { } braces should be indented accordingly, instead of left-aligning everything.

Naming style

Please use camelCase instead of PascalCase for your variable and method names. As you can tell from the syntax highlighting on your present code, PascalCase is the naming convention for type/class names. This makes reading your code slightly harder as other developers have to 're-parse' your variable/method names from what looks like class names.

try-with-resources

Since Java 7, try-with-resources is the recommended approach to read from an I/O source, such as wrapping a Scanner over System.in:

try (Scanner scanner = new Scanner(System.in)) {
    // ...
    String line = scanner.nextLine();
    // ...
}


This lets the JVM close I/O resources automatically for you.

Less reliance on 'global' static variables

You should slowly adopt better object-oriented (OO) programming practices, such as relying less on static variables. It's better to make more effective use of method arguments so that other developers can understand what the method requires. More on this will be illustrated below.

return early

Inside your Answer() method, you should return early so that you can do away with temporary variables. In my opinion, temporary variables are often a crutch to writing longer methods, which makes code harder to read and understand in the long run. A possible improvement can be:

private static float getAnswer(String action, int first, int second) {
    switch (action) {
    case "+":
        return (float) first + second;
    case "-":
        return (float) first - second;
    // ...
    }
}

Code Snippets

try (Scanner scanner = new Scanner(System.in)) {
    // ...
    String line = scanner.nextLine();
    // ...
}
private static float getAnswer(String action, int first, int second) {
    switch (action) {
    case "+":
        return (float) first + second;
    case "-":
        return (float) first - second;
    // ...
    }
}

Context

StackExchange Code Review Q#96338, answer score: 12

Revisions (0)

No revisions yet.