This is a problem for you to figure out a correct regular expression using any Java compiler you are familiar or any java online interpreter to solve. YOUR NEED TO TYPE OUT YOUR REGULAR EXPRESSION (NOT HAND WRITTEN IN WORKSHEET) SO I CAN CUT AND PASTE YOUR REGULAR EXPRESSION TO VALIDATE YOUR ANSWER IN THE RUNNING JAVA PROGRAM WHEN I GRADE. Your answer should be replacing the string "^(\\d*)" in line 21 and submit this string. The "^(\\d*)" regular expression now is way too simple so it only able to validate the 1st test case out of 9 test cases. The line you will be changing is Pattern patt = Pattern.compile("^(\\d*)");
This is a problem for you to figure out a correct regular expression using any Java compiler you are familiar or any java online interpreter to solve.
YOUR NEED TO TYPE OUT YOUR REGULAR EXPRESSION (NOT HAND WRITTEN IN WORKSHEET) SO I CAN CUT AND PASTE YOUR REGULAR EXPRESSION TO VALIDATE YOUR ANSWER IN THE RUNNING JAVA PROGRAM WHEN I GRADE.
Your answer should be replacing the string "^(\\d*)" in line 21 and submit this string. The "^(\\d*)" regular expression now is way too simple so it only able to validate the 1st test case out of 9 test cases.
The line you will be changing is
Pattern patt = Pattern.compile("^(\\d*)");
===============================================
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExp {
public static void main(String[] args) {
if (isANumber("12345")) System.out.println("true"); else System.out.println("false");
if (isANumber("-.23")) System.out.println("true"); else System.out.println("false");
if (isANumber("2.443e10")) System.out.println("true"); else System.out.println("false");
if (isANumber("+2e-1")) System.out.println("true"); else System.out.println("false");
if (isANumber("123.45e+4")) System.out.println("true"); else System.out.println("false");
if (isANumber("23e")) System.out.println("true"); else System.out.println("false");
if (isANumber("cs131")) System.out.println("true"); else System.out.println("false");
if (isANumber("-+5")) System.out.println("true"); else System.out.println("false");
if (isANumber("1a2e4")) System.out.println("true"); else System.out.println("false");
}
static boolean isANumber(String str) {
str = str.trim();
if(str.equals("")) return false;
Pattern patt = Pattern.compile("^(\\d*)");
Matcher mat = patt.matcher(str);
return mat.matches();
}
}
/*
A list of characters that can be in a valid decimal number:
Numbers : 0-9
Exponent : "e"
Plus and Minus : "+"/"-"
Decimal point : "."
^ means beginning of line
\\d* means 0 or multiple digits can occur
\\d+ means 1 or multiple digits can occur
\\. means a decimal point
[] means any character within the bracket can occurs
For more detail rules about regular expression, please reference to following document
To verify your regular expression is correct, check your answers to the following 9 cases.
"12345" ------ true
"-.23" ------ true
"2.443e10" ------ true
"+2e-1" ------- true
"123.45e+4" --- true
"23e" -------- false
"cs131" ------ false
"-+5" -------- false
"1a2e4" ------ false
Regular expression document at
https://jenkov.com/tutorials/java-regex/index.html#matching-digits
https://jenkov.com/tutorials/java-regex/pattern.html
https://jenkov.com/tutorials/java-regex/matcher.html
Step by step
Solved in 3 steps with 2 images