0

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.
        }
    }
2
  • 1
    Why do you think int numero = gigi.nextInt(); will reassign the class variable numero, rather than creating a new variable with the same name? Commented Jan 11, 2018 at 10:42
  • @Tom Basically I already wrote the “int numero = gigi.nextInt()” part, but then the test method gave me an error about the fact that there was no var named numero. So I created the method numero and didn’t thought about the fact that I had two identical var Commented Jan 11, 2018 at 11:01

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot! Didn’t thought about the fact that I had two variables.
1

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

2 Comments

And “a” will become my user’s input variable, right?
Yes, a is just what I called it and then it's used in the test() method. I just changed the name to avoid confusion (even though I recommended removing the static variable).
0

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).

1 Comment

Thanks! Yes, I’m a beginner so for now I’m just trying to understand how things works
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.