CommandLine Java Calculator miscalculates
Ive just learned java and asked a question about my one line calculator it doenst give error anymore but it miscalculates. Here is the code
import java.util.Scanner;
public class omg {
public static void main(String args[]) {
int fnum,snum,anum = 0;
String strtype;
char[] testchar;
char currentchar;
int machinecode = 0;
String tempnumstr;
int operatorloc = 0;
char[] tempnum = new char[256];
Scanner scan = new Scanner(System.in);
System.out.println("Enter The Calculation: ");
strtype = scan.nextLine();
testchar = strtype.toCharArray();
for(int b = 0; b < testchar.length; b++)
{
currentchar = testchar[b];
if(currentchar == '+') {
machinecode = 1;
operatorloc = b;
}
else if(currentchar == '-') {
machinecode = 2;
operatorloc = b;
}
else if(currentchar == '*') {
machinecode = 3;
operatorloc = b;
}
else if(currentchar == '/') {
machinecode = 4;
operatorloc = b;
开发者_如何学Go}
}
for(int t = 0;t < operatorloc;t++) {
tempnum[t] = testchar[t];
}
tempnumstr = new String(tempnum).trim();
fnum = Integer.parseInt(tempnumstr);
for(int temp = operatorloc;temp < testchar.length;temp++) {
for(int t = 0;t<(testchar.length-operatorloc);t++) {
tempnum[t] = testchar[temp];
}
}
tempnumstr = new String(tempnum).trim();
snum = Integer.parseInt(tempnumstr);
switch(machinecode) {
case 1:
anum = fnum + snum;
break;
case 2:
anum = fnum - snum;
break;
case 3:
anum = fnum * snum;
break;
case 4:
anum = fnum / snum;
}
System.out.println(anum);
}
}
This code would give me 8+8 = 96, And this is obviously not correct.
Which IDE are you using? Eclipse? In that case you should really try out the debugging perspective.
I simple stepped through your program and found the following:
At this line:
tempnumstr = new String(tempnum).trim();
tempnum
equals { 8, 8 }
and temunumstr
becomes 88
(thats why you get 88+8 = 96)
I must say that the program is sort of a mess, and not really the way to create a calculator. You've really taken on a complicated task as a first exercise ;-)
That said, I think you should change
for (int temp = operatorloc; temp < testchar.length; temp++) {
for (int t = 0; t < (testchar.length - operatorloc); t++) {
tempnum[t] = testchar[temp];
}
}
tempnumstr = new String(tempnum).trim();
with just the line
tempnumstr = strtype.substring(operatorloc + 1);
(At least then it gives 16
for the input 8+8
.)
The loop for the second term is faulty, you are nesting the loop, change it to
for (int temp = operatorloc+1, t=0 ;temp < testchar.length;temp++, t++ ) {
tempnum[t] = testchar[temp];
}
And it will work
The original loop
for (int temp = operatorloc;temp < testchar.length;temp++) {
for (int t = 0;t<(testchar.length-operatorloc);t++) {
tempnum[t] = testchar[temp];
}
}
Is essentially equivalent to
int temp = testchar.length-1;
for (int t = 0;t<(testchar.length-operatorloc);t++) {
tempnum[t] = testchar[temp];//temp doesn't change
}
And there was also an off by one on the start index (that's where the +1
came from in int temp = operatorloc+1
).
Your logic is somewhat borked here:
for (int temp = operatorloc; temp < testchar.length; temp++) {
for (int t = 0; t < (testchar.length - operatorloc); t++) {
tempnum[t] = testchar[temp];
}
}
To see what it's doing, add a println for debugging puroses:
for (int temp = operatorloc; temp < testchar.length; temp++) {
for (int t = 0; t < (testchar.length - operatorloc); t++) {
tempnum[t] = testchar[temp];
System.out.println("test: " + new String(tempnum).trim());
}
}
Try to print fnum
and snum
before calculation and you will see that snum
has value 88. Your code not obvious for me, so you should debug it and fix manually :)
Your snum assigning is wrong. Now since it looks like homework :) I'm not going to spell it out.
You also need to take a look at some Java naming conventions as it will make the code more readable. One last suggestion is to use constants for the machine codes.
You should download an IDE (eclipse Netbeans and IntelliJ are all free or have a free version), and execute your program inside their debugger. You would quickly find your errors.
That said, stop treating Strings like char arrays, and stop using fixed length arrays hoping their size will be enough. String is a full-fledged object, with a lot of useful methods. substring is one of them, and could be used to extract the left and right operand. It would be much easier and less buggy than to copy from one char array to another, reused, one (you use tempnum
to store the chars of both operands, without resetting the cahrs to default values).
Here I have a plain and simple but always correctly calculating Java calculator:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
public static void main(String[] args) {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("javascript");
try {
Object o = engine.eval(allArgs(args));
System.out.println("The result is = " + o.toString());
} catch (ScriptException e) {
System.out.println("Please type in a syntactically correct expression.");
}
}
private static String allArgs(String[] args) {
StringBuilder sb = new StringBuilder();
for (String s : args) {
sb.append(s);
}
return sb.toString();
}
}
invoke program and behold the result
精彩评论