+ 4
Java fractions
So I am making a method for adding fractions, but the user input begins as such: String frac1 = â5/6â; String frac2 = â1/7â; String frac3 = â-2/9â; How can I get java to read the String and interpret it as 2 seperate int variables (numerator, denominator)? Iâm assuming parsing but Iâm totally boggled how to break up the string and especially how to interpret negative values for calculating 2 fractionsâ sum
3 Answers
+ 4
You can use String.split for get numerator and denominator of fraction then use Integer.valueOf (or Integer.parseInt) for detect if either are valid numbers and get their values like integer
+ 3
Ues regex to split the input to nom/denom
+ 2
Thanks guys, so very helpful!