Need help with some code DUE IN 4 HOURS
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Need help with some code DUE IN 4 HOURS

  1. #1
    Join Date
    Nov 2011
    Posts
    2

    Need help with some code DUE IN 4 HOURS

    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);

    String firstfractionstr = st1_sub1.nextToken();
    StringTokenizer st1_sub2 = new StringTokenizer(firstfractionstr, "/");
    firstnum = Integer.parseInt(st1_sub2.nextToken());
    firstdenom = Integer.parseInt(st1_sub2.nextToken());
    }

    if(indwhole==true&&indmixed==false){ //if section one is just a fraction, parse numerator and denominator

    StringTokenizer st1_sub2 = new StringTokenizer(first, "/ ");
    firstnum = Integer.parseInt(st1_sub2.nextToken());
    firstdenom = Integer.parseInt(st1_sub2.nextToken());


    }
    //
    //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

    StringTokenizer st2_sub2 = new StringTokenizer(second, "/ ");
    secondnum = Integer.parseInt(st2_sub2.nextToken());
    seconddenom = Integer.parseInt(st2_sub2.nextToken());


    }


    if(operator == '+'){ //function execution for if operator is addition

    if(firstdenom!=0 && seconddenom!=0){
    cmndenom = firstdenom * seconddenom;
    }
    if(firstdenom!=0 && seconddenom==0){
    cmndenom = firstdenom;
    }
    if(firstdenom==0 && seconddenom!=0){
    cmndenom=seconddenom;
    }

    finalint = first_whole_int + second_whole_int;

    if(firstnum!=0 && secondnum!=0){
    finalnum = firstnum*seconddenom + secondnum*firstdenom;
    }
    if(firstnum==0 && secondnum!=0){
    finalnum=secondnum;
    }
    if(firstnum!=0 && secondnum==0){
    finalnum=firstnum;
    }
    }
    if(operator == '-'){
    finalint = first_whole_int - second_whole_int;
    if(firstdenom!=0 && seconddenom!=0){
    cmndenom = firstdenom * seconddenom;
    }
    if(firstdenom!=0 && seconddenom==0){
    cmndenom = firstdenom;
    }
    if(firstdenom==0 && seconddenom!=0){
    cmndenom=seconddenom;
    }
    if(firstnum!=0 && secondnum!=0){
    finalnum = firstnum*seconddenom - secondnum*firstdenom;
    }
    if(firstnum==0 && secondnum!=0){
    finalnum=secondnum;
    }
    if(firstnum!=0 && secondnum==0){
    finalnum=firstnum;
    }
    }
    if(operator == '/'){
    if(first_whole_int!=0 && firstdenom!=0){
    firstnum=first_whole_int*firstdenom + firstnum;
    }
    if(first_whole_int!=0 && firstdenom==0){
    firstnum=first_whole_int;
    firstdenom=1;
    }
    if(second_whole_int!=0 && seconddenom!=0){
    secondnum=second_whole_int*seconddenom + secondnum;
    }
    if(second_whole_int!=0 && seconddenom==0){
    secondnum=second_whole_int;
    seconddenom=1;
    }
    finalnum=firstnum*seconddenom;
    cmndenom=firstdenom*secondnum;

    }
    if(operator=='*'){ //function execution for if operator is multiplication
    if(firstdenom==0){
    firstnum=first_whole_int;
    firstdenom=1;
    }
    if(firstdenom!=0 && first_whole_int!=0){
    firstnum=first_whole_int*firstdenom + firstnum;
    }
    if(seconddenom==0){
    secondnum=second_whole_int;
    seconddenom=1;
    }
    if(seconddenom!=0 && second_whole_int!=0){
    secondnum=second_whole_int*seconddenom + secondnum;
    }
    finalnum=firstnum*secondnum;
    cmndenom=firstdenom*seconddenom;
    }
    finaldenom = cmndenom; //nuance of code, ignore
    while(iterator>1){ //reduction of fraction through common dividend
    if(finalnum%iterator==0 && finaldenom%iterator==0){
    finalnum = finalnum / iterator;
    finaldenom = finaldenom / iterator;
    iterator--;
    }
    else{
    iterator--;
    }
    }
    while(finalnum>finaldenom){ //reduction of fraction to mixed number
    finalnum = finalnum - finaldenom;
    finalint++;
    }


    //Output
    //Prints are self-explanatory through blue text, and are only executed if they are possible
    //Outputs do not print if an error has occurred
    if(error=false){
    if(finalint!=0 && finaldenom!=1 && finaldenom!=0 && finalint>0){
    System.out.println("Mixed number form: " + finalint + "_" + finalnum + "/" + finaldenom);
    }
    if(finaldenom==0 && finalnum==0 && finalint!=0){
    System.out.println("Final Number: " + finalint);
    }
    finalnum = finalnum + (finalint*finaldenom);
    if(finaldenom!=1 && finaldenom!=0){
    System.out.println("Fraction form: " + finalnum + "/" + finaldenom);
    }
    if(finaldenom==1){
    System.out.println("Final Number: " + finalnum);
    }
    }
    else {
    System.out.println("There was an error in your input, try again.");
    }
    System.out.println();
    }
    if(quit==true){
    System.out.println("Program has exited.");
    }
    }
    }
    }

  2. #2
    Join Date
    Nov 2011
    Posts
    2

    Re: Need help with some code DUE IN 4 HOURS

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center