ichigo gyuunyuu
ichigo gyuunyuu

Reputation: 693

Use Variable Which is in main method in Another Method

I'm trying to create a simple program to output the number of stars entered by user. I'm trying to learn how to use more than one method to do this Here's my code

import java.util.Scanner;
public class Alpha 
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
         int n;

        System.out.println("Enter no. of stars");
        n = input.nextInt();

    }
    public static void Loop ()
    {
        for (int counter = 1; counter <= n; counter++)
        {
            System.out.println("*");
        }
    }
}

The problem I'm facing is that in the Loop method, I am unable to use the variable n Is there a way to use a variable which is in the main method, in another one? Ty

-Pingu

Upvotes: 3

Views: 17412

Answers (7)

Pawan
Pawan

Reputation: 6

Like this you can use variable from different methods in different methods

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int sum=0;
    int a=Sum(sum);
    System.out.println("The Average of the numbers is: "+a);
    
}

public static int Sum(int sum) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the total count of number for Average: ");
    int a=sc.nextInt();
    for(int i=1;i<=a;i++) {
        
        System.out.println("Enter the"+i+"Number: ");
        int b=sc.nextInt();
        sum+=b;
    }
    int avg=sum/a;
    return avg;
}

Upvotes: 0

CoderNeji
CoderNeji

Reputation: 2074

Pass it as a paramteer

import java.util.Scanner;
public class Alpha 
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
         int n;    
        System.out.println("Enter no. of stars");
        n = input.nextInt();    
        loop(n); // added this

    }
    public static void loop (int n) // changed here
    {
        for (int counter = 1; counter <= n; counter++)
        {
            System.out.println("*");
        }
    }
}

Upvotes: 2

dly
dly

Reputation: 1088

Two ways.. one has been posted already as answer and the other one would be using the variable as a field. This way you can access (and modify) it in every method without having to pass it on.

public class Alpha 
{

    static int n;
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter no. of stars");
        n = input.nextInt();
        loop();
    }
    public static void loop ()
    {
        for (int counter = 0; counter < n; counter++)
        {
            System.out.println("*");
        }
    }
}

And please start method names with lowercase and counting with 0. It's common practise and it helps a lot to use the standards right from the beginning.

Upvotes: 0

Maslor
Maslor

Reputation: 1910

import java.util.Scanner;
public class Alpha 
{
public static void main(String args[])
{
    Scanner input = new Scanner(System.in);
     int n;

    System.out.println("Enter no. of stars");
    n = input.nextInt();

    Loop(n); //calls Loop function and passes parameter n
}
public static void Loop(int n)  //this function now expects a number n
{
    for (int counter = 1; counter <= n; counter++)
    {
        System.out.println("*");
    }
}
}

Upvotes: 3

OldSchool
OldSchool

Reputation: 2183

I think you should use it as a instance variable and for better understanding name your class like StarClass it can provide better understanding. Good programming practice.

But you should avoid unneccesserily making instance variable without any logic behind it.

Upvotes: 1

Antonio Marino
Antonio Marino

Reputation: 26

I also think you could declare n as a public variable. That should make it accessible throughout the code.

public int n;

But I guess that passing it as parameter is a better practice, since you don't create a deppendance inside your code. What I mean is, if something changes with the variable you break the function. It's good practice to always keep things "modular" in your code, so it makes it more resilient to changes and debugging. It's better if you get used to it from the beggining =)

Upvotes: 0

nano_nano
nano_nano

Reputation: 12523

simply pass it as parameter:

public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
         int n;

        System.out.println("Enter no. of stars");
        n = input.nextInt();
        Loop(n);

    }
    public static void Loop (int count)
    {
        for (int counter = 1; counter <= count; counter++)
        {
            System.out.println("*");
        }
    }

Upvotes: 2

Related Questions