Jump to content

it tell me: This method must return a result of type boolean

rubendelope
import java.util.Scanner;
public class example{
public static boolean isPrime (int a) {
  Scanner in = new Scanner(System.in);
  a = in.nextInt();
  boolean isItPrime = true;


  for(int j = 2; j<= 50; j++){
    if(a % j == 0){
      return isItPrime;
      
    }
    else if(isItPrime){
     return isItPrime;
    
  }
  System.out.println(isItPrime);

  
  }
  
 

  
  



}
public static void main(String[] args) {
  isPrime();
}
}

Delete this before posting. Please make sure to include the language that you're using in the title, and use the <> button for any code.

 

Link to comment
Share on other sites

Link to post
Share on other sites

since your return statements are inside conditional statements, the compiler can't tell if the function always returns a boolean (which it must, since it's declared as a boolean function).

 

Also that function will never work as you expect it to. It can only return true. Think about what you're writing.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

I'll admit up front here that my programming ability is very limited and my knowledge of Java rusty, but at a glance, there's a lot of weird stuff going on here.

  1. You're defining a function that requires an argument, that "int a" you're declaring in the parentheses of isPrime. But then you're not passing any value to the function once you call it in the main function and instead using user input from the Scanner to assign a value to the variable "a".
  2. If you want to define a function that does everything on its own, prints all the statements and doesn't return anything for further processing, you don't need to defined isPrime as a boolean, but rather a void (meaning it returns no value).
  3. You have that for loop that always returns true regardless of what happens. Also, this error specifically happens, because you're using "return" from within a loop and if statement. You should use a helper variable that you set inside the loop and at the very end of the function return that.
  4. That for loop only works for numbers between 2 and 50 and there's no error handling for anything outside that range.

Here's a quick example of what you're intending to do with a function that returns a boolean value for further processing. Note that this has absolutely no error handling if someone enters an invalid value. Also, I'm sure any competent programmer could whip up something far more elegant than this.

import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int num = scan.nextInt();
		scan.close(); //Closing the scanner avoids potential memory leaks.

		/* We're checking if the value is a prime number with our 
		isPrime() function and then deciding what to print.*/
		if (isPrime(num) == true){
			System.out.println(num + " is a prime number.");
		} else {
			System.out.println(num + " is not a prime number.");
		}
  }

	public static boolean isPrime(int a){
		int i;

		/*Since we're checking for a prime number, we only need to 
		loop through half the value of the desired number. 
		(int) Math.ceil() rounds up the division to the next integer.*/
		int j = (int) Math.ceil(a / 2);
		
		/*We assume every value we're passed is a prime number by default, 
		therefore true. If it proves to be divisible by anything else, 
		we set it to false. This is the helper variable that at the end of 
		the function gets returned.*/
		boolean result = true;

		//If the number is below 2, we can already determine that it's not a prime number.
		if (a < 2){
			result = false;
		} else {
			for (i = 2; i < j; i++){
				if(a % i == 0){
					result = false;
				}
			}
		}
		return result;
	}
}

 

And now a word from our sponsor: 💩

-.-. --- --- .-.. --..-- / -.-- --- ..- / -.- -. --- .-- / -- --- .-. ... . / -.-. --- -.. .

ᑐᑌᑐᑢ

Spoiler

    ▄██████                                                      ▄██▀

  ▄█▀   ███                                                      ██

▄██     ███                                                      ██

███   ▄████  ▄█▀  ▀██▄    ▄████▄     ▄████▄     ▄████▄     ▄████▄██   ▄████▄

███████████ ███     ███ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀████ ▄██▀ ▀███▄

████▀   ███ ▀██▄   ▄██▀ ███    ███ ███        ███    ███ ███    ███ ███    ███

 ██▄    ███ ▄ ▀██▄██▀    ███▄ ▄██   ███▄ ▄██   ███▄ ▄███  ███▄ ▄███▄ ███▄ ▄██

  ▀█▄    ▀█ ██▄ ▀█▀     ▄ ▀████▀     ▀████▀     ▀████▀▀██▄ ▀████▀▀██▄ ▀████▀

       ▄█ ▄▄      ▄█▄  █▀            █▄                   ▄██  ▄▀

       ▀  ██      ███                ██                    ▄█

          ██      ███   ▄   ▄████▄   ██▄████▄     ▄████▄   ██   ▄

          ██      ███ ▄██ ▄██▀ ▀███▄ ███▀ ▀███▄ ▄██▀ ▀███▄ ██ ▄██

          ██     ███▀  ▄█ ███    ███ ███    ███ ███    ███ ██  ▄█

        █▄██  ▄▄██▀    ██  ███▄ ▄███▄ ███▄ ▄██   ███▄ ▄██  ██  ██

        ▀███████▀    ▄████▄ ▀████▀▀██▄ ▀████▀     ▀████▀ ▄█████████▄

 

Link to comment
Share on other sites

Link to post
Share on other sites

Take a look at this thread ^ ^

On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×