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*)");

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Top down approach design
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education