Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:14:24+00:00 2026-06-18T07:14:24+00:00

using Eclipse, Java Try to make method for checking whether a number is prime

  • 0

using Eclipse, Java

Try to make method for checking whether a number is prime (means can be devided by1 and by himself) or not

Currently was created some functionality (code below). This code return required result, but not for all numbers that are prime, means for number 33 and 55 it’s return false instead of true.

Code

private static void numberCheker() throws IOException {
    System.out.println("Enter number - ");
    int number;
    number = getNumber();
    boolean result = true;
    if ((number % 2 == 0) && (number != 2)) {
        System.out.println("Not");
    } else if (number == 1) {
        System.out.println("1 not a valid number");    
    } else if (number == 2)
        System.out.println("Simple");    
    else {
        for (int i = 2; i < number + 1; i++) {
            if (number % i == 0) {
                result = false;
            } else
                result = true;
        }
        if (result = true)
            System.out.println("Simple");    
        else 
            System.out.println("not Simple");
    }
}

The question – where i’m make mistake in my “logic” – spend few hours for fixing – but cant do that. Any suggestion?

Result must be like below

Enter number - 
1
simple
Enter number - 
2
simple
Enter number - 
3
simple
Enter number - 
4
not simple
Enter number - 
5
simple

…

but i got

Enter number - 
33
not simple - **WRONG**
Enter number - 
55
not simple - **WRONG**

EDIT:

many thanks all
try to do like Michael Konietzka sad:

 private static boolean numberCheker() throws IOException {

    System.out.println("Enter number - ");
    int number = getNumber();

    for (int i=2;i<number;i++)
     {
        if (number%i==0) return false; 
     }
        return (number>1);

}

and getNumber()

public static int getNumber () throws IOException{
int temp;
try{    
    BufferedReader bReader = new BufferedReader (new InputStreamReader(System.in));
    String resultReader = bReader.readLine();
    temp =  Integer.parseInt(resultReader);
    return temp;
}
catch (NumberFormatException failDescription){
    System.out.println(failDescription);
    return 0;
}

}

now its work perfect
Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:14:25+00:00Added an answer on June 18, 2026 at 7:14 am

    You may put the check in a static method of your class:

    public static boolean isPrime(final int n)
        {
         for (int i=2;i<n;i++)
         {
            if (n%i==0) return false; 
         }
            return (n>1);
        }
    

    You must iterate trough all numbers from 2 to n-1 and check, if n is divisible by i without remainder. If so, the method can return immediately false, because one factor was found. If no factor is found between 2 and n-1 and the number is greater 1 it is a prime; the method returns true.

    To use this method you can use it simple this way for example:

    public static void main (String[] args) 
      {
         for (int i=1;i<10;i++)
         {
             System.out.println(i+": "+isPrime(i));
         }
    

    which will output

    1: false

    2: true

    3: true

    4: false

    5: true

    6: false

    7: true

    8: false

    9: false

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i can connect mysql with java using eclipse in a java application with these
This is my first time using Eclipse 3.3.2 to make an application in Java.
As an experiment we want to build our products using the Eclipse java compiler
I am using Eclipse Helios for Java. Everything was working fine until recently. The
I have web application written in java using Eclipse. It has just one servlet
How to create simple webserver in Java using Eclipse, Tomcat and Jersey i.e steps
I've been developing an Android app in Eclipse (java) using openGL ES 1.0 on
I have recently created a web project in Java using eclipse. I have a
When debugging Java code using Eclipse, for collection variables, I saw the modcount member.
I am using the latest Eclipse for Java EE & the latest JBoss Tools

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.