java - Getting error code, but don't know why -


i need write code fraction calculator can add, subtract, multiply, , divide 2 fractions. have code , getting error message

exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: -1     @ java.lang.string.substring(unknown source)     @ calculator.run(calculator.java:24)     @ calculator.main(calculator.java:13) 

i know error message gives me spot need fix cannot figure out have done wrong. still pretty new java easy fix.

thank in advance.

    import java.util.*;      public class calculator {     public static void main(string[] args) {         system.out.println("please enter 2 fractions add, subtract, multiply, or divide\nor\ntype 'quit' exit program.");         boolean on = true;         scanner console = new scanner(system.in);         while (on) {             string input = console.nextline();             if (input.equalsignorecase("quit")) {                 on = false;             } else                 system.out.println(run(input));         }     }      public static string run(string input) {         int indexofsecondspace = 0;         int indexofoperation = 0;         string firstnumber = "0";         string secondnumber = "0";         int beginning = input.indexof(" ") + 1;         int end = input.indexof(" ", input.indexof(" "));         string operator = input.substring(beginning, end);         if (input.contains("+") == true) {             indexofoperation = input.indexof("+");         } else if (operator.equals("-")) {             indexofoperation = input.indexof("-");         } else if (operator.equals("*")) {             indexofoperation = input.indexof("*");         } else if (operator.equals("/")) {             indexofoperation = input.indexof("/");         }         firstnumber = (input.substring(input.indexof(" ")));         secondnumber = (input.substring(input.indexof(" ") + 1));         int = 0;         int b = 0;         int c = 0;         int d = 0;           if (firstnumber.contains("/")) {             = integer.parseint(firstnumber.substring(0,firstnumber.indexof("/")));             b = integer.parseint(firstnumber.substring(0),firstnumber.indexof("/"));         } else if (!firstnumber.contains("/"))             = integer.parseint(input.substring(0, input.indexof(" ")));             b = integer.parseint("1");         {             if (secondnumber.contains("/")) {                 c = integer.parseint(secondnumber.substring(secondnumber.indexof("/")));                 d = integer.parseint(secondnumber.substring(secondnumber.indexof("/" + 1, secondnumber.length())));             } else if (!secondnumber.contains("/")) {                 c = integer.parseint(secondnumber.substring(secondnumber.length()));                 d = integer.parseint("1");             }         }         return input;      }     public static string calculate(string input, int a, int b, int c, int d){     if (input.contains ("+"))     {         system.out.println("your answer " + (a*d + b*c)+"/" +(b*d));     }     else if  (input.contains("-"))     {         system.out.println("your answer " + (a*d - b*c)+ "/" +(b*d));     }     else if (input.contains("/"))     {         system.out.println("your answer " + (a*d)/(b*c)+ "/" +(b*d));     }     else if (input.contains("*"))     {         system.out.println("your answer " + (a*c) +"/" +(b*d));     }     return input; } } 

the string.indexof method return -1 if string not found. @ snipplet have:

int beginning = input.indexof(" ") + 1; int end = input.indexof(" ", input.indexof(" ")); 

resulting in

int beginning = input.indexof(" ") + 1; // = -1 + 1 = 0 int end = input.indexof(" ", input.indexof(" ")); // input.indexof(" ", -1); error! 

and ment is:

int beginning = input.indexof(" ") + 1; // = -1 + 1 = 0 int end = input.indexof(" ", beginning ); // input.indexof(" ", 0); great job! 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -