Skip to main content
deleted 2 characters in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

How can I make my code more efficient on reading Listing prime numbers up to the given number, and continue on looping when 0 or 1 is input

import java.util.*;


public class PrimeNumber {
    private static ArrayList<Integer> listOfPrime = new ArrayList<Integer>();

    public static void main(String args[]) {
        Scanner userInput = new Scanner(System.in);
        System.out.print("Please enter a number to checked the list of prime number >  ");
    //Request user to input  number.
        String input = userInput.nextLine();
        int number = 0;
        boolean check = false;
    //Check if integer entered is it integer
        while(check != true) {
            try {
    // if input can be parse into integer, shows that its number so can change check into true to exit the while loop


                number = Integer.parseInt(input);
                check = true;
            }
            catch(NumberFormatException e) {
                System.out.println("Please enter numbers only  >  ");
                input = userInput.nextLine();
            }
        }

    //Check if input enter is 0 or 1, as prime number cannot be those.
        if(number < 2) {
            System.out.println("Prime number cannot be 0 or 1");
        }else {
            checkPrime(number);
        }


    }


    //Check for all prime numbers within the number user input.
    private static void checkPrime(int input) {
        boolean primeValidation = true;
    //first for loop to check from small to biggest integer in the input
        for(int i = 1; i <= input;i++) {
            if(i !=1) {

    //Second for loop to check from prime number in the small to big number.

    

    //If the number i is 2 or 3, the loop will exit and add into the list as the default is prime, and no option changes it to not prime number.



                for(int x = 2; x < i ; x++) {
                    if(i%x == 0) {
    //If the number is divisable by any number to 0, set it to not prime.
                        primeValidation = false;
                    }       
                }
            }

`//if the input is either 0 or 1 set it to false and 0 and 1 is not prime
            

            `elseelse {
                primeValidation = false;
            }
            if(primeValidation == true) {
                listOfPrime.add(i);
            }else {
                primeValidation = true;
            }


        }

        System.out.println("The list of prime numbers of the number " + input + " are "+listOfPrime);


    }

}

How can I make my code more efficient on reading prime number, and continue on looping when 0 or 1 is input

import java.util.*;


public class PrimeNumber {
    private static ArrayList<Integer> listOfPrime = new ArrayList<Integer>();

    public static void main(String args[]) {
        Scanner userInput = new Scanner(System.in);
        System.out.print("Please enter a number to checked the list of prime number >  ");
    //Request user to input  number.
        String input = userInput.nextLine();
        int number = 0;
        boolean check = false;
    //Check if integer entered is it integer
        while(check != true) {
            try {
    // if input can be parse into integer, shows that its number so can change check into true to exit the while loop


                number = Integer.parseInt(input);
                check = true;
            }
            catch(NumberFormatException e) {
                System.out.println("Please enter numbers only  >  ");
                input = userInput.nextLine();
            }
        }

    //Check if input enter is 0 or 1, as prime number cannot be those.
        if(number < 2) {
            System.out.println("Prime number cannot be 0 or 1");
        }else {
            checkPrime(number);
        }


    }


    //Check for all prime numbers within the number user input.
    private static void checkPrime(int input) {
        boolean primeValidation = true;
    //first for loop to check from small to biggest integer in the input
        for(int i = 1; i <= input;i++) {
            if(i !=1) {

    //Second for loop to check from prime number in the small to big number.

    

    //If the number i is 2 or 3, the loop will exit and add into the list as the default is prime, and no option changes it to not prime number.



                for(int x = 2; x < i ; x++) {
                    if(i%x == 0) {
    //If the number is divisable by any number to 0, set it to not prime.
                        primeValidation = false;
                    }       
                }
            }

`//if the input is either 0 or 1 set it to false and 0 and 1 is not prime
            

            `else {
                primeValidation = false;
            }
            if(primeValidation == true) {
                listOfPrime.add(i);
            }else {
                primeValidation = true;
            }


        }

        System.out.println("The list of prime numbers of the number " + input + " are "+listOfPrime);


    }

}

Listing prime numbers up to the given number

import java.util.*;


public class PrimeNumber {
    private static ArrayList<Integer> listOfPrime = new ArrayList<Integer>();

