javaarraylistcalculator

making a simple calculator but the input is in one line (newbie)


package kotauto;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class calculator {

    static double addition(int a, int b)
    {
        return a + b;
    }
    
    static double subtraction(int a, int b)
    {
        return a - b;
    }
    
    static double multiplication(int a, int b)
    {
        return a * b;
    }
    
    static double division(int a, int b)
    {
        return a / b;
    }
    
    static double exponentiation(int a, int b)
    {
        double z=1;
        for(int i=1; i<=b; i++)
        {
            z= z*a;
        }
        return z;
    }
    
    static double root_extraction(double a)
    {
        return Math.sqrt(a);
    }

    
    public static void main(String[] args)
    {
        
        
        System.out.print("WELCOME TO THE CALCULATOR"+'\n');
        
        Scanner s = new Scanner(System.in); // scanner 
        ArrayList<Character> cal = new ArrayList<>();
        String z = s.next();
        
        for(int i=0; i<z.length(); i++)
        {
            cal.add(z.charAt(i));
        }
        
        
        int num1 = 0;
        int j =1;
        int x = 0;

for(int i=0; i<z.length(); i++)
{
     for(int k=0; k<z.length(); k++)
     {                                                                     if(!Character.isDigit(cal.get(k)))
{
  j =(int)Math.pow(10, k-1);                                                    break;
}
else
{
x++;
}
}
if(Character.isDigit(cal.get(i)))
{
int n = Character.getNumericValue(cal.get(i));
num1 += j*n;
j=j/10;             
}
else {
break;
}
                            
}
    int num2= 0;
    int l =1;
        for(int i=0; i<z.length(); i++)
        {
           for(int k=0; k<z.length(); k++)
             {                                                      if(!Character.isDigit(cal.get(k)))                                                      {                                                           l =(int)Math.pow(10, k-1);                                                              break;                                                      }
}
                                        if(Character.isDigit(cal.get(i)))
{
int n = Character.getNumericValue(cal.get(i));
num2 += l*n;
l=l/10;
}
                                
}   
            
                switch(cal.get(x-1))
                {
                case '+':
                {
                    System.out.println(addition(num1, num2));
                    break;
                
                }
                case'-':
                {
                    System.out.println(subtraction(num1, num2));
                    break;
                }
                case'/':
                {
                    System.out.println(division(num1, num2));
                    break;
                }
                case '*':
                {
                    System.out.println(multiplication(num1, num2));
                    break;
                }
                case '^':
                {
                    System.out.println(exponentiation(num1, num2));
                    break;
                }
                case's':
                {
                    System.out.println(root_extraction(num1));
                    break;
                }
                
            
                }
    
    
                
        s.close();
    }

}

soo the issues I'm facing are that if i'll input a number that has multiple digits i'll recieve a java.lang.IndexOutOfBoundsException at the switch(cal.get(x-1)) line furthermore when i'll input a number that only consists of one digit the console displays the result where (while adding) the first number gets added twice or (while multiplicating) the result comes with an added square of the frst number, it probably dosant work aswell in the other cases but im kind of too lazy to decode it. Also the root extraction is working very ineffectively as i need to input some random number after the s because otherwise it wouldnt compile without giving me an exception

btw im sorry for that awful display of code and lack of tabbing but its my first time using stack overflow and i simply dont know how to do it

i tried experimenting with the switch(cal.get(x-1)) line because i thought that thats the only reason my code is not working. I genuinely have no idea how to address these issues


Solution

  • Even though I don't understand why you written so complicated logic for simple 2 variable. But let me explain you found in your code

    Use below code

            Scanner s = new Scanner(System.in); // scanner
            ArrayList<Character> cal = new ArrayList<>();
            String z = s.next();
    
            for (int i = 0; i < z.length(); i++)
            {
                cal.add(z.charAt(i));
            }
    
            int num1 = 0;
            int j = 1;
            int x = 0;
            boolean isOperatorFound = false;
    
            for (int i = 0; i < z.length(); i++)
            {
                if(!isOperatorFound)
                {
                    for (int k = 0; k < z.length(); k++)
                    {
                        if (!Character.isDigit(cal.get(k)))
                        {
                            j = (int) Math.pow(10, k - 1);
                            isOperatorFound = true;
                            break;
                        }
                        else
                        {
                            x++;
                        }
                    }
                }
                if (Character.isDigit(cal.get(i)))
                {
                    int n = Character.getNumericValue(cal.get(i));
                    num1 += j * n;
                    j = j / 10;
                }
            }
            int num2 = 0;
            int l = (int) Math.pow(10, (cal.size() - (x+2)));
            for (int i = x+1; i < z.length(); i++)
            {
                if (Character.isDigit(cal.get(i)))
                {
                    int n = Character.getNumericValue(cal.get(i));
                    num2 += l * n;
                    l = l / 10;
                }
            }