0

I'm trying to figure out some issue I'm having.

Basically what my issue is, is that the values that are being returned by my methods aren't right.

I have the print line statement just to be sure it's working but it always returns 1, even when I call to another method that should return a String.

The variable current_number/image_number is supposed to be updated every time I call to a method (If I keep calling to forward starting from 1, I should be getting 2, 3, 4 etc..).

This is my code

public class Menu {

    static final int MIN_NUMBER = 1;
    static final int MAX_NUMBER = 8;
    static int image_number = 1;
    static boolean exit;

    public static int forward(int current_number) {
        if (current_number < MAX_NUMBER) {
            current_number++;
        } else if (current_number >= MAX_NUMBER) {
            current_number = MIN_NUMBER;
        }
        return current_number;
    }

    public static void showMenu() {
        int current_number = image_number; // global int that equals 1

        while (!exit) {
            Scanner input = new Scanner(System.in);
            Random rand = new Random();
            int randomvalue = rand.nextInt(MAX_NUMBER) + MIN_NUMBER; // used in another method

            System.out.println("1. forward"); // menu with options
            System.out.println("2.");
            System.out.println("3.");
            System.out.println("4. Exit");
            System.out.println(current_number);

            int choice = input.nextInt();

            switch (choice) {
                case 1:
                    forward(current_number);
                    break;

                case 4:
                    exit = true;
                    break;
            }
        }
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • please correct your code indentation – Ami Hollander Mar 15 '18 at 22:03
  • Please indent your code correctly. It is unreadable for us in the current state. Just google *"java formatter"*, there are plenty of tools. – Zabuzard Mar 15 '18 at 22:03
  • I think you should just simply say what you want and I will see if I can provide you a simpler code, this your code seems to much and probably unnecessary! No offense just explain what you want to accomplish. – Wale Mar 15 '18 at 22:11
  • The code `current_number++;` in your `forward` method only changes a **local copy** of the variable, not the `current_number` variable you passed from the `showMenu` method. Take a look at the linked duplicate that explains why this is the case. – Zabuzard Mar 15 '18 at 22:12
  • Really sorry about the guys, I'm at work and just kind of quickly posted it up. I appreciate everyone helping me out! – Enrique Shockwave Mar 15 '18 at 22:21

3 Answers3

1

Ok, I think I understand.

In your switch statement, when you call the forward method, where the new number is return, you need a value to pass the new number to, otherwise it gets lost.

Changing your code to this might help.

switch (choice) {
    case 1:
      current_number = forward(current_number);
      break;

    case 4:
      exit = true;
      break;
}
Rob G
  • 21
  • 3
1

int is a primitive data type in Java. This means that your function forward's current_number is a different instance than your methods current_number. The variable you passed in is of the same value, but not the same reference.

To fix you need to assign the value of current_number to the result of forward.

current_number = forward(current_number);

Useful reads:

ug_
  • 11,267
  • 2
  • 35
  • 52
0

whenever you call the method a copy of current_number is passed. Then when the method ends, the copy is discarded. Try using the Integer object instead.

Nathan
  • 107
  • 6
  • While that works, I wouldn't suggest using `Integer`. It is slower and takes more space. A simply catch and update in `showMenu` of the `current_number` variable would do it too. – Zabuzard Mar 15 '18 at 22:14
  • Most computers have the memory and speed that the difference between int and Integer is negligible. If he's working on an embedded system, this might come into play, but that doesn't seem like the case here. – Nathan Mar 15 '18 at 22:20