I am trying to make a calculator. It requires I use a string Tokenizer. I have done so fine with the numbers but can't figure out how to put the & and / in there. The input is supposed to be as follows
These are improper fractions whole & numerator / denominator.
2&6/8 - 1&1/8 (no space in between.) How can I make it ignore the '&' and '/' and give me just the numbers.
Here is me code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Torbecire
*/
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Calculator {
public String nextToken(String separators) {
String ch = "&,/";
StringTokenizer strToken;
strToken = new StringTokenizer (",");
strToken.nextToken("&/ ");
return strToken.nextToken();
}
public static void main(String [] args)
{
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
String oneLine;
StringTokenizer str;
int x;
int y;
int z;
String q;
System.out.println("Enter a fractional problem:");
try
{
oneLine = in.readLine();
if( oneLine == null)
return;
str = new StringTokenizer( oneLine);
if( str.countTokens() != 3)
{
System.out.println(" NEED 3 INTS");
return;
}
x = Integer.parseInt( str.nextToken());
// String q = /*str.nextToken() + */ "&" + str.nextToken();
y = Integer.parseInt( str.nextToken());
// String w = /*str.nextToken() + */ "/" + str.nextToken();
z = Integer.parseInt( str.nextToken());
System.out.println("Max: " + Math.max(x, y));
}
catch(IOException e)
{ System.err.println("Unexpected IO error"); }
catch( NumberFormatException e)
{ System.err.println("Error: need two ints"); }
}
}