1. Member
Join Date
Mar 2017
Posts
10
Rep Power
0

## Need help/advice with my program.

Hi all,

I have written a simple calculator for learning and practicing purposes.
I succeeded in making the program work, yet the code looks very messy, with the same lines written a few times (couldn't figure out how to fix it) and I am not sure how good it is.
I would highly appreciate anyone looking into my code giving me any advice or changes he can think of (below the code I wrote a few things that I couldn't make work).

(The code is below)
Just a bit of background into my code:
I wanted to create a simple calculator that will take 2 numbers from the user and an operand, the last number that we got will be saved, and then the user will have to only give 1 number to work with (as the answer from before becomes the first number), the user, at any time, will be able to write "exit" to exit the program or "clear" to go back to 0 as the answer, and giving 2 new numbers.

I wrote this using 2 classes (2 files):
First class (the calculator, this should have all the private variables and public methods)
Java Code:
```public class Calculator {
private int num_result;
public void result(String first_num, String second_num,String Operand){
try{
switch(Operand){
case "/":
if (second_num.equals("0")){
System.out.println("cant devide in 0");
break;
}
num_result = Integer.parseInt(first_num) / Integer.parseInt(second_num) ;
break;
case "*":
num_result = Integer.parseInt(first_num) * Integer.parseInt(second_num) ;
break;
case "-":
num_result = Integer.parseInt(first_num) - Integer.parseInt(second_num) ;
break;
case "+":
num_result = Integer.parseInt(first_num) + Integer.parseInt(second_num) ;
break;
default:
}}catch(Exception e){
System.out.println("You didnt put integers on both numbers");
}

}

public void clear(){
num_result = 0;
}
public int getResult(){
return num_result;
}
}```
Second class (this is my main code that will use the calculator methods):
Java Code:
```import java.util.Scanner;

public class Main {
public static void main(String[] args){
Calculator test = new Calculator();
Scanner input = new Scanner(System.in);
String first_num;
String second_num;
String operand;
int x = 0;

while (true){
if (x == 0){
System.out.print("Enter your first number - > ");
first_num = input.nextLine();
if (first_num.toLowerCase().equals("exit")){
break;

}else if(first_num.toLowerCase().equals("clear")){
test.clear();
System.out.println(test.getResult());
x = 0;
continue;
}
}else{
first_num = Integer.toString(test.getResult());
}
System.out.print("Enter your second number - > ");
second_num = input.nextLine();
if (second_num.toLowerCase().equals("exit")){
break;

}else if(second_num.toLowerCase().equals("clear")){
test.clear();
System.out.println(test.getResult());
x = 0;
continue;
}
System.out.print("Enter your operand - > ");
operand = input.nextLine();
if (operand.toLowerCase().equals("exit")){
break;

}else if(operand.toLowerCase().equals("clear")){
test.clear();
System.out.println(test.getResult());
x = 0 ;
continue;
}
test.result(first_num, second_num, operand);
System.out.println(test.getResult());
x = 1;

}
input.close();

}
}```
1)As you can see on my second code (the main function), I have the same function written 3 times (checking whether the answer is "exit" or "clear"), I tried writing it only once using a new method, yet couldn't figure out how (if I try to use a method to check it and someone writes clear on the first number then I cant use "continue" to "restart" my while loop). does any one have any idea on how to make it work without repeating all those lines?

2)Does writing the code like this, 2 classes, 1 class have private variables and public methods while the other one using it is the best way? or should I write my code another, more organized way (if so how)?

If any one could please give me any tips on how you think I can make my program better/look good it would be highly appreciated.
Thanks,
Nirel.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Need help/advice with my program.

Instead of prompting for input within the if/else blocks, simply prompt for input at the beginning of the loop. Then, make decisions on that
input (e.g. store a number, do an operation, or clear or exit, etc).

Another problem is that you test for 0 as a string for division by zero. What if someone enters 000 for their second argument?

Regards,
Jim

3. Member
Join Date
Mar 2017
Posts
10
Rep Power
0

## Re: Need help/advice with my program.

Originally Posted by jim829
Instead of prompting for input within the if/else blocks, simply prompt for input at the beginning of the loop. Then, make decisions on that
input (e.g. store a number, do an operation, or clear or exit, etc).

Another problem is that you test for 0 as a string for division by zero. What if someone enters 000 for their second argument?

Regards,
Jim
Oh wow, I didn't even think about the 000 thing... I fixed it right now thank you!

Regarding the prompting of the input at the begining of the loop, my problem with that is that if someone writes "clear" or "exit" on the first input (the first number) the program will still ask for the second number and the operand before actually doing it.
I am looking for a way not writing the if/else statement 3 times but also to check the input right after the user wrote them. Is thete any way to do so?

Nirel

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Need help/advice with my program.

Here is a hybrid example (java and pseudo code) to illustrate what I suggested.

Java Code:
```public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] operands = new String[2];
int i = 0;
while (true) {
String cmd = input.nextLine();
if (cmd.equals("clear") {
i = 0;
} else if(cmd.equals("exit")){
break;
} else if some condition3 {
perform operation if legal
} else if some condition4 {
perform operation if legal
}
// continue to handle conditions based on current user input.

// must be a good operand
operands[i++] = cmd;
}
}```
Regards,
Jim

5. Member
Join Date
Mar 2017
Posts
10
Rep Power
0

## Re: Need help/advice with my program.

Originally Posted by jim829
Here is a hybrid example (java and pseudo code) to illustrate what I suggested.

Java Code:
```public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] operands = new String[2];
int i = 0;
while (true) {
String cmd = input.nextLine();
if (cmd.equals("clear") {
i = 0;
} else if(cmd.equals("exit")){
break;
} else if some condition3 {
perform operation if legal
} else if some condition4 {
perform operation if legal
}
// continue to handle conditions based on current user input.

// must be a good operand
operands[i++] = cmd;
}
}```
Regards,
Jim
Just wanted to update, I fixed my code, I used things from what you suggested.
I am not sure if my code is better or even easier to understand, but at least I got rid of the 3 same if/else statements I didn't want.

Java Code:
```import java.util.Scanner;

public class Main {
public static void main(String[] args){
Calculator test = new Calculator();
Scanner input = new Scanner(System.in);
int i = 0;
String[] operands = new String[3];

while (true) {

switch (i){
case 0:
break;
case 1:
break;
case 2:
break;
}

String cmd = input.nextLine();
if (cmd.equals("clear")) {
i = 0;
test.clear();
continue;

}
else if(cmd.equals("exit")){
break;
}
operands[i++] = cmd;
if (i == 3){
test.result(operands[0], operands[1], operands[2].charAt(0));
operands[0] = Integer.toString(test.getResult());
System.out.println(operands[0]);
i = 1;
}
}
input.close();
}
}```
If you have any remarks I would love to hear them.
Any way, thank you very much! :)
Nirel.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•