-2

So the code I am working with is supposed to check if a number is prime and then I need to print out the result of, whether it is prime? From my instance and class method. I am having trouble because I feel like I set up everything correctly, but I am getting no results when I run the program. I will take any advice. Be easy on me this is my first year programming.

import java.io.IOException;
import java.util.Scanner;

public class Assignment4 {
    public static void main(String[] args) throws IOException {

        Scanner myInput = new Scanner(System.in);
        int someValue = myInput.nextInt();

        MyInteger myInt = new MyInteger(someValue);
        System.out.println("Testing instance method:");
        System.out.println(myInt.isPrime());
        System.out.println("Testing class method:");
        System.out.println(MyInteger.isPrime(myInt));
    }
}

class MyInteger {
    private int value;

    public MyInteger(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    public boolean isPrime() {
        int sqrt = (int) Math.sqrt((double)value);
        for(int i = 2; i <= sqrt; i++) {
            if (value % i == 0) return false;
        }
        return true;
    }

    public static boolean isPrime(MyInteger myInt) {
        return myInt.isPrime();
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    What do you mean by "no result"? Nothing prints at all? Please show what you're actually seeing when you run the program. – Hovercraft Full Of Eels Nov 19 '17 at 18:35
  • Please read [mcve] and enhance your question accordingly. – GhostCat Nov 19 '17 at 18:35
  • @GhostCat: actually, this **is** a valid MCVE. I've up-voted the question accordingly, and encourage you to do the same. I'm guessing that he's running it wrong. – Hovercraft Full Of Eels Nov 19 '17 at 18:35
  • The program is OK and, once you type in a number and hit enter, the results are there. – laune Nov 19 '17 at 18:36
  • `public static boolean isPrime(MyInteger myInt) {return myInt.isPrime();}` makes no sense since instead of `MyInteger.isPrime(myInt)` we can simply write `myInt.isPrime();`. That method should probably take `int` and not `MyInteger` as argument, then wrap it in `MyInteger` and return result of its `isPrime`, like `return new isPrime(intValue).isPrime();`. – Pshemo Nov 19 '17 at 18:37
  • "getting no results" doesn't sound mcve to me... – GhostCat Nov 19 '17 at 18:38
  • That's the problem statement, and yes that is deficient, but the code itself fulfills MCVE requirements. – Hovercraft Full Of Eels Nov 19 '17 at 18:38
  • Anyway your code seems to be *somehow* working https://ideone.com/SxSUSf. Only thing which may cause "not working" impression I can think of now is possible lack of any value in standard input. In other words your code may simply be waiting for user input at `int someValue = myInput.nextInt();` line before it moves on with rest of code. – Pshemo Nov 19 '17 at 18:48

2 Answers2

0

Program is working fine, and everything is good, i think you are not giving input to the program.

Run the program, write any number on the screen/console, press enter, you will see output then.

Here is the working screenshot: enter image description here

Rehan Javed
  • 418
  • 3
  • 14
0

Your class method has no benefit in the way you write it.

you have to pass an int instead of MyInteger

Like this:

public static boolean isPrime(int integer) {
    MyInteger myInt = new MyInteger(integer);
    return myInt.isPrime();
} 

And also your code is working

Ebraheem Alrabeea
  • 2,130
  • 3
  • 23
  • 42