2

I have a java program that does not return the correct answer and I cannot figure out why. here's the code:

public class hello {
    public static void main(String[] args) {
        int a =5;
        doubleNumbers(a);
        System.out.println(" 5 doubled is:"+a);
    }

    private static void doubleNumbers(int a) {
        a = 5*2;
    }
}

It is my first java program after helloWorld.

5
  • Make sure you're following along in your book; it should discuss return values etc. Commented May 30, 2012 at 12:12
  • Ok I think I understand now. It works correctly with my new code. What should I do with this question now? delete it? and how do they decide who gets points for answering? sorry for the noob questions, this is my first time here. Commented May 30, 2012 at 12:22
  • It's a great question, actually. You might consider to change the title, so it's easier to find for someone else. Other than that, you should read the stackoverflow.com/faq as proposed before. Commented May 30, 2012 at 12:26
  • Does the new title sound ok? and I am a little confused on how you get your question upvoted to be seen by more people. it sounds hard to do.. for example: there are seven answers but only 1 upvote? Commented May 30, 2012 at 12:32
  • I have now changed the title (not to include the answer, but the symptoms) and reindented the code (Because that makes it more likely to be read, by someone), but it has to be reviewed by a moderator first. Commented May 30, 2012 at 12:47

8 Answers 8

6

Java is pass-by-value, which means that the variables passed to a function are not changed outside of it.

As this is homework I will not show you the solution, but just tell you to return the value of the calculation.

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

2 Comments

All primitives are pass-by-value, but all other objects are pass-by-reference.
@twall: Wrong. Everything in Java is passed by value, but the references are passed by value; the objects are never passed at all. There's a difference.
3

you're not returning anything from your method

change it to

 private static int doubleNumbers(int a) { 
return a * 2; 
 } 

Comments

1

There are two problems with your code. First you should change the doubleNumbers method to return something, next you should change the print statement to where it is printing the returned value.

for example(in pseudo code, so you have to think about it!):

method doCalculation{
....
return calculated answer
}

main{
....
Print(doCalculation)
}

Comments

1

I would change the method doubleNumbers to return the result of the calculation, so it would look something like this:

private static void doubleNumbers(int a) {
    return a*2;
}

And then change the lines in the main method:

int a = 5;
a = doubleNumbers(a);

Also, doubleNumbers would only ever return 10 in the original implementation. You need to use the a variable you passed in, as the above code shows.

Comments

0

primitive types in Java passed by value not by reference. You need Object-type to pass it by reference try this:

 private static void doubleNumbers(Integer a) {
a = a*2;
 }

Comments

0

There are 2 obvious problems:

  1. You do not assign the return value of doubleNumbers() to any variable.
  2. doubleNumbers() does not return a value (replace voidwith int)

Comments

0
public class hello {


     public static void main(String[] args) 
     {
       int a =5;
       a = doubleNumbers(a);

       System.out.println(" 5 doubled is:"+a);

     }

     private static int doubleNumbers(int a) {
       return a*2;
     }

}

Each variable has a context, by default limited to the function which uses it.

Your a in the doubleNumbers() function isn't the same as the first one.

You need to return the result, and assign it to your original a variable

Comments

0

A better way will be to change double Numbers() method from void to int so it can be returnable and also instead of printing only a, rather print the method as now it will be doubling the a. I've also added Scanner so the program don't just double 5 but any number you enter.

public class Hello {
  public static void main(String[] args) {
    Scanner myScn = new Scanner(System.in);
    System.out.println("Please enter a number: ");
    int a = myScn.nextInt();
    //doubleNumbers(a);
    System.out.println(a+ " doubled is:"+doubleNumbers(a));

  }

  private static int doubleNumbers(int a) {
    int x = a*2;
        return x;
  }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.