Menu

[Solved]Java Creating Program Supposed Able Solve Basic Addition Multiplication Subtraction Wonder Q37229054

JAVA ONLY

I am creating a program that is supposed to be able to solvebasic addition, multiplication, and subtraction. I am wondering whynothing shows up in my console and why I am getting errors. Can youhelp?

/**

* Rules: if it is a whole number, you must have a .0 afterit.

* for example, if I want to do 2-3=0, I must do 2.0-3.0=0.0.Another rule is that there must be spaces BEFORE and AFTER eachnumber

*/

public class Main {

public staticvoid main(String[] args) {

//variables I will need

String welcome=”Welcome to the Algebraic Calculator nWrite yourequation or problem here:”;

//variables for if it is just answers

String finalanswer = “”;

//run it, this is the main runner

System.out.println(welcome);

Scanner input = newScanner(System.in);

String UserInput= input.nextLine();

finalanswer=””+solve(UserInput);

System.out.println(finalanswer);

}

}

public staticdouble solve(String answer) {

double solve= firstnum(0,answer);

for (int i=1;i

if (answer.substring(i-1,i)==”+”) {

solve+=secondnum(i,answer);

}

if (answer.substring(i-1,i)==”*”) {

solve*=secondnum(i,answer);

}

if (answer.substring(i-1,i)==”/”) {

solve/=secondnum(i,answer);

}

if (answer.substring(i-1,i)==”-“) {

solve-=secondnum(i,answer);

}

}

return solve;

}

public staticdouble firstnum(int loc,StringInput) {

String bob=””;

//if((Input.substring(loc-2,loc-1)==” “)) {

// return Double.parseDouble(bob);

//}

while (Input.substring(loc+1,loc+2)!=” “) {

if((Input.substring(loc+1,loc+2)==”1″)||(Input.substring(loc+1,loc+2)==”2″)||(Input.substring(loc+1,loc+2)==”3″)

||(Input.substring(loc+1,loc+2)==”4″)||(Input.substring(loc+1,loc+2)==”5″)||(Input.substring(loc+1,loc+2)==”6″)

||(Input.substring(loc+1,loc+2)==”7″)||(Input.substring(loc+1,loc+2)==”8″)||(Input.substring(loc+1,loc+2)==”9″)

||(Input.substring(loc+1,loc+2)==”0″)||(Input.substring(loc+1,loc+2)==”.”)){

bob+=Input.substring(loc+1,loc+2);

loc++;

}

}

return Double.parseDouble(bob);

}

//this is a method that is meant to get the number that isbehind the +, *, -, or / symbol

//it takes in the location of the +, *, -, or / symbol and thestring and then returns the

//double that is behind the +, *, -, or / symbol as a double

public staticdouble secondnum(int loc,StringInput) {

String bob=””;

while (Input.substring(loc+1,loc+2)!=” “) {

if((Input.substring(loc+1,loc+2)==”1″)||(Input.substring(loc+1,loc+2)==”2″)||(Input.substring(loc+1,loc+2)==”3″)

||(Input.substring(loc+1,loc+2)==”4″)||(Input.substring(loc+1,loc+2)==”5″)||(Input.substring(loc+1,loc+2)==”6″)

||(Input.substring(loc+1,loc+2)==”7″)||(Input.substring(loc+1,loc+2)==”8″)||(Input.substring(loc+1,loc+2)==”9″)

||(Input.substring(loc+1,loc+2)==”0″)||(Input.substring(loc+1,loc+2)==”.”)){

bob+=Input.substring(loc+1,loc+2);

loc++;

}

}

return Double.parseDouble(bob);

}

}

Expert Answer


Answer to JAVA ONLY I am creating a program that is supposed to be able to solve basic addition, multiplication, and subtraction. … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *