Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Var ends up being 0

  1. #1
    Junior Member
    Join Date
    Oct 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Var ends up being 0

    hello! I am currently making a beginner level program that makes an addition and stores it in a map to make some tests on functions! But when i wish to store the variables in the map, their value is 0 in the function! Here is the code. If anyone knows how to fix this i'd appreciate it!




    public class Additions {
    static Map<String, Integer>storeResult = new HashMap<String, Integer>();
    static int a;
    static Scanner keyboard = new Scanner(System.in);
    static Scanner doYouContinue = new Scanner(System.in);
    static int b;
    static String clientsChoice;
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    do{
    System.out.println("Donnez un entier:");
    int a = keyboard.nextInt(); // FOR TEST I GAVE 3
    System.out.println("Donnez un autre entier:");
    int b = keyboard.nextInt(); // FOR TEST I GAVE 4
    additionate(a, b);
    }while(!clientsChoice.equals("non"));
    }

    private static void additionate(int a, int b) {
    // TODO Auto-generated method stub
    final int z = a+b;
    System.out.print(" C'est egal à ");
    System.out.println(z); // PRINTED 7
    storeInMap(z);
    }

    private static void storeInMap(int z) {
    // TODO Auto-generated method stub
    System.out.println(a); // THIS INDICATES 0
    String stringA = String.valueOf(a);// MAKING SOME TESTS TO TRY TO FIX IT
    String stringB = String.valueOf(b);
    storeResult.put(stringA+"+"+stringB, z);
    continuing();
    }

  2. #2
    Junior Member
    Join Date
    Dec 2021
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Var ends up being 0

    You creating local variables here :
    int a = keyboard.nextInt(); // FOR TEST I GAVE 3
    System.out.println("Donnez un autre entier:");
    int b = keyboard.nextInt(); // FOR TEST I GAVE 4

    these values do not get saved. what you meant to do here :

    a = keyboard.nextInt(); // FOR TEST I GAVE 3
    System.out.println("Donnez un autre entier:");
    b = keyboard.nextInt(); // FOR TEST I GAVE 4

    Because you declared those variables, the values will be properly saved this way.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Var ends up being 0

    Well spotted by armela.
    But why are you passing a and b as parameters to function additionate(), but not to function storeInMap() ? This is so inconsistent. Either pass your variables down to functions, or use global variables - but do not mix both methods willy-nilly.

Similar Threads

  1. Replies: 6
    Last Post: August 3rd, 2012, 02:36 PM
  2. Starting a New Game After One Ends.
    By Delgado21 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 19th, 2011, 07:11 AM