    public static void main(String args[]) {
        Scanner userInput = new Scanner(System.in);
        System.out.print("Please enter a number to checked the list of prime number >  ");
    //Request user to input  number.
        String input = userInput.nextLine();
        int number = 0;
        boolean check = false;
    //Check if integer entered is it integer
        while(check != true) {
            try {
    // if input can be parse into integer, shows that its number so can change check into true to exit the while loop


                number = Integer.parseInt(input);
                check = true;
            }
            catch(NumberFormatException e) {
                System.out.println("Please enter numbers only  >  ");
                input = userInput.nextLine();
            }
        }

    //Check if input enter is 0 or 1, as prime number cannot be those.
        if(number < 2) {
            System.out.println("Prime number cannot be 0 or 1");
        }else {
            checkPrime(number);
        }


    }


    //Check for all prime numbers within the number user input.
    private static void checkPrime(int input) {
        boolean primeValidation = true;
    //first for loop to check from small to biggest integer in the input
        for(int i = 1; i <= input;i++) {
            if(i !=1) {

    //Second for loop to check from prime number in the small to big number.

    

    //If the number i is 2 or 3, the loop will exit and add into the list as the default is prime, and no option changes it to not prime number.



                for(int x = 2; x < i ; x++) {
                    if(i%x == 0) {
    //If the number is divisable by any number to 0, set it to not prime.
                        primeValidation = false;
                    }       
                }
            }

//if the input is either 0 or 1 set it to false and 0 and 1 is not prime
            

            else {
                primeValidation = false;
            }
            if(primeValidation == true) {
                listOfPrime.add(i);
            }else {
                primeValidation = true;
            }


        }

        System.out.println("The list of prime numbers of the number " + input + " are "+listOfPrime);


    }

}
added 332 characters in body
Source Link

import java.util.*;

public class PrimeNumber { private static ArrayList listOfPrime = new ArrayList();

import java.util.*;


public class PrimeNumber {
    private static ArrayList<Integer> listOfPrime = new ArrayList<Integer>();

    public static void main(String args[]) {
        Scanner userInput = new Scanner(System.in);
        System.out.print("Please enter a number to checked the list of prime number >  ");
    //Request user to input  number.
        String input = userInput.nextLine();
        int number = 0;
        boolean check = false;
    //Check if integer entered is it integer
        while(check != true) {
            try {
    // if input can be parse into integer, shows that its number so can change check into true to exit the while loop


                number = Integer.parseInt(input);
                check = true;
            }
            catch(NumberFormatException e) {
                System.out.println("Please enter numbers only  >  ");
                input = userInput.nextLine();
            }
        }

    //Check if input enter is 0 or 1, as prime number cannot be those.
        if(number < 2) {
            System.out.println("Prime number cannot be 0 or 1");
        }else {
            checkPrime(number);
        }


    }


