Java Reference
In-Depth Information
public static double calculate(double op1, double op2, String operation) {
switch(operation) {
case "+":
return op1 + op2;
case "-":
return op1 - op2;
case "*":
return op1 * op2;
case "/":
return op1 / op2;
}
return Double.NaN;
}
}
You can evaluate an arithmetic expressing.
Expression must be in the form: a op b
a and b are two numbers and op is +, -, * or /.
Please enter an expression and press Enter: 10 + 19
10.0 + 19.0 = 29.00
StringTokenizer and StreamTokenizer
Java has some utility classes that let you break a string into parts called tokens. A token in this context is a part of the
string. You define the sequence of characters that are considered tokens by defining delimiter characters. Suppose
you have a string “This is a test, which is simple”. If you define a space as a delimiter, this string has the following
seven tokens:
1.
This
2.
is
3.
a
4.
test,
5.
which
6.
is
7.
simple
If you define a comma as a delimiter, the same string has the following two tokens:
1.
This is a test
2. which is simple
The StringTokenizer class is in the java.util package. The StreamTokenizer class is in the java.io package.
A StringTokenizer lets you break a string into tokens whereas a StreamTokenizer gives you access to the tokens in a
character-based stream.
 
Search WWH ::




Custom Search