I want to only accept fractions.
Here's what I've done so far:
if (nm == 2) {
System.out.print("First Fraction: ");
aa = sc.nextLine();
System.out.print("Second Fraction: ");
bb = sc.nextLine();
if (aa.startsWith("[0-9].*") && aa.contains("/") && aa.endsWith("[0-9].*") && bb.startsWith("[0-9].*") && bb.contains("/") && bb.endsWith("[0-9].*")) {
ar = new Arithmetic_Operations(aa, bb);
} else {
System.out.println("Please Input Fraction");
System.out.println();
break;
}
}
Arithmetic_Operations.Add();
How do I detect that the String starts and ends with a number? Also, I have no idea what I was doing with the .start and .end codes.
CodePudding user response:
The question can be expressed more generally as "how to write a parser". Generally speaking, regex is not the most straightforward way to tackle it. See here for a more thorough explanation, which can be summarized in your case as follows:
Write a method to check that the input has exactly:
- a numerator
- a denominator
- a division sign between them
This is a pretty simple case, so it'd be overkill to write something really complicated for it but one approach without using regex and with three separate simple "functions" (steps) would be the following:
boolean isFraction(String text) {
String[] numeratorAndDenominator = text.split("/");
// check for the division sign
if (numeratorAndDenominator.length != 2) {
return false;
}
// check that the numerator is an integer
try {
Integer.parseInt(numeratorAndDenominator[0].trim());
} catch (NumberFormatException e) {
return false;
}
// check that the denominator is also an integer
try {
Integer.parseInt(numeratorAndDenominator[1].trim());
} catch (NumberFormatException e) {
return false;
}
return true;
}
For more complicated stuff it would probably be more worth your time to look at existing parsing libraries for mathematical expressions. One possibility might be mathparser.
CodePudding user response:
If you want to detect simple fractions, you can use below method.
public static boolean isFraction(String text){
return (text.matches("[0-9] [/][0-9] "));
}
If you want a more complex solution, you need to also check,
- , - signs
- Parentheses
- More than one /
- Fractions
If you are implementing these via regex, below link will help you.
CodePudding user response:
I think this will work:
while (nm == 2) {
System.out.print("First Fraction: ");
aa = sc.nextLine();
System.out.print("Second Fraction: ");
bb = sc.nextLine();
if (aa.contains("/") && bb.contains("/")) {
try{
temp = Float.parseFloat(aa);
} except Exception {
System.out.println("Please Input Fraction\n");
}
ar = new Arithmetic_Operations(aa, bb);
break;
} else {
System.out.println("Please Input Fraction\n");
}
}
Arithmetic_Operations.Add();
First, as the fraction should have a division sign "/" it is checked in the if condition.
Also the result of a fraction could be an integer or a floating point number, so the result is parsed into a floating, if that code causes an exception which means that could not be a fraction.