    //Check for all prime numbers within the number user input.
    private static void checkPrime(int input) {
        boolean primeValidation = true;
    //first for loop to check from small to biggest integer in the input
        for(int i = 1; i <= input;i++) {
            if(i !=1) {

    //Second for loop to check from prime number in the small to big number.

    

    //If the number i is 2 or 3, the loop will exit and add into the list as the default is prime, and no option changes it to not prime number.



                for(int x = 2; x < i ; x++) {
                    if(i%x == 0) {
    //If the number is divisable by any number to 0, set it to not prime.
                        primeValidation = false;
                    }       
                }
            }

`//if the input is either 0 or 1 set it to false and 0 and 1 is not prime


`//if the input is either 0 or 1 set it to false and 0 and 1 is not prime
            

            `else {
                primeValidation = false;
            }
            if(primeValidation == true) {
                listOfPrime.add(i);
            }else {
                primeValidation = true;
            }


        }

        System.out.println("The list of prime numbers of the number " + input + " are "+listOfPrime);


    } 

}

}

import java.util.*;

public class PrimeNumber { private static ArrayList listOfPrime = new ArrayList();

public static void main(String args[]) {
    Scanner userInput = new Scanner(System.in);
    System.out.print("Please enter a number to checked the list of prime number >  ");
//Request user to input  number.
    String input = userInput.nextLine();
    int number = 0;
    boolean check = false;
//Check if integer entered is it integer
    while(check != true) {
        try {
// if input can be parse into integer, shows that its number so can change check into true to exit the while loop


            number = Integer.parseInt(input);
            check = true;
        }
        catch(NumberFormatException e) {
            System.out.println("Please enter numbers only  >  ");
            input = userInput.nextLine();
        }
    }

//Check if input enter is 0 or 1, as prime number cannot be those.
    if(number < 2) {
        System.out.println("Prime number cannot be 0 or 1");
    }else {
        checkPrime(number);
    }


}


//Check for all prime numbers within the number user input.
private static void checkPrime(int input) {
    boolean primeValidation = true;
//first for loop to check from small to biggest integer in the input
    for(int i = 1; i <= input;i++) {
        if(i !=1) {

//Second for loop to check from prime number in the small to big number.



//If the number i is 2 or 3, the loop will exit and add into the list as the default is prime, and no option changes it to not prime number.



            for(int x = 2; x < i ; x++) {
                if(i%x == 0) {
//If the number is divisable by any number to 0, set it to not prime.
                    primeValidation = false;
                }       
            }
        }

`//if the input is either 0 or 1 set it to false and 0 and 1 is not prime

        `else {
            primeValidation = false;
        }
        if(primeValidation == true) {
            listOfPrime.add(i);
        }else {
            primeValidation = true;
        }


    }

    System.out.println("The list of prime numbers of the number " + input + " are "+listOfPrime);


}

}

import java.util.*;


public class PrimeNumber {
    private static ArrayList<Integer> listOfPrime = new ArrayList<Integer>();

    public static void main(String args[]) {
        Scanner userInput = new Scanner(System.in);
        System.out.print("Please enter a number to checked the list of prime number >  ");
    //Request user to input  number.
        String input = userInput.nextLine();
        int number = 0;
        boolean check = false;
    //Check if integer entered is it integer
        while(check != true) {
            try {
    // if input can be parse into integer, shows that its number so can change check into true to exit the while loop


                number = Integer.parseInt(input);
                check = true;
            }
            catch(NumberFormatException e) {
                System.out.println("Please enter numbers only  >  ");
                input = userInput.nextLine();
            }
        }

    //Check if input enter is 0 or 1, as prime number cannot be those.
        if(number < 2) {
            System.out.println("Prime number cannot be 0 or 1");
        }else {
            checkPrime(number);
        }


    }


    //Check for all prime numbers within the number user input.
    private static void checkPrime(int input) {
        boolean primeValidation = true;
    //first for loop to check from small to biggest integer in the input
        for(int i = 1; i <= input;i++) {
            if(i !=1) {

    //Second for loop to check from prime number in the small to big number.

    

    //If the number i is 2 or 3, the loop will exit and add into the list as the default is prime, and no option changes it to not prime number.



                for(int x = 2; x < i ; x++) {
                    if(i%x == 0) {
    //If the number is divisable by any number to 0, set it to not prime.
                        primeValidation = false;
                    }       
                }
            }

`//if the input is either 0 or 1 set it to false and 0 and 1 is not prime
            

            `else {
                primeValidation = false;
            }
            if(primeValidation == true) {
                listOfPrime.add(i);
            }else {
                primeValidation = true;
            }


        }

        System.out.println("The list of prime numbers of the number " + input + " are "+listOfPrime);


    } 

}
Source Link

How can I make my code more efficient on reading prime number, and continue on looping when 0 or 1 is input

import java.util.*;

public class PrimeNumber { private static ArrayList listOfPrime = new ArrayList();

public static void main(String args[]) {
    Scanner userInput = new Scanner(System.in);
    System.out.print("Please enter a number to checked the list of prime number >  ");
//Request user to input  number.
    String input = userInput.nextLine();
    int number = 0;
    boolean check = false;
//Check if integer entered is it integer
    while(check != true) {
        try {
// if input can be parse into integer, shows that its number so can change check into true to exit the while loop


            number = Integer.parseInt(input);
            check = true;
        }
        catch(NumberFormatException e) {
            System.out.println("Please enter numbers only  >  ");
            input = userInput.nextLine();
        }
    }

//Check if input enter is 0 or 1, as prime number cannot be those.
    if(number < 2) {
        System.out.println("Prime number cannot be 0 or 1");
    }else {
        checkPrime(number);
    }


}


//Check for all prime numbers within the number user input.
private static void checkPrime(int input) {
    boolean primeValidation = true;
//first for loop to check from small to biggest integer in the input
    for(int i = 1; i <= input;i++) {
        if(i !=1) {

//Second for loop to check from prime number in the small to big number.



//If the number i is 2 or 3, the loop will exit and add into the list as the default is prime, and no option changes it to not prime number.



            for(int x = 2; x < i ; x++) {
                if(i%x == 0) {
//If the number is divisable by any number to 0, set it to not prime.
                    primeValidation = false;
                }       
            }
        }

`//if the input is either 0 or 1 set it to false and 0 and 1 is not prime

        `else {
            primeValidation = false;
        }
        if(primeValidation == true) {
            listOfPrime.add(i);
        }else {
            primeValidation = true;
        }


    }

    System.out.println("The list of prime numbers of the number " + input + " are "+listOfPrime);


}

}