2

I am trying to get this code to run and basically solve an equation. So, I asked the user to write an equation. It looked like this:

System.out.println("Write an equation and I will solve for x.");
int answer = in.nextLine(); 

But I can't get the user to write a string and an int. Do I need to say String answer or int answer?

newuser
  • 8,338
  • 2
  • 25
  • 33
Nikki
  • 21
  • 1
  • `String answer` if you want them to input an equation. You will then need to parse it, determining its operators and operands, and solve for x. – J0e3gan Jan 09 '15 at 04:43
  • I am unfamiliar with parse, could you show me an example, please? – Nikki Jan 09 '15 at 04:47
  • here is an example http://stackoverflow.com/questions/787735/what-is-parse-parsing – ZEE Jan 09 '15 at 04:48
  • @Nikki can you please post example of equation. – mady Jan 09 '15 at 04:58
  • To test out the equation I am using 6x = 3. – Nikki Jan 09 '15 at 05:10
  • On another note, Nikki, there is no solution to `6x = 3` if you restrict `x` to being an `int`. You may want to use `float` or `double` for the solution to an equation like this one. – Dawood ibn Kareem Jan 09 '15 at 05:25
  • Are you writing logic for solving equations or are you asking how to solve the equation, because the user is going to enter a equation that you will read it and pass it to some algorithm which will calculate the result (in your case value of x) and return it to you. – Naman Gala Jan 09 '15 at 05:29

3 Answers3

0

An int is used when you want the user to enter a number, but here you're looking for a combination of numbers and other characters, so you will need to use a string. When you have the equation stored in a string, you can use other methods to split up the equation into something solvable, then set int answer to whatever the answer comes out to be.

Unsolved Cypher
  • 1,025
  • 10
  • 24
0

On a simpler side, String will be required input from the user, User will enter the equation.

Then comes the complex part of solving/computing the equation.

1.) create your own parser to pass operands/operator.

2.) Provide a equation with values to some API, you can make use of MVEL or ANTLR

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

Here's a little program that demonstrates one way to get the equation and divide into numeric / non-numeric values provided the equation input is space delimited. You can then determine what the non-numeric values are and proceed from there.

import java.util.Scanner;

public class SolveX{

    public static void main(String[] a){

        Scanner in = new Scanner(System.in);
        System.out.println("Write an equation and I will solve for x.");
        String input = "";

        while( in.hasNext() ){
            input = in.next();
            try{  
                double d = Double.parseDouble(input);
                System.out.println("Double found at: " + input);
                //  Do what you need to with the numeric value
            }  
            catch(NumberFormatException nfe){
                System.out.println("No double found at: " + input);
              //    Do what you need to with the non numeric value 
            }
        }
    }//end main
}//end SolveX class
Stack Underflow
  • 2,363
  • 2
  • 26
  • 51