#Need help on how to solve an actual equation with Java.
34 messages · Page 1 of 1 (latest)
Hey, @pearl wadi!
Please remember to /close this post once your question has been answered!
first u have to get the string then parse it
if there are brackets then u would have to solve the brackets one first
I don’t know what that means. I’m relatively new to Java, sorry.
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
there is str.split
u can use str.split("*");
u can read an int from string using Integer.valueOf(Stirng)
I can’t put an equation in an int because the equation could contain () * / + - and those cannot go inside an int
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
u need more terms?
@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();
}
}
}
you can use recursion for more terms
Wait so what is this going to do?
And what do I have to do to use it?
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
first of all u have to use boadmass way right??
first addition then multiplicaltion like thhat
you understood the above one yes?
Not entirely. I’m new to Java, I’m confused on most of this.
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