This code is due in 4 hours it isnt working idk *** is wrong. it is supposed to be a fraction calculator that uses a string tokenizer and parses input.
import java.util.*;
public class Fractioncalculator {
public static void main(String[] args){
boolean quit = false; //boolean for recognition of quit command
boolean error = false;
String inputstr = null;
Scanner sc = new Scanner(System.in);
//Loop for input of problem and output of solution
while(quit==false){
int iterator = 31; //iterator for reduction of end result
int first_whole_int = 0; //first whole integer
int firstnum = 0; //first numerator
int firstdenom = 0; //first denominator
int second_whole_int = 0; //second whole integer
int secondnum = 0; //second numerator
int seconddenom = 0; //second denominator
char operator; //operator storage
int cmndenom = 0; //common denominator
int finalint = 0; //final integer
int finalnum = 0; //final numerator
int finaldenom = 0; //final denominator
//Request for input
System.out.println("Please enter your fractions: ");
inputstr = sc.nextLine();
if(inputstr.equals("quit")){ //check if quit received
quit = true;
}
StringTokenizer errorst = new StringTokenizer(inputstr, " ");
if(errorst.countTokens()!=3){
error = true;
}
else {
StringTokenizer st1 = new StringTokenizer(inputstr, " "); //Tokenize by spaces (First Section, Operator, Second Section)
//
//Section One
//
String first = st1.nextToken();
boolean indwhole = first.contains("/"); //boolean for checking if section one contains a fraction
if(indwhole==false){ //parses first whole number if above is false
first_whole_int = Integer.parseInt(first);
}
boolean indmixed = first.contains("_"); //boolean for checking is section one is a mixed number
if(indmixed==true){ //if above is true, execute parsing of first whole integer,
StringTokenizer st1_sub1 = new StringTokenizer(first, "_"); //first numerator and first denominator.
String first_whole = st1_sub1.nextToken();
first_whole_int = Integer.parseInt(first_whole);
}
//
//operator
//
String op = st1.nextToken();
operator = op.charAt(0);
//
//Second section
//
String second = st1.nextToken();
boolean indwhole2 = second.contains("/"); //boolean for checking if section two contains a fraction
if(indwhole2==false){ //parses first whole number if above is false
second_whole_int = Integer.parseInt(second);
}
boolean indmixed2 = second.contains("_"); //boolean for checking is section two is a mixed number
if(indmixed2==true){ //if above is true, execute parsing of second whole integer,
StringTokenizer st2_sub1 = new StringTokenizer(second, "_"); //second numerator and second denominator.
String second_whole = st2_sub1.nextToken();
second_whole_int = Integer.parseInt(second_whole);
String secondfractionstr = st2_sub1.nextToken();
StringTokenizer st2_sub2 = new StringTokenizer(secondfractionstr, "/");
secondnum = Integer.parseInt(st2_sub2.nextToken());
seconddenom = Integer.parseInt(st2_sub2.nextToken());
}
if(indwhole2==true&&indmixed2==false){ //if section one is just a fraction, parse numerator and denominator
Whenever I input fractions I just get back my own error message, (the error message I send as System.err.println("There was an error in your input, try again."); ) or nothing at all. I think it might be a problem with my output code but I'm not sure. I'm not having a program crash error message or system error message. The error message I'm talking about is the one I have set up in case someone does something stupid like 2/3 ++ 1/3.
Bookmarks