-2

The new number will not switch over and I'm not sure why, it reads just fine but will not take over the old number's spot.

 private static void newNumber(int num, Scanner kb)
 {         
     System.out.println("Enter in a value: ");
     num = kb.nextInt();

     if (num < 0)
     {
        System.out.println("Number not accepeted, enter a non negative number");
     }
     else
     {
        System.out.println("New number you entered was: " + num);
     }  
}
intcreator
  • 4,206
  • 4
  • 21
  • 39
Tav
  • 1
  • 2
  • What is the `newNumber` function supposed to do? Replace the parameter `num` with a number the user gives as input? – intcreator Jan 27 '16 at 04:34
  • It needs to replace the number the user inputted at the beginning of the code with the new number the user typed in...for example if they inputted 8 at the beginning, then from the menu chose to change the number it needs to replace the 8 with the new number they typed with the code inputted at the top – Tav Jan 27 '16 at 05:14
  • Please edit your question to make that clear. You may want to include a function call to illustrate your point. – intcreator Jan 27 '16 at 05:54

2 Answers2

0

Why are you using the nextInt() method? You could just do num++ Here is what I would do:

 private static void newNumber(int num)
 {         
     num++;

     if (num < 0)
        System.out.println("Number not accepeted, enter a non negative number");

     else
        System.out.println("New number you entered was: " + num);
}

Also, since this is a method, you should be getting the user input before the method, and putting it through as an argument, which you have it set up for, but never use.

asdfasdfadsf
  • 381
  • 3
  • 14
  • I have it already where the code ask the user for number already, its a menu type code and one of the choices is the user enters a new number to have it replace the old number they inputted at the beginning. And right now the code is just reading the number, doing the SOP and returning back to option menu. – Tav Jan 27 '16 at 03:28
0

Java is pass by reference. You cannot modify the value of a primitive type (such as an integer) by passing it into a function, because the value of that variable is passed into the function, not a pointer to it. Whatever you change in the function only happens inside the function. You can fix your problem in one of two ways:

Solution 1: Modify your function to return a value and assign the variable you need to change as the result of the function call. The function will look like this:

private static void newNumber()
{         
     int userInput;
     // get user input, error check
     return userInput;
}

And the call will look like this:

myInt = newNumber();

Notice you do not need to pass in the Scanner or an int as you can put that all in the newNumber function (unless you want to reuse a Scanner object you created earlier).

Solution 2: Encapsulate the int in a class. This is probably unnecessary and over-complicated for this problem, but if you make a class with the int inside it, you can create an object with your int inside it, pass a pointer to that object into a function, and use that pointer as it will still point to the object you created earlier.

Community
  • 1
  • 1
intcreator
  • 4,206
  • 4
  • 21
  • 39