我有一个文本文件和一个正则表达式,它提取整个文本中写入的所有数字。我能够匹配并提取数字,但我无法确定哪些提取的数字是素数。
有人可以帮帮我吗?谢谢!
这是我的代码:
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.
}
}
答案 0 :(得分:2)
您完全误解了与之关联的帖子。看起来你有一个表示数字的字符串,你认为你可以使用正则表达式来确定它是否是素数。这完全不可能。
该帖子展示了如何通过创建一个长度为n
的字符串来测试n
是否为素数,并将其与仅在字符串可以匹配的正则表达式匹配时进行匹配分解为2个或更多个长度和长度相等的组> = 2.如果可以,那么数字不能是素数,因为它必须是两个数字的乘积> = 2.这是一种非常差的方法来测试素性。它的唯一用途是让一些人炫耀自己的聪明才智。
即使您确实创建了该长度的字符串,您的代码也无法正常工作,因为您正在使用find()
,如果任何子字符串,这将成功你的源字符串匹配。这不会奏效。只有当匹配器被强制匹配整个源字符串时,该技巧才有效。
答案 1 :(得分:1)
如果您使用一元格式的数字,那就很好。
您可以参考此link
答案 2 :(得分:0)
所以是的,我所要做的就是添加另一个“isPrime”方法。虽然我将每个匹配添加到数组列表中,但我相信您也可以将组(0)解析为Integer并测试每个值。
这是我的代码:
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;
import java.util.*; // Import all java utilities
public class assignmentTwo {
public static ArrayList<Integer> nums;
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/assignment2.csv"));//Create a Scanner Class and File Class to read a file from your computer.
INPUT_TEXT.useDelimiter(" ");//Divide DemoData.txt into several pieces by a space
// test to see if file was received
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
// array variable
nums = new ArrayList<Integer>();
// find values that match
if (m1.find( )) {
// parse array values from string to integers
int parsedNum = Integer.parseInt(m1.group());
nums.add(parsedNum);
//If a number is matched, print the number.
System.out.print(m1.group() + " ");
// increment counter when number validation is reached
count++;
// if count reaches 5, reset count and print new line
if (count == 5) {
count = 0;
System.out.println();
}
// run isPrime method
isPrime();
}
}
INPUT_TEXT.close();//Close your Scanner.
}
// isPrime method to test which #s are prime
public static int isPrime(){
// finds size of array
for(int s = 0; s < nums.size(); s++){
// primeNum is true by default
boolean primeNum = true;
/* if a value from the array is divisible by any # <= array value
then that value is not prime */
for (int divisor = 2; divisor <= nums.get(s) / 2; divisor++) {
if (nums.get(s) % divisor == 0) {
primeNum = false;
}
}
// if value is prime, print out current value
if (primeNum) {
//System.out.println();
System.out.print(nums.get(s) + " ");
// return array list values
return nums.get(s);
}
}
// exit status
return 0;
}
}