0

my teacher asked us to create an additive prime program for my computer science class. I have created all my void methods, and believe to have all the math logic figured out. However, when I try to pass arguments into the instances of my methods within my main method, It gives me an error saying:

it can not find the variable in this case variable 'x'

package additiveprimes;

import java.util.Arrays;
import java.util.Scanner;


/**
 *
 * @author talarik048
 */
public class AdditivePrimes {

public static void main(String[] args){ 

    AdditivePrimes additivePrime = new AdditivePrimes();
    additivePrime.userInput(x);
    additivePrime.isPrime(x);
    additivePrime.numArray(x);

}

public void userInput(String x){
    Scanner sc = new Scanner(System.in);

    try{
        System.out.println("Please enter a number: ");
        x = sc.nextLine();
    } catch(NumberFormatException e){
        System.out.println("Error, try again: ");
        x = sc.nextLine();
    }

}

public void isPrime(String x){
    this.userInput(x);
    boolean prime = true;
    int y = Integer.parseInt(x);


    for(int i = 0; i < y; i++){

        if(y % i == 0){
            prime = false;
            break;
        }

        if(prime){
            System.out.println("Your number is prime...");
        } else {
            System.out.println("Your number is not prime...");
        }
    }

}

public void numArray(String x){
    this.userInput(x);
    String[] strArray = x.split("\\s+");
    boolean prime = true;

    int[] numbArray = new int[strArray.length];
    for(int j = 0; j < strArray.length; j++){
        try{
        numbArray[j] = Integer.parseInt(strArray[j]);
        } catch(NumberFormatException e){
            System.out.println("ERROR");
        }


       for(int i = 0; i < numbArray.length; i++){ 
       int sum = (Arrays.stream(numbArray).sum());
       if(sum % i == 0){
           prime = false;
           break;
       }
       if(prime){
           System.out.println("Your number is an additive prime...");
       } else {
           System.out.println("Your number is not an additive prime...");
       }
     }
    }

}

}
Eduardo Dennis
  • 12,511
  • 11
  • 72
  • 102
  • 1
    "x" is not defined. – Ousmane D. Mar 09 '17 at 15:50
  • What you are trying to do won't work. You should read https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value. Your userInput Method should **return** the input so that you can use them. You cannot pass a String to that method and then change that string inside the userInput method and have that an effect outside of the method. – OH GOD SPIDERS Mar 09 '17 at 15:54
  • 1
    Welcome to SO. For future please post a [MCVE] or SSCCE (http://sscce.org) that reproduces the problem. This will increase your chances of getting a helpful answer (and also may help you find the problem). – c0der Mar 09 '17 at 16:02
  • 1
    Also something that is unrelated to your Problem: Your catching of NumberFormatException is pretty useless as no line in the try block will ever throw it if you just use nextLine(). – OH GOD SPIDERS Mar 09 '17 at 16:06

3 Answers3

3

I think you wanted to RETURN a value from userInput:

public static void main(String[] args){ 

    AdditivePrimes additivePrime = new AdditivePrimes();
    String x = additivePrime.userInput();
    additivePrime.isPrime(x);
    additivePrime.numArray(x);

}

public String userInput(){
    Scanner sc = new Scanner(System.in);
    String x = null;
    try{
        System.out.println("Please enter a number: ");
        x = sc.nextLine();
    } catch(Exception e){// see  OH GOD SPIDERS comment
        System.out.println("Error, try again: ");
        x = sc.nextLine();//this is not a good way for a retry
    }
    return x;
}

Alternatively you could make x a field.

c0der
  • 15,550
  • 6
  • 26
  • 53
1

Your methods accept a string as an argument, but you have not passed any. You need to initialize x in main.

public static void main(String[] args){ 

    AdditivePrimes additivePrime = new AdditivePrimes();
    String x= "55";
    additivePrime.userInput(x);
    additivePrime.isPrime(x);
    additivePrime.numArray(x);

}

Note

Although the above code fixes your current issue. It doesn't seem to be the correct way to use this class. You should probably be calling .userInput() method and getting the userInput then passing it to your other methods.

You could probably change your overall class to

public static void main(String[] args){ 

    AdditivePrimes additivePrime = new AdditivePrimes();
    String x = additivePrime.userInput();
    additivePrime.isPrime(x);
    additivePrime.numArray(x);

}

public String userInput(){
    Scanner sc = new Scanner(System.in);
    String x = null;
    try{
        System.out.println("Please enter a number: ");
        x = sc.nextLine();
    } catch(NumberFormatException e){
        System.out.println("Error, try again: ");
        x = sc.nextLine();
    }
    return x;
}
Eduardo Dennis
  • 12,511
  • 11
  • 72
  • 102
0

You have not defined x as anything before using it. Try defining x. In this case since your arguments are strings, I recommend...

String x = new String();
Nova Ardent
  • 151
  • 16