Man Hei Li
Man Hei Li

Reputation: 31

How to use the value of a method in another method?

I'm trying to write a program to generate a random 3 digit number and reverse it, and so I wrote out two methods where getRandomNum generates the number and reverseDigits reverses it. However the 2nd method doesn't take in the random number generated from the 1st method, as the 1st method shows a 3 digit number but the 2nd method shows 0 when running the code.

I've tried looking up how to share variables between methods and it seems that I need to use static variables outside the methods. But it still shows 0 for reverseDigits.

Am I missing something or is there something else to be done?

public class MathTrick
{
    static int upperBound = 999;
    static int lowerBound = 100;
        
        //generate random 3 digit number
    static int getRandomNumber = 0;
    static int mDifference = 0;
    
    public static void main(String[]args)
    {
        getRandomNum();
        reverseDigits();
    }
    
    static void getRandomNum()
    {
        int upperBound = 999;
        int lowerBound = 100;
        
        //generate random 3 digit number
        int getRandomNumber = 0;
        int mDifference = 0;
        while (mDifference <= 1)
        {
            getRandomNumber = (int)(Math.random()*((upperBound-lowerBound)+1)) + lowerBound + 1;
            int x = (int)(getRandomNumber/100);
            int y = getRandomNumber%10;
            mDifference = Math.abs(x-y);
        }
        int m = getRandomNumber;
    
    }
    
    static int m = getRandomNumber;
    static void reverseDigits()
    {
        int a = m, reverseDigits = 0;
        while (a != 0)
        {
            int remainder = a % 10;
            reverseDigits = reverseDigits * 10 + remainder;
            a = a / 10;
        }
        int n = reverseDigits;
    }
}

Upvotes: 2

Views: 795

Answers (9)

Melron
Melron

Reputation: 579

Your second method (which is gonna return the reversed number) needs the random generated number as parameter.

Actually, your idea can be written like this:

public class MathTrick 
{
    static int upperBound = 999;
    static int lowerBound = 100;
    
    public static void main(String[] args) 
    {
        final int randomNumber = getRandomNum();
        final int reverseRandomNumber = reverse(randomNumber);
        
        System.out.println("Random number: " + randomNumber);
        System.out.println("Reverse random number : " + reverseRandomNumber);
    }

    static int reverse(int num) 
    {
        return Integer.parseInt(new StringBuilder(String.valueOf(num)).reverse().toString());
    }

    static int getRandomNum()
    {
        return ThreadLocalRandom.current().nextInt(lowerBound, upperBound);
    }
}

Upvotes: 0

Issa Khodadadi
Issa Khodadadi

Reputation: 1104

you should pass the random number to the method that reverse it. this is what you looking for:

public static void main(String[]args)
{
    int rand = getRandomNum();
    int reverse = reverseDigits(rand);

    System.out.println(rand);
    System.out.println(reverseDigits(rand));
}

static int getRandomNum()
{
    int upperBound = 999;
    int lowerBound = 100;

    //generate random 3 digit number
    int getRandomNumber = 0;
    int mDifference = 0;
    while (mDifference <= 1)
    {
        getRandomNumber = (int)(Math.random()*((upperBound-lowerBound)+1)) + lowerBound + 1;
        int x = (int)(getRandomNumber/100);
        int y = getRandomNumber%10;
        mDifference = Math.abs(x-y);
    }
    return getRandomNumber;

}



static int reverseDigits(int random)
{
    int reverseDigits = 0;
    while (random != 0)
    {
        int remainder = random % 10;
        reverseDigits = reverseDigits * 10 + remainder;
        random = random / 10;
    }
    return reverseDigits;
}

Upvotes: 1

public class MathTrick {
    public static void main(String[] args) {
        int randomNumber = getRandomNumber();
        int reverseRandomNumber = getReverseRandomNumber(randomNumber);
        System.out.println(randomNumber + "***" + reverseRandomNumber);
    }

    private static int getRandomNumber() {
        return (int) Math.round(Math.random() * 999);
    }

    private static int getReverseRandomNumber(int num) {
        return Integer.parseInt(new StringBuffer("" + num).reverse().toString());
    }
}

Try this. It may help you

Upvotes: 1

Hadi Hoteit
Hadi Hoteit

Reputation: 120

You can imagine Methods as boxes anything declared inside a method will be only visible to that method, so to have a variable Visible to all methods inside a class, the variable should be declared at the top of the class outside of all methods,and plus u should know how the variables declaration and initialization works The first Answer in this post explains them really well because i noticed you declared the variable multiple times, lets say the value that u needed was inside the first declared variable, when u declare the same variable again that old value will not be read from inside the method used to declare a second time and as a good practice u should have the variable as private and try not to use static variable often unless it is necessary. Example of what you should have:

public class MathTrick{

  private int number;
  public static void main(String[]args)
    {
        getRandomNum();
        reverseDigits();
    }

  public void getRandomNumb(){
  generate a number...
  number = NumberGenerated;
  }

  public void reverseDigits(){
  reverse the digits in variable number..
  }

}

Upvotes: 1

Yogendra
Yogendra

Reputation: 1300

first generate the random number than pass it in the reverse function.

static int getRandomNum(){
    //generate random number and return that number
    return n;
}
    
static int reverseDigits(int n){
    //reverse number code and return the number
    return n;
}
public static void main(String[]args)
{
    int number = getRandomNum();
    int reverse = reverseDigits(number);
}

Upvotes: 1

djm.im
djm.im

Reputation: 3323

The direct answer to your question

Replace this line int m = getRandomNumber; with m = getRandomNumber;

Basically, you overshadowed the static variable m with a local variable int m.


A few changes, here they are (my proposal).

Change that both methods to return an integer getRandomNum returns a new random number, and the reverseDigits method returns reversed number. Additionally, reverseDigits get a parameter - a number that should change.

So, after changes.

public static void main(String[] args) {
    int randomNum = getRandomNum();
    int reverse = reverseDigits(randomNum);

    System.out.println(randomNum);
    System.out.println(reverse);
}

static int getRandomNum() {
    // 
    return getRandomNumber;
}

static int reverseDigits(int m) {
    // 
    return reverseDigits;
}

And you can remove other static fields.

Upvotes: 2

gantoin
gantoin

Reputation: 185

Your methods need return statement, this could help you :

public class MathTrick {
    // generate random 3 digit number
    private int getRandomNumber = 0;
    private int mDifference = 0;

    private int m = getRandomNumber;

    public static void main(String[] args) {
        MathTrick application = new MathTrick();
        System.out.println("getRandomNum() return: " + application.getRandomNum());
        System.out.println("reverseDigits() return: " + application.reverseDigits());
    }

    private int getRandomNum() {
        while (mDifference <= 1) {
            int upperBound = 999;
            int lowerBound = 100;
            getRandomNumber = (int) (Math.random() * ((upperBound - lowerBound) + 1)) + lowerBound + 1;
            int x = (int) (getRandomNumber / 100);
            int y = getRandomNumber % 10;
            mDifference = Math.abs(x - y);
        }
        m = getRandomNumber;
        return getRandomNumber;
    }

    private int reverseDigits() {
        int a = m, reverseDigits = 0;
        while (a != 0) {
            int remainder = a % 10;
            reverseDigits = reverseDigits * 10 + remainder;
            a = a / 10;
        }
        return reverseDigits;
    }
}

Upvotes: 2

Harshita
Harshita

Reputation: 85

int m = getRandomNumber; has scope of only method getRandomNum(); To get random number to another method, variable m should be declared on class level.

Upvotes: 1

nurmanbetov
nurmanbetov

Reputation: 102

Your methods are void,try something like this by making them return int

static int getRandomNum(){
    //some code
    return n;
}

 static int reverseDigits(int n){
//some code
return n;
}

Upvotes: 1

Related Questions