Results 1 to 4 of 4
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Please help me with this fraction calculator? I don't understand what to fix.

    I keep getting this error:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsExceptio... String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at Calculator.main(Calculator.java:18)




    Java Code:
    import java.util.Scanner;
    
    public class fractioncalc {
    
        public static void main(String[] args) {
            // prompt user for fraction string
            // receive and store in variable
            // convert to improper fraction
            //print
    
            System.out.println("Enter a fraction:"); //prompt for string
            Scanner input = new Scanner(System.in); //Receive
    
            String fractionAsString = input.nextLine(); //store in variable
    
            int getSpace = fractionAsString.indexOf('_'); //should fine where java spaces for the initial entering of fractoin
    
            int getSlash = fractionAsString.indexOf('/'); //should find divisor (like in 1 1/2)
    
            String firstString= fractionAsString.substring(0, getSpace+1);//converting string to int
    
            String secondString=fractionAsString.substring(getSpace, getSlash);
    
            String thirdString=fractionAsString.substring(getSlash+1);
    
            int wholeNumber=Integer.parseInt(firstString);
    
            int numerator=Integer.parseInt(secondString);
    
            int denominator=Integer.parseInt(thirdString);
    
            int newResult=wholeNumber*denominator;
    
            int finalResult=numerator+newResult;
    
            System.out.println(finalResult+"/"+denominator);
    
            input.close();
    
        }
    
    }
    Last edited by Tolls; 11-13-2013 at 04:05 PM. Reason: add code tags

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Please help me with this fraction calculator? I don't understand what to fix.

    In the future, please place your code between [CODE][/CODE] tags for proper formatting. I have done it for you here.

    Java Code:
    public class fractioncalc {
    
    public static void main(String[] args) {
    // prompt user for fraction string
    // receive and store in variable
    // convert to improper fraction
    //print
    
    System.out.println("Enter a fraction:"); //prompt for string
    Scanner input = new Scanner(System.in); //Receive
    String fractionAsString = input.nextLine(); //store in variable
    int getSpace = fractionAsString.indexOf('_'); //should fine where java spaces for the initial entering of fractoin
    int getSlash = fractionAsString.indexOf('/'); //should find divisor (like in 1 1/2)
    String firstString= fractionAsString.substring(0, getSpace+1);//converting strng to int
    String secondString=fractionAsString.substring(getSpace, getSlash);
    String thirdString=fractionAsString.substring(getSlash+1) ;
    int wholeNumber=Integer.parseInt(firstString);
    int numerator=Integer.parseInt(secondString);
    int denominator=Integer.parseInt(thirdString);
    int newResult=wholeNumber*denominator;
    int finalResult=numerator+newResult;
    System.out.println(finalResult+"/"+denominator);
    input.close();
    }
    
    }
    Without knowing what your input looks like I can only guess that you are using the return values from one of your index calls without checking its value. This method returns -1 if the particular token is not found.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Please help me with this fraction calculator? I don't understand what to fix.

    This initial part is just supposed to take a mixed fraction like 1 1/2 and convert it to 3/2

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Please help me with this fraction calculator? I don't understand what to fix.

    Java Code:
    fractionAsString.indexOf('_');
    Unless you are entering '1_1/2', rather than '1 1/2', that will return -1.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Fraction Calculator need help
    By askinne2 in forum New To Java
    Replies: 15
    Last Post: 07-10-2012, 03:33 AM
  2. Mixed Fraction
    By x3iancute in forum NetBeans
    Replies: 2
    Last Post: 01-19-2010, 04:15 PM
  3. Fraction Problem
    By Jakora33 in forum New To Java
    Replies: 6
    Last Post: 09-16-2009, 04:51 PM
  4. Need help on Fraction
    By mitch25 in forum New To Java
    Replies: 2
    Last Post: 07-24-2009, 10:46 PM
  5. Big Fraction 1.00
    By JavaBean in forum Java Software
    Replies: 1
    Last Post: 03-26-2008, 05:24 AM

Posting Permissions

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