Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
            }
        }
        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.

like image 983
Learath2 Avatar asked Jun 05 '26 03:06

Learath2


2 Answers

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.)

like image 51
aioobe Avatar answered Jun 07 '26 16:06

aioobe


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).

like image 29
ratchet freak Avatar answered Jun 07 '26 16:06

ratchet freak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!