I have a text file and a regex that extracts all of the numbers that are written throughout the text. I am able to match and extract the numbers, but I am having trouble trying to identify which extracted numbers are prime.
Could someone help me out? Thanks!
Here is my code:
import java.util.Scanner;//Import Scanner class to read a file in the computer
import java.io.File;//Import File class to read a file in the computer
import java.io.FileNotFoundException;//Import FileNotFoundException class if case file is not found in the computer
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex2 {
public static void main(String[] args) throws FileNotFoundException {
int count = 0;
//Display each word in INPUT_TEXT
Scanner INPUT_TEXT = new Scanner(new File("/Users/Matt/Desktop/regex2.csv"));//Create a Scanner Class and File Class to read a file from your computer.
INPUT_TEXT.useDelimiter(" ");//Divide file into several pieces by a space
if (INPUT_TEXT != null) {
System.out.println(INPUT_TEXT);
}
while(INPUT_TEXT.hasNext()) {
//Read each word
String TempString = INPUT_TEXT.next();
//Display all numbers.Eliminate comma, e.g., 1,200 -> 1200
String temp1 = TempString.replaceAll("[\\,]", "");//Create a String class temp1 that eliminates comma.
String pattern1 = "[0-9]+";//Create a String class pattern1 that stores the Regular Expression to match numbers.
Pattern r1 = Pattern.compile(pattern1);//Create Pattern class to compile pattern1.
Matcher m1 = r1.matcher(temp1);//Match each piece with the Regular Expression
if (m1.find()) {
System.out.print(m1.group() + " ");
//System.out.println(count);
count++;
// if count reaches 5, reset count and print new line
if (count == 5) {
count = 0;
System.out.println();
}
}
}
INPUT_TEXT.close();//Close your Scanner.
}
}