![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
objects in java
I have all the objects written. However, when i run it it gives a NoSuchElement exception. I barely know what im doing can someone please help? Thanks
Here are the classes Number class 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 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 this is the class that runs the program with main 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");
}
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
What line is throwing the error? That is an enumeration error, and I am guessing that it is maybe this line:
check = tok.nextToken(); ummm..now I think I understand....look at the following line of code: StringTokenizer tok = new StringTokenizer(this.getInput()); What is that line of code supposed to be doing? How can you check to see if it is doing what you think it is doing? What I am guessing is happening is that you are returning a null value, and when you hit the check = tok.nextToken();
__________________
HijackThis Team-SFDC |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
StringTokenizer tok = new StringTokenizer(this.getInput()); that line is supposed to take whatever the user inputted and tokenize it. The overall program is supposed to be like a calculator. The user inputs an expression and the program solves it left to right ignoring any order of operations. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
What I meant was, if you do a System.out and print out the contents of tok, is there anything there? I don't know what you are doing for user input, but when I hard code some values in, I am not getting any errors.
__________________
HijackThis Team-SFDC Last edited by groovicus; Feb 27th, 2005 at 9:39 PM. |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
ok so what i had was totally unorganized and crazy. Now i have it a bit more under control i think. So what i want to do is make a program that takes a numeric expression entered by the user as a string and solve it from left to right ignoring any order of operations. I have to do this with different objects. There has to be 3 objects 1 is the CalculatorOO object that runs the program a Number object that takes an int and makes it a Number and an Operator object that takes a char and makes it an Operator. Now what i have to do is turn the user entered string into tokens. I have that in CalculatorOO and i have to say if the token is an operator (+,-,*,/,%) then make a new operator object. and if it is not an operator then it has to be a Number so make it a number object. Then in the Number object i need a method called perform operation that takes 2 Number objects and an Operator object as perameters and turns the Operator into a char and the Numbers into ints and solves the expression. The function that turns the Operator into a char and the Numbers into ints are supposed to be 2 different methods within the respected class. So i have a couple problems first i dont know how to transfer tokens from class to class. Second, how to i convert a user defined object into an int or a char? Please help im really confused at this point.
here is the new code i have so far. CalculatorOO runs the program import java.io.*;
import java.util.*;
public class CalculatorOO1
{
public static void main(String args[])
{
String cont = "";
do
{
System.out.print("Please enter a numeric expression: ");
CalculatorOO1.getInput();
System.out.println("Do you want to enter another expression? (y/n): ");
cont = getInput();
}while(cont.equals("y"));
}
static 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 "";
}
}
static void Tokenization()
{
StringTokenizer tok = new StringTokenizer(CalculatorOO1.getInput());
String check = "";
int p = 0;
do
{
check = tok.nextToken();
if(check.equals("+"))
Operator1 op = new Operator("+");
if(check.equals("-"))
Operator1 op = new Operator("-");
if(check.equals("*"))
Operator1 op = new Operator("*");
if(check.equals("/"))
Operator1 op = new Operator("/");
if(check.equals("%"))
Operator1 op = new Operator("%");
else
{
p = Integer.parseInt(check);
Number1 num = new Number(p);
}
}while(tok.hasMoreTokens());
}
}Number Object import java.io.*;
import java.util.*;
public class Number1
{
Number1(int a)
{
}
int getValue()
{
}
}Operator object import java.io.*;
import java.util.*;
public class Operator1
{
Operator1(String a)
{
getOp();
}
String getOp()
{
}
}Maybe i think what i really need to figure out now is this: i need to figure out how to take those tokens and use them in other classes but like just take the operator tokens into the Operator class and turn them into chars and take the number tokens into the Number class and turn them into ints. Thanks to anyone soooo much for any help. Last edited by cwl157; Feb 28th, 2005 at 9:36 PM. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
Are you trying to use postfix notation? I am still a little unclear on what yuu are trying to accomplish. It is trivial to take a string for example 1 + 2 or 1 2 + and parse it into seperate components. One way, right off the top of my head, would be:
-split my string into seperate tokens (into an array)
-check the first token and see if it matches [0-9]*
- if it matches my expression, parse it and put it into an int array (or double)
- else put token in operator array //if not a number, then an operatorSo basically you have three arrays. You can then create instances of your Operator class, and the Number class. Within the seperate classes you could have set methods. So, for instance, in your main class, you would do something like this: Operator o = new Operator; //don't pass an operator into the constructor. o.setOperator(operatorArray[0]); //gives setOperator method first operator in your array Inside your operator class, then you would have a method: public void setOperator(String s){
operator = s;
}Of course, you would need a field in that class like this: String operator = ""; So if we rewrite your Operator class, it would look like this: import java.io.*;
import java.util.*;
public class Operator1
{
private String operator = "";
Operator1()
{
}
String setOperator(String s)
{
operator = s;
}
}Now you have your operator inside your class, and it can be used. You already had a getOp, so then you would use that to return the string. String getOperator()
{
return operator;
}That's one way to go about it. I don't know what your specific requirements are as far as constructors for the seperate classes, or any required methods.
__________________
HijackThis Team-SFDC |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 327
Rep Power: 4
![]() |
yea for the constructors in the Number class it has to take an int and make it a number and in the operator class take a char and make it an operator.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|