#Need help on how to solve an actual equation with Java.

34 messages · Page 1 of 1 (latest)

wise creekBOT
#

Hey, @pearl wadi!
Please remember to /close this post once your question has been answered!

tribal void
#

first u have to get the string then parse it

#

if there are brackets then u would have to solve the brackets one first

pearl wadi
tribal void
#

its ok nor do i xd

#

there are many terms hmm?

#

you can first try making just two terms basic one?

#

one sec i will try

tribal void
#

u can use str.split("*");

#

u can read an int from string using Integer.valueOf(Stirng)

pearl wadi
#

I can’t put an equation in an int because the equation could contain () * / + - and those cannot go inside an int

tribal void
#

yes u can first split then

#

i think so maybe first u check if it containds * then u split it then read ints from the array and then do with other symbols

#

this is simple and would work if only 2 terms like 2*3 and 7-9

tribal void
#

@pearl wadi i made a simple two terms one for u

    public static double equate(String line) throws wrongequationexception {//only for two terms like 8+90
        //System.out.println(line);
        try {
        if(line.contains("+")) {
            String[] array = line.split("\\+");
            return Integer.valueOf(array[0])+Integer.valueOf(array[1]);
        }else if(line.contains("-")) {
            String[] array = line.split("-");
            return Integer.valueOf(array[0])-Integer.valueOf(array[1]);
        }else if(line.contains("*")) {
            String[] array = line.split("\\*");// this"\\" is there because it will throw exeption its not a normal stirng
            return Integer.valueOf(array[0])*Integer.valueOf(array[1]);
        }else if(line.contains("/")) {
            String[] array = line.split("/");
            return Integer.valueOf(array[0])/Integer.valueOf(array[1]);
        }else {
            throw new wrongequationexception("no symbols used");
        }
        }catch(NumberFormatException e) {
            throw new wrongequationexception("more then 1 terms/ a-z used");
        }

    }
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(true) {
            String line = scan.nextLine();
            try {
                System.out.println("Answer: "+equate(line));
            } catch (wrongequationexception e) {
                e.printStackTrace();
            }
        }   
     }
tribal void
pearl wadi
#

And what do I have to do to use it?

tribal void
#

recursion calls the method again inside the method and doesnt calls infinitely so stops calling/ returns otherwise inifnsitely it will keep doing
a prime example is reading alllll files in a folder

#

you can get just a list of files in that folder not the subfolder using files.listfiles

tribal void
#

first addition then multiplicaltion like thhat

tribal void
pearl wadi
#

Not entirely. I’m new to Java, I’m confused on most of this.

tribal void
#
    public static double equate(String line) {
        if(line.contains("+")) {
            String[] array = line.split("\\+",2);
            return Double.valueOf(equate(array[0]))+Double.valueOf(equate(array[1]));
        }else if(line.contains("/")) {
            String[] array = line.split("/",2);
            return Double.valueOf(equate(array[0]))/Double.valueOf(equate(array[1]));
        }else if(line.contains("*")) {
            String[] array = line.split("\\*",2);// this"\\" is there because it will throw exeption its not a normal stirng
            return Double.valueOf(equate(array[0]))*Double.valueOf(equate(array[1]));
        }else if(line.contains("-")) {
            String[] array = line.split("-",2);
            return Double.valueOf(equate(array[0]))-Double.valueOf(equate(array[1]));
        }else {
            return Double.valueOf(line);
        }

    }
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(true) {
            String line = scan.nextLine();
            System.out.println("Answer: "+equate(line));
        }

this would work but not brackets()

#

.contains basically returns true if the string has that string

#

this just reads lines from console everytime

#

Scanner scan = new Scanner(System.in);
while(true) {
String line = scan.nextLine();
System.out.println("Answer: "+equate(line));
}

#

this

#

Double.valueOf converts an string to double which is an integer with decimal or without decimal it can be too

#

int cant have decimals