简体   繁体   中英

How to allow more entry and return factors for Prime Checker [Java]?

I set out to write a Prime Number Checker:

import java.util.Scanner;
public class PrimeChecker {

    int userEntry;
    int even_check;
    int odd_check;

    final void run(String[] args)
    {
        Scanner jaiho = new Scanner(System.in);
        System.out.printf("Please enter a prime number: %n");
        userEntry = jaiho.nextInt();
        even_check = userEntry % 2; // This is to get remainders
        odd_check = userEntry % 3; // If its not a multiple a remainder tells us that.
        System.out.println("Even Check is: " + even_check);
        System.out.println("Odd Check is: " + odd_check);

        if (even_check > 0 && odd_check > 0)
        {
            System.out.printf("This number is a prime number! Hoorah!");
        }   
        else if (userEntry == 2 || userEntry == 3)
        {
            System.out.printf("This number is a prime number! Hoorah!");
        }
        else
        {
            System.out.printf("This number is not a prime number.");
        }       
    }
}

Basic Flow: User Enters Number. System Checks and Prints Back "it is prime" or "No it isn't prime". So far it works, but I haven't tried to hard to break it.

1) How can I allow for more numbers to be entered and check at once? EG, User Enters "3,6,89,13" and system returns "prime, not prime, prime, prime"

2) Functionality to return factors. I guess to keep it simple return 2 or 3 as the factors. EG, User Enters "32" and system returns "This number is not prime. 2 is a factor."

I don't necessarily want the full answer but I do want some tips and some code.

--------------------------------After JP's Answer-------------------------------

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PrimeChecker {

    String primeOrNot = "(";

    final void run(String[] args)
    {
        Scanner jaiho = new Scanner(System.in);
        System.out.printf("Please enter a prime number: %n");
        String userEntry = jaiho.next();
        Pattern p=Pattern.compile("\\((\\d)+(?:,(\\d+))*\\)");
        Matcher m = p.matcher(userEntry);
        if (m.matches())
        {
            for (int a = 0; a < m.groupCount(); a++)
            {
                String d = m.group(a+1);
                int i = Integer.parseInt(d);
                int even_check = i % 2; // This is to get remainders
                int odd_check = i % 3; // If its not a multiple a remainder tells us that.
                System.out.println("Even Check is: " + even_check);
                System.out.println("Odd Check is: " + odd_check);
                if (even_check > 0 && odd_check > 0)
                {
                    primeOrNot += "prime, ";
                }   
                else if (i == 2 || i == 3)
                {
                    primeOrNot += "prime, ";
                }
                else if (even_check == 0 || odd_check ==0)
                {
                    primeOrNot += "not a prime, ";
                }
                else
                {
                    primeOrNot += "not a prime, ";
                }   

            }
            primeOrNot += ")";
            System.out.println(primeOrNot);
        }
    }
}

You first need to parse the string you want (3,6,86,13) for example. For this problem, you can use a regexp (now you have two problems (-:). See http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html . Your regexp needs to parse a parens, following by some digits, then maybe a comma and some more digits multiple times, then a closing comma. The expression for that would be:

Pattern p=Pattern.compile("\\((\\d)+(?:,(\\d+))*\\)");

You need to escape the parens, and you need to say you don't want the comma in the results you get from evaluating the pattern, hence the ?:. But that won't work, because it won't capture the repeating numbers after the first comma, only the last one (see Java regex: Repeating capturing groups ). So you must first parse the parens and first digit, then repeatedly parse the comma and following digits.

    // search for parens  + digits
    Pattern p=Pattern.compile("\\((\\d+)");
    Matcher m=p.matcher(userEntry);
    // where to search from in the string
    int st=0;
    while (m.find(st))  {

            String d = m.group(1);
            // get new start index
            st=m.end();
            int i = Integer.parseInt(d);
            int even_check = i % 2; // This is to get remainders
            // prime checks
            ...
            // now we need to search for comma + digits
            p=Pattern.compile(",(\\d+)");
            m=p.matcher(userEntry);
    }
    primeOrNot += ")";
    System.out.println(primeOrNot);

From then on, for each matching group you can keep the factors in a list, and print everything at the end...

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM