anon
anon

Reputation:

Java Method use the wrong value of a variable

I'm currently studying for an exam and I found a little problem with a basic code I wrote to learn methods (I just started, I'm a newbie).

The problem here is that I created a method called test which is supposed to do a simple product between an already stated number and another number decided by the user. I had to declare the variable of the user input as another method because it wouldn't work otherwise, so I created it before everything else and gave it value 0. The problem here is that when I use the method "test", the one which should do the product, it uses the value of the variable I stated in the beginning instead of the one that the user decided.

Here's the code:

import java.util.*;
import java.io.*;

public class ProvaMetodi {
    public static int numero; //User's input variable
    public static void test(){ //The problematic method
        int paolo = 23;  //I decided to multiply the user's input by this number
        int prodotto = paolo*numero; //Just a product
        System.out.println(prodotto); //Tried this, before it was an int class which 
                                     //returned the int "prodotto" so i could use it in different scenarios.
        }
        public static void main (String args []){
            Scanner gigi = new Scanner(System.in);
            System.out.println("Scrivi un numero e lo moltiplico per 23."); //Just the instructions for the user
            int numero = gigi.nextInt();
            System.out.println("Numero="+numero); //Added this for debugging purposes. The output is the user's input, so it's correct.
            test();  //here's the output, which is always 0.
        }
    }

Upvotes: 0

Views: 315

Answers (4)

Sohel0415
Sohel0415

Reputation: 9853

You can get rid of static method and static variables entirely and good practice to declare const at the top of class as a static final-

import java.util.*;
import java.io.*;

public class ProvaMetodi {

  private static final int CONST_PAOLO=23;
  public void test(int numero) {
    int prodotto = CONST_PAOLO * numero;
    System.out.println(prodotto);
  }

  public static void main(String args[]) {
    Scanner gigi = new Scanner(System.in);
    System.out.println("Scrivi un numero e lo moltiplico per 23.");
    int numero = gigi.nextInt();
    System.out.println("Numero=" + numero);
    (new ProvaMetodi()).test(numero);
  }
}

Upvotes: 0

sascha10000
sascha10000

Reputation: 1230

If you want to stay with your code change 'int numero = gigi.nextInt();' to 'numero = gigi.nextInt();'. Otherwise you are defining 'numero' locally and you do not access the global instance of 'numero'.

Take a look at Scopes and read something about it. It may help you to understand the problem.

There are many design issues in your code, but that doesn't matter as you are a beginner.

But this would be a clean approach:

package betahelper;

import java.util.Scanner;

class Test {
  public static void test(int numero){ //The problematic method
    int paolo = 23;  //I decided to multiply the user's input by this number
    int prodotto = paolo*numero; //Just a product
    System.out.println(prodotto); //Tried this, before it was an int class which
//returned the int "prodotto" so i could use it in different scenarios.
  }
  public static void main (String args []){
    Scanner gigi = new Scanner(System.in);
    System.out.println("Scrivi un numero e lo moltiplico per 23."); //Just the instructions for the user
    int numero = gigi.nextInt();
    System.out.println("Numero="+numero); //Added this for debugging purposes. The output is the user's input, so it's correct.
    test(numero);  //here's the output, which is always 0.
  }
}

As stated before changing test() to test(int).

Upvotes: 0

Eran
Eran

Reputation: 393781

You have two numero variables:

  1. The first one - int numero = gigi.nextInt(); - is the local variable inside your main method into which you read the user input.
  2. The second one is public static int numero;. This one is being used by the test method, and its value remains 0 by default.

You'd better pass the user input to your test method:

public static void test(int numero) {
    int paolo = 23;  
    int prodotto = paolo*numero;
    System.out.println(prodotto);                     
}

and in main:

test(numero);

You can remove the static variable - public static int numero;.

Upvotes: 3

achAmháin
achAmháin

Reputation: 4266

Remove the static int.

You need to pass the input to the test() method

So change the method to accept the int like so:

public static void test(int a){ 

And also change what your multiplying by to match the parameter:

int prodotto = paolo * a;

Then call it in your main with the user input:

test(numero);  

Upvotes: 1

Related Questions