0

I'm trying to understand passing arguments and I've run into an issue.

Let's say I have the following code:

I pass the value 60 to the method someMethod. From there I want that value to be modified with a user input. Once it gets modified, I want it pass that value to another method called getValue. The getValue method then returns that value.

Here's the problem:

1) If I were to call someMethod, it would also call for the user input again which I don't want.

2) What is the correct way of printing out the value of the getValue method as doing:

New.getValue(int returnedValue); calls the error "unexpected type, required value, found class"

public class New {

    Scanner sc = new Scanner(System.in)

private int static num1 = 60;
someMethod(num1);

    public static int someMethod(int myValue)
    {
        //modify the integer
        System.out.println("Enter in the value to modify");
        myValue = sc.nextInt();
        //output the value to a getter method
        getValue(myValue);
        return myValue;
    }

    public static int getValue(int returnedValue)
    {
        return returnedValue;
    }

}

As always, if you need clarification, ask! Thanks.

Brian
  • 115
  • 2
  • 3
  • 15
  • 1
    You use this statement -- `getValue(myValue);` -- but it returns a value which you do not assign to anything, so the value is "thrown on the floor". – Hot Licks Oct 12 '12 at 18:14
  • Please take a look at this answer for more clarification: http://stackoverflow.com/a/9404727/597657 – Eng.Fouad Oct 12 '12 at 18:15

2 Answers2

5

someMethod(num1); should be wrapped inside your main method or some other method.

Second: You don't have sc variable defined. So, your code fails there also.

Third: You didn't define type of num1. It should be private static int num1 - 60;

You need to pass real value instead of definition while invoking getValue() method.

Example: System.out.println(getValue(50));

kosa
  • 65,990
  • 13
  • 130
  • 167
  • I still get the same error when I wrap the passing argument of someMethod inside the someMethod method. When I call it from another static method I say System.out.println(getValue(int returnedValue)); and I get the expected value, got class error. – Brian Oct 12 '12 at 18:19
  • You should call something like System.out.println(getValue(50)) You need to pass real value here, not definition – kosa Oct 12 '12 at 18:21
  • If I pass in a real value, how will what the user entered get displayed? – Brian Oct 12 '12 at 18:39
  • What ever user enters that will be real value. – kosa Oct 12 '12 at 18:40
  • Now it makes sense. The value was getting returned without having a place to get stored. – Brian Oct 12 '12 at 20:27
0

Try this out...

I have also added the modification Part....

public class New {

private static int num1;


    public static int someMethod()
    {

        System.out.println("Enter in the value to modify");

        Scanner s = new Scanner(System.in);

        int temp = s.nextInt();

        return getValue(num1+temp); // Modification Done
    }

    public static int getValue(int returnedValue)
    {
        return returnedValue;
    }

    public static void main(String[] args){

        Scanner scan = new Scanner(System.in);

        New.num1 = scan.nextInt();
        int i = someMethod();
        System.out.println(i);



    }

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75