I am trying to do a program which does simple maths by scanning
using provided three items (two number and an operator) then let the program
do the calculatin and printout the result.

The way i have attempted it so far (below is the code) the value of the operator is also being included in the result.

Anyhelp to help me look at what i should do will be appreciated.

package jaavv;
import java.util.*;


public class Linie {

public static void main(String[] args){

String ti,ch,gi;

System.out.println("Please type your question here!");
Scanner adder = new Scanner(System.in);

ti = adder.next();
ch = adder.next();
gi = adder.next();

double newTi = Double.parseDouble(ti);
char newCh = ch.charAt(0);
double newGi = Double.parseDouble(gi);


System.out.println (newTi + newCh + newGi);

}
}

Recommended Answers

All 2 Replies

System.out.println (newTi + newCh + newGi);

Will not give you the desired result. Java do not have any inline function that will evaluate a expression.
You will have to write the method to evaluate.
You can do like check the value of 'newCh' and perform the operation on the two operators accordingly.
e.g.

if(newCh == '+'){
  result = newTi + newGi;
}
else if(newCh == '-'){
  result = newTi - newGi;
}
//..and so on and at the end
System.out.println(result);

Or you can also use some expression parsers available at
http://sourceforge.net/projects/jeplite/
http://www.japisoft.com/formula/

Hope this was helpful

parry_kulk

i solved the problem using your advice. thank you
very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.