-1

I'm having trouble implementing 3 methods to create a program that checks if a number is prime. When I call my methods and run the program the only value that shows is 0. I'm assuming this has to do with variables or logic in my methods

I have tried using different variables to store user input then using that variable as an argument in my methods.

package practice;
import java.util.Scanner; 

public class Practice {

    static Scanner s = new Scanner(System.in);  

    public static void main(String[] args) {
        //int result = 0 ; //stores number user picks 
        int numPicked = 0; //
        int endResults = 0; //stores result of calculation 

        // Calling methods 
        isPrime(numPicked);
        pickedNum(numPicked);
        results(endResults);
    }

    // Method to check if numbers are prime
    public static boolean isPrime(int numPicked){
        if (numPicked <= 1) {
            return false;  
        }  
        for (int i = 2; i <= numPicked; i++) {  
            if (numPicked % i == 0) {  
                return false;  
            }  
        }  
        return true;  
    }  

    // Method that asks user for a positive integer 
    public static int pickedNum (int userNumbers){  
        Scanner s = new Scanner(System.in);
        System.out.println("Type a positive number that you want to know if it's prime or not.");
        int numPicked = s.nextInt();
        return numPicked;
    }

    // Method that displays result of calculation 
    public static int results (int numPicked){

        if(numPicked == 0 && numPicked != 1 ){
            System.out.println( numPicked + " is a Prime Number");
        }
        else{
            System.out.println(numPicked + " is Not a Prime Number");
        }
        return numPicked;
    }
}

I need to fix the logic within my methods to be able to call them correctly.

Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
  • Possible duplicate of [What is wrong with my isPrime method?](https://stackoverflow.com/questions/20798391/what-is-wrong-with-my-isprime-method) – alea Sep 12 '19 at 14:36
  • Please see this [How to use debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – Yoshikage Kira Sep 12 '19 at 14:36
  • 3
    Look at the order in which you call your methods in the main method. First you call `isPrime`, and pass it a hard-coded value of 0. Then you get the user input, but never store it in a variable. Then you print the results based in that initial, hard-coded 0. You need to get the user input first, store it in a variable, pass that variable to the `isPrime` method, store the return value from that method, and then pass that value to the `results` method. Also, your `results` method always says that 0 is prime, and non-zero numbers are not prime. It should use the boolean from `isPrime` – Jordan Sep 12 '19 at 14:36
  • @johnny 5 Where do you see the `return true` for numbers greater than 1? And for even numbers? His problem is he is just disregarding any results he is getting from the methods. – Nexevis Sep 12 '19 at 14:40
  • @johnny 5 And before that last line the `if` statement will `return false` for any value less than `1` so it will never reach that line. And then the `for` loop will `return false` for all even values because of `numPicked % i == 0` in the first loop iteration. – Nexevis Sep 12 '19 at 14:58
  • @johnny5: No, it returns `false`. Proof: https://repl.it/repls/NovelMenacingGraphicslibrary The logic is still flawed, though, as it returns that for _all_ numbers. – Marvin Sep 12 '19 at 16:23
  • @Marvin oops didn't read the full code you're correct – johnny 5 Sep 12 '19 at 16:53

2 Answers2

0
package test;

import java.util.Scanner; 

public class Practice {

    static Scanner s = new Scanner(System.in);  

    public static void main(String[] args) {
        int numPicked = 0; //

Commented out endResults as it currently has no functionality.

        //int endResults = 0; //stores result of calculation 

Added a Boolean to record whether isPrime is true or false

    boolean isPrime;

numPicked now stores the returned value of the pickedNum method and it takes no parameters.

    // Calling methods 
    numPicked = pickedNum();
    isPrime = isPrime(numPicked);

Pass the Boolean value to the results method, true or false.

    results(numPicked, isPrime);

    }



    // Method to check if numbers are prime
    public static boolean isPrime(int numPicked) 
    {
        if (numPicked <= 1) {  
            return false;  
        } 

Removed <= with just less than. This is as if numPicked equals its own value it will return false. In reality a prime number can only be divided by itself so you just need to check the numbers less than it.

    for (int i = 2; i < numPicked; i++) {  
        if (numPicked % i == 0) {  
            return false;  
        }  
    }  
    return true;  
}  

    // Method that asks user for a positive integer 
    public static int pickedNum ()
    {   
        Scanner s = new Scanner(System.in);


        System.out.println("Type a positive number that you want to know if it's prime or not.");
        int numPicked = s.nextInt();


        return numPicked;
    }

    // Method that displays result of calculation 
    public static int results (int numPicked, boolean isPrime)
    {

Checks if isPrime is true.

        if(isPrime)
        {
            System.out.println( numPicked + " is a Prime Number");
        }
        else
        {
            System.out.println(numPicked + " is Not a Prime Number");
        }
        return numPicked;
    }
}
Deane Kane
  • 126
  • 2
  • 15
0

I did some correction in your code and working version is below. There is a lot of things that should be also changed but I tried to make your program working without big changes.

So first of all, You had 2 Scanners so I deleted one of them. Second thing your isPrime method was not working correctly so I replace it with working version. Other thing was that you called isPrime before loading value which was main problem that you always got 0. Other thing is that java is pass by value it means that if you put int as argument and you modify it inside method you still have old value so you have to assign returned value to your value again

static Scanner s = new Scanner(System.in);  

    public static void main(String[] args) {
        //int result = 0 ; //stores number user picks 
        int numPicked = 0; //
        boolean isPrime = false;


        // Calling methods 
        numPicked = pickedNum();
        isPrime = isPrime(numPicked);
        results(numPicked, isPrime);
    }

    // Method to check if numbers are prime
    public static boolean isPrime(int numPicked) {
        if (numPicked == 2)
            return true;
        if (numPicked < 2 || numPicked % 2 == 0)
            return false;
        for (int i = 3; i * i <= numPicked; i += 2)
            if (numPicked % i == 0)
                return false;
        return true;
    }

    // Method that asks user for a positive integer 
    public static int pickedNum()
    {   
        System.out.println("Type a positive number that you want to know if it's prime or not.");
        int numPicked = s.nextInt();

        return numPicked;
    }

    // Method that displays result of calculation 
    public static int results (int numPicked, boolean isPrime)
    {

        if(isPrime)
        {
            System.out.println( numPicked + " is a Prime Number");
        }
        else
        {
            System.out.println(numPicked + " is Not a Prime Number");
        }
        return numPicked;
    }
Rafał Sokalski
  • 1,817
  • 2
  • 17
  • 29
  • Thank you, that helped me understand it. Just another question. What does this for loop do? for (int i = 3; i * i <= numPicked; i += 2) (I just want to be able to understand the purpose of it) – Eliza Delgado Sep 13 '19 at 02:54
  • @ElizaDelgado I just found quickly some algorithm for checking if it is prime or not. It was from this link: https://stackoverflow.com/a/20798440/6003541 – Rafał Sokalski Sep 16 '19 at 09:15