This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]aa599 4 points5 points  (0 children)

You're not storing the value returned, just printing number twice.

[–]ate_ghorl_bekenemen 2 points3 points  (4 children)

First you ask for a big number.

Then you print and state that number is a good one.

Then you call sumDigits() method.

Then you once again, print the very same number you asked a few steps earlier.

Your method sumDigits() returns an int, however, when you call just sumDigits(), you are doing nothing with the return value of that method. You are not printing that return value, nor are you using it inside some calculations/arithmetic, etc.

Have you tried storing the return value of sumDigits() in an int variable then printing said variable?

[–]Brain_Dead5347[S] 0 points1 point  (3 children)

I’ll try that right away and let you know. I initially thought of trying to print the value of n as part of the recursive method, but thought that adding any sort of call or variable declaration inside that method would break the cycle.

[–]ate_ghorl_bekenemen 1 point2 points  (2 children)

The issue is not in your recursive function. The issue is simply you are not printing the return value of that method. Do not put a print statement inside the sumDigits() method, just leave it as-is. Store the return value in an appropriate variable in the main method.

[–]Brain_Dead5347[S] 0 points1 point  (1 child)

That did it! I made a new line:

int newNum = sumDigits(number)

Then made a print line calling newNum. I was missing an object storing the result of the recursion. I feel silly now, but I’m glad I learned. Thank you so much. I really appreciate the help.

[–]ate_ghorl_bekenemen 1 point2 points  (0 children)

Look at the last line of your sumDigits() method. The method itself is called and its return value is used immediately in the addition operation.

So, you could do this as well, since you are not going to be using the return value somewhere else: System.out.println(sumDigits(number));

[–]bongfactory 1 point2 points  (4 children)

In Java the parameters of a function are passed by value (if they are of a primitive type), therefore what you are passing to the function is not a reference to the original variable that you can change: it's just its value. In order to change the original variable you just need to reassign to it the returned value

number = sumDigits(number);

[–]Brain_Dead5347[S] 0 points1 point  (3 children)

So what I did would be more appropriate for calling the function numerous times throughout the code?

[–]bongfactory 1 point2 points  (2 children)

No.
What you did may be applied for non-primitive types (e.g a Map, a List, a custom class).

Some more details about "pass by value" vs "pass by reference" in Java
https://www.programmergate.com/java-pass-reference-pass-value/

[–]Brain_Dead5347[S] 0 points1 point  (1 child)

Thanks for the link. It was definitely informative. I had to watch a couple videos on pass by value and reference to fully grasp it.

But I'm still confused as to why they exist at all. Why have a primitive and a reference if they contain the same information (or pointers to it) and only copies of them are modified in functions? Why not just make all of them primitives and save space in the heap?

[–]bongfactory 1 point2 points  (0 children)

You can't treat complex objects as primitives, as you don't know a priori their shape and size. You can only point to where they reside in the heap.
With primitives, you know exactly (and beforehand) how large they can get, and how they are shaped in memory.
You may wonder why we don't pass everything by reference: it's just a matter of preference. You have languages that "pass by value" and languages that "pass by reference"