# Fraction Calculator - Parsing Mixed Number Input?

Printable View

• 12-04-2013, 01:28 PM
Propinquity
Fraction Calculator - Parsing Mixed Number Input?
I'm having a hard time trying to parse mixed numbers to use as an input for my fraction calculator.
I am attempting to use an underscore "_" as the delimiter.
Whenever I try to run the program with mixed numbers, I get the following exception:
Exception in thread "main" java.lang.NumberFormatException: For input string: "5_1"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at FractionCalc.fractionCalc(FractionCalc.java:75)
at FractionCalc.setDataInput(FractionCalc.java:63)

Could someone please help me with this?
I managed to parse the fractions correctly, but I'm having trouble getting the mixed numbers to work.
What I am trying to do in my code to input mixed numbers is parse the whole numbers from the fraction, evaluate them separately, and add the result (wholenumberResult) to the whole number variable (wholeNumber)after the normal fraction is converted to mixed number form, near the end of the program, and evaluate and parse the remaining fraction like normal. However, my parsing isn't working and I have no real idea why.

Could someone please help and point out what I'm doing wrong, or explain how to do this correctly?

Code:

```import java.util.StringTokenizer; import java.text.DecimalFormat; public class FractionCalc {     private FractionReducer reducer = new FractionReducer(); //since i had multiple classes,  i used private variables     private StringTokenizer tokenizer1; //tokenizer for 1st fraction     private StringTokenizer tokenizer2; //tokenizer for 2nd fraction     private StringTokenizer tokenizer3;     private StringTokenizer tokenizer4;     private DecimalFormat formatter; //variable to reduce fraction to decimal form     private String resultsString; //total printed result     private String decimalResults; //printed decimal result     private String strFrac1; //input     private String strFrac2; //input     private String operator; //operator (*,-,/,+)     private String result; //unreduced fraction result     private String reducedFrac; //reduced fraction result     private double resultDec; //decimal variable     private int wholenumber1;     private int wholenumber2;     private int wholenumResult;     private int num1;     private int den1;     private int num2;     private int den2;     private int resultDen;     private int resultNum;     private char opp;     // Constructor //     public FractionCalc(){         String resultsString = "";         String decimalResults = "";         String strFrac1 = "";         String strFrac2 = "";         String operator = "";         String result = "";         String reducedFrac = "";         String resultFactor = "";         double resultDec = 0.0000;         int wholenumber1 = 0;         int wholenumber2 = 0;         int wholenumResult = 0;         int num1 = 0;         int den1 = 0;         int num2 = 0;         int den2 = 0;         int resultDen = 0;         int resultNum = 0;         char opp;     }     // Getters //     public String getResults(){ return resultsString; }     public String getDecimalResults(){ return decimalResults; }     // Setter //     public void setDataInput(String frac1, String frac2, String oper){         strFrac1 = frac1;         strFrac2 = frac2;         operator = oper;         fractionCalc();     }     // private //     private void fractionCalc(){         //this creates a tokenizer to split the user input and the delimiter is '/'         tokenizer1 = new StringTokenizer(strFrac1,"/");         tokenizer2 = new StringTokenizer(strFrac2,"/");         tokenizer3 = new StringTokenizer(strFrac1,"_");         tokenizer4 = new StringTokenizer(strFrac2,"_");                 // parse each input as integer then assign to variable         //first fraction         if ((strFrac1.contains("_"))) {                 wholenumber1 = Integer.parseInt(tokenizer3.nextToken());         } //parse mixed number input as integer         if ((strFrac2.contains("_"))) {                 wholenumber2 = Integer.parseInt(tokenizer4.nextToken());         }         num1 = Integer.parseInt(tokenizer1.nextToken());         if (!(strFrac1.contains("/")) ) { //if statement accounts for integer input                 den1 = 1;         }else{         den1 = Integer.parseInt(tokenizer1.nextToken());         }         //second fraction         num2 = Integer.parseInt(tokenizer2.nextToken());         if (!(strFrac2.contains("/")) ) { //if statement accounts for integer input                 den2 = 1;         }else{         den2 = Integer.parseInt(tokenizer2.nextToken());         }                 opp = operator.charAt(0); // string to char                 //divide by zero         if (den1 == 0 || den2 == 0) {             resultsString = "You cannot divide by zero.";             decimalResults = "";         }         //if not dividing by zero         else{             //depending on char choose how to handle calculation             switch (opp) { //switch statement                 case '+':                     resultNum = (num1*den2) + (num2*den1);                     resultDen = den1*den2;                     if ((strFrac1.contains("_") || (strFrac2.contains("_")))) {                             wholenumResult = wholenumber1+wholenumber2;                     }                     break; //statements after the matching "case" label are executed until "break" statement                 case '-':                     resultNum = (num1*den2) - (num2*den1);                     resultDen = den1*den2;                     if ((strFrac1.contains("_") || (strFrac2.contains("_")))){                             wholenumResult = wholenumber1-wholenumber2;                     }                     break;                 case '/':                     resultNum = num1*den2;                     resultDen = den1*num2;                     if ((strFrac2.contains("_") || (strFrac2.contains("_")))){                             wholenumResult = wholenumber1/wholenumber2;                     }                     break;                 case '*':                     resultNum = num1*num2;                     resultDen = den1*den2;                     if ((strFrac1.contains("_") || (strFrac2.contains("_")))){                             wholenumResult = wholenumber1*wholenumber2;                     }                     break;                 default:                     resultNum = 1;                     resultDen = 1;                     break;             }//end-switch             //build string with result             int wholeNumber = ( resultNum / resultDen );             if ((strFrac2.contains("_") || (strFrac2.contains("_")))){                     wholeNumber = ( resultNum/ resultDen ) + wholenumResult;             }             int partRemainder = ( resultNum % resultDen ); //converting result from improper to mixed fraction             String mixednumResults = ("" + wholeNumber + "_" + partRemainder + "/" + resultDen );             result = (resultNum + "/" + resultDen);             //divide each number for decimal result             resultDec = (double)resultNum/(double)resultDen;             // reduces the fraction to 4 significant figures, ex: 4.99998569849 = 4.9999             formatter = new DecimalFormat("0.0000");             //set decimalResults             decimalResults = formatter.format(resultDec);             //reduce fraction             reducer.setFraction(resultNum, resultDen);             //set reducedFrac             reducedFrac = reducer.getResultsFrac();             //build and set the result string             resultsString = num1 + "/" + den1 + " " + opp + " " +                             num2 + "/" + den2 + " = " +                             result + " " + reducedFrac + " (" + mixednumResults + ")";         }//end-else     }//end-calcFraction }```
• 12-04-2013, 01:55 PM
Norm
Re: Fraction Calculator - Parsing Mixed Number Input?
How do you execute the code for testing? What is the input to the program?