Below are the code. To fix the error, I simply rewrote the code in getFraction()
method to den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/") 1, fracValue.length()))
, by adding 1
. I have never seen or learn this during my courses and I just encounter this while doing project. I want to understand what the code did in num = Integer.parseInt(fracValue.substring(0, fracValue.indexOf("/")))
and den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/"), fracValue.length()))
, we are converting the numerator to int and the num is all the values before the /
and the den is all the values after the /
. Am I right ? My second question is that why do we need to add 1
after the indexOf("/")
? Is it so we are taking the values after the /
?
import java.util.Scanner;
public class FractionCalculator {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
intro();
while (true) {
String operation = getOperation();
Fraction frac1 = getFraction();
Fraction frac2 = getFraction();
Fraction result = new Fraction(1,1);
String result2 = "";
if (operation.equals("=")) {
System.out.println(frac1 " " operation " " frac2 " is " frac1.equals(frac2));
} else {
if (operation.equals(" ")) {
result=frac1.add(frac2);
} else if (operation.equals("-")) {
result=frac1.subtract(frac2);
} else if (operation.equals("/")) {
if(frac2.getNumerator()==0) {
result2="Undefined";
} else {
result=frac1.divide(frac2);
}
} else if (operation.equals("*")) {
if(frac2.getNumerator()==0) {
result2 = "Undefined";
} else {
result=frac1.multiply(frac2);
}
}
//print results
} if (result2!="") {// division and multiplication by zero is undefined
System.out.println(frac1 " " operation " " "0" " = " result2);
} else if (result.getNumerator()%result.getDenominator() == 0) {
System.out.println(frac1 " " operation " " frac2 " = " (result.getNumerator()/ result.getDenominator()));
} else {
System.out.println(frac1 " " operation " " frac2 " = " result.toString());
}
}
}
public static void intro() {
System.out.println("\nThis program is a fraction calculator");
System.out.println("It will add, subtract, multiply and divide fractions until you type Q to quit.");
System.out.println("Please enter your fraction in the form a/b, where a and b are integers.");
for (int i=0; i<80; i ) {
System.out.print("-");
}
}
public static String getOperation() {
System.out.println("\nPlease enter an operation ( , -, /, *, = or \"Q\" to quit): ");
Scanner input = new Scanner(System.in);
String operation = input.nextLine();
int x = 0;
while (x == 0) {
if (operation.equals(" ") || operation.equals("-") || operation.equals("/") || operation.equals("*") || operation.equals("=")) {
x ;
} else if (operation.equalsIgnoreCase("q")) {
System.exit(0);
} else {
System.out.println("Invalid input, enter valid operation ( , -, /, *, = or \"Q\" to quit)");
operation = input.nextLine();
}
}
return operation;
}
public static boolean validFraction(String input) {
boolean valid;
if (input.startsWith("-")) {
input = input.replaceFirst("-",""); // or use 'input.substring("1", input.length())';
}
if (input.contains("-") || input.charAt(input.indexOf("/") 1)==('0') || input.contains(" ")) {
valid = false;
} else if (input.contains("/")) {
input = input.replace("/", "");
}
if (input.matches("^[0-9] $") && input.length() > 0) {
valid = true;
} else {
valid = false;
}
return valid;
}
public static Fraction getFraction() {
System.out.println("Please enter a fraction (a/b) or integer (a): ");
String fracValue = input.nextLine();
//validate input
while (!validFraction(fracValue)) {
System.out.println("Please enter a fraction (a/b) or integer (a): ");
fracValue = input.nextLine();
}
//convert to numerator, denominator
int num = 0;
int den = 0;
if (fracValue.contains("/")) {
num = Integer.parseInt(fracValue.substring(0, fracValue.indexOf("/")));
den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/"), fracValue.length()));
} else {
num = Integer.parseInt(fracValue);
den = 1;
}
// return fraction
Fraction fracConv = new Fraction(num, den);
return fracConv;
}
}
CodePudding user response:
substring(int start, int end)
includes the character at start
and excludes the character at end
. Since the integer cannot be parsed with a /
in it, you need to do fracValue.indexOf("/") 1
to get just the numerical part of the denominator.
CodePudding user response:
Firstly you have to understand your input if your input is String a = "6 7", it means your string length is 3, and alloted indexes are 0,1 and 2 where 0 index is '6', 1 index is ' ' and 2 index is '7' So, when you use a.substring(0, a.indexOf(" ")) it means you are saying
a.indexOf(" ") = 1 index
you should get a string including '0' index but not 1 index, because substring works with inclusive first param and exclusive second param.
In case of this a.substring(a.indexOf(" ") 1, a.length())
you don't want to include ' ' in your denominator, So you should not include 1 index, that's why you are adding ( 1) with indexof. So, by adding 1 you are saying only pick value from a.indexOf(" ") 1, i.e. 2 index, and length of string is 3, which means you will get string inclusive of 2 index, i.e 7
if your input is "6 7" in that case you should use 'trim()' before using Integer.parseInt.