user7344108
user7344108

Reputation:

Calling void methods from main method, cannot pass arguments within void methods

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...");
       }
     }
    }

}

}

Upvotes: 0

Views: 500

Answers (3)

Nova Ardent
Nova Ardent

Reputation: 161

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();

Upvotes: 0

c0der
c0der

Reputation: 18792

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.

Upvotes: 2

Eddie Martinez
Eddie Martinez

Reputation: 13910

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;
}

Upvotes: 1

Related Questions