Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

however it is finding an error when calling computation. can someone tell me what it is please?

  Asked By: Jimmy    Date: Jun 15    Category: Java    Views: 664
  

i wrote the below code for a calculator. however it is finding an error
when calling computation. can someone tell me what it is please???




import java.io.*;
import java.util.*;
public class NewCalculator
{
private static String Equation ="";
//==============================================================================\
=======
// Read user input
//==============================================================================\
=======
public static String readText(String message)
{
BufferedReader input;
String text = "";
try
{
System.out.println(message);
input = new BufferedReader(new InputStreamReader(System.in));
text = input.readLine();
}
catch(IOException ioe)
{
}
return text;
}
//==============================================================================\
==
// Converting to Vector
//==============================================================================\
==
public static Vector parse(String equation)
{
Vector ParsedEquation = new Vector ();
String [] temp = equation.split ("\\s");
for (int i=0; i<temp.length; i++)
{
ParsedEquation.addElement (temp[i]);
}
return ParsedEquation;
}
//==============================================================================\
==
//==============================================================================\
==
// To convert to vector
//==============================================================================\
==
public static void calculation (Vector ParsedEquation)
{
Object [] operator = {"+","-","*","/"};
for (int i=0; i<operator.length; i++)
{
while (ParsedEquation.contains (String.valueOf (operator[i])) !=
false)
{
int a = ParsedEquation.indexOf("" +operator[i]);
if (a!=-1)
{
String answer = computation ("" +ParsedEquation.get(a-1),operator[i],
"" + ParsedEquation.get(a+1));
ParsedEquation.removeElementAt(a-1);
ParsedEquation.removeElementAt(a-1);
ParsedEquation.removeElementAt(a-1);
ParsedEquation.add(a-1, answer);
}
}
}
for (int i=0; i<ParsedEquation.size();i++)
System.out.println("\t"+ParsedEquation.elementAt(i));
}

//==============================================================================
//==============================================================================
// Computation
//==============================================================================
public static String computation (String a, char o, String b)
{
double firstOperator = Double.parseDouble(a);
double secondOperator = Double.parseDouble(b);
double result=0;
switch (o)
{
case '+':
result = firstOperator + secondOperator;
break;
case '-':
result = firstOperator - secondOperator;
break;
case '*':
result = firstOperator * secondOperator;
break;
case '/':
result = firstOperator / secondOperator;
break;
}
return""+ result;
}
//==============================================================================\
==
// Printing answer
//==============================================================================\
==
public static void printResult()
{
Vector ParsedEquation = parse (Equation);
for (int i=0; i<ParsedEquation.size(); i++)
System.out.println("\n" +ParsedEquation.elementAt(i));
calculation (ParsedEquation);
}
//==============================================================================\
=
//==============================================================================\
==
// Main Method
//==============================================================================\
==
public static void main (String args[])
{ Equation = readText("\n Please enter the mathematical calculating using a
SPACE: ");
printResult();
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Willie Gomez     Answered On: Jun 15

just change Object to char in Object [] operator  = {"+","-","*","/"};

 




Tagged: