o so now i think i have all the objects written. However, when i run it in NetBeans IDE it compiles and runs fine. But if i try to run it in the command line it gives a nosuchelement exception. Does anyone know why it runs ok in netbeans but not in the command line? Here are the classes
Number class
Code:
import java.io.*;
import java.util.*;
public class Number
{
Number()
{
}
//gets a single input from the user, and returns it as a String
String getInput()
{
java.io.InputStreamReader input = new java.io.InputStreamReader(System.in);
java.io.BufferedReader console = new java.io.BufferedReader(input);
try
{
return console.readLine();
}
catch(java.io.IOException e)
{
System.err.println("Bad NEWS!!!");
return "";
}
}
int getValue(String result)
{
Operator o = new Operator();
int num = 0;
String check = "";
StringTokenizer tok = new StringTokenizer(this.getInput());
do
{
check = tok.nextToken();
if(!check.equals("+") || !check.equals("-") ||!check.equals("*") || !check.equals("/") || !check.equals("%"))
num = Integer.parseInt(check);
else
o.getOp(result);
}while(tok.hasMoreTokens());
return num;
}
//Uses the StringTokenizer to break up the numeric experession and sends the tokens to the performOperation method
int doOperation(String Exper)
{
int result=0; int num =0; String operation ="";
StringTokenizer numEx = new StringTokenizer(Exper, "+-/%*", true);
result = Integer.parseInt(numEx.nextToken());
if(!numEx.hasMoreTokens())
{
return result;
}
do
{
operation = numEx.nextToken();
num = Integer.parseInt(numEx.nextToken());
result = Number.performOperation(result, num, operation);
}while(numEx.hasMoreTokens());
return result;
}
//performs the operation op on num1 and num2 returning the result as an int to result int the doOperation class
static int performOperation(int num1, int num2, String op)
{
if(op.equals("+"))
{
return num1 + num2;
}
else if(op.equals("-"))
{
return num1 - num2;
}
else if(op.equals("*"))
{
return num1 * num2;
}
else if(op.equals("/"))
{
return num1 / num2;
}
else if(op.equals("%"))
{
return num1 % num2;
}
else
{
System.err.println("Not an operation: " + op);
return -1;
}
}
}
Operator class
Code:
import java.io.*;
import java.util.*;
public class Operator
{
Number t = new Number();
String result = t.getInput();
Operator()
{
this.getOp(result);
}
char getOp(String result)
{
char op = '+';
String check = "";
StringTokenizer tok = new StringTokenizer(t.getInput());
do
{
check = tok.nextToken();
if(check.equals("+"))
op = '+';
if(check.equals("-"))
op = '-';
if(check.equals("*"))
op = '*';
if(check.equals("/"))
op = '/';
if(check.equals("%"))
op = '%';
}while(tok.hasMoreTokens());
return op;
}
}
CalculatorOO class with main method
Code:
import java.io.*;
import java.util.*;
public class CalculatorOO
{
public static void main(String args[])
{
String cont = "";
Number n = new Number();
StringTokenizer result = new StringTokenizer(n.getInput());
Operator op = new Operator();
do
{
String exp = n.getInput();
System.out.println(op.getOp(exp));
System.out.println(n.getValue(exp));
}while(result.hasMoreTokens());
do
{
System.out.println("Please enter a numeric expression: ");
System.out.print("Do you want to enter another expression (y/n): ");
cont = n.getInput();
}while(cont.equalsIgnoreCase("y"));
System.out.println("Goodbye");
}
}