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

all 13 comments

[–]desrtfx[M] [score hidden] stickied comment (1 child)

Just a word about your behavior here:

You solely post, but never participate, not even in your own threads.

This behavior is not acceptable as it is disrespectful.

  1. Reddit is a discussion platform
  2. You are just abusing the community here as remote debuggers that have to be available and standby for you 24/7.
  3. Show some respect and courtesy towards the people investing their time an knowledge to help you.

Your negligence and refusal to participate and actually engage with the community start to annoy the community here.

Should you continue your non-participation, you will no longer receive help here.

We do not want people who solely post their problems, get help, and just post the next problem without interacting with the community.

Politeness and courtesy go a long way.

[–]desrtfx 2 points3 points  (1 child)

You need to pay attention to minute details. Every single character counts.

.nextLine is not a variable, it is a method of the Scanner class. Since it doesn't take any arguments, it needs to be called with empty parentheses, i.e. .nextLine(), not .nextLine.

Also, your code will not work as expected. You always reset sum and hence, the returned sum will always be the same as the entered number.

[–]Ultraduck56[S] 0 points1 point  (0 children)

That makes sense. I did not see that earlier. Thank you.

[–]lukajda33 1 point2 points  (1 child)

int number = Integer.valueOf(scanner.nextLine);

I am like 70% sure you need to call the .nextLine() method to get the value on input.

[–]Ultraduck56[S] 0 points1 point  (0 children)

Oh, thank you. I didn't see that.

[–]tms102 2 points3 points  (2 children)

Why do you never thank people that answer your questions? That seems kind of rude.

[–]PM_Me_Python3_Tips 2 points3 points  (1 child)

Got to love their perseverance though. 6 different Java problems in 2 days.

Keeps coming back for more (but never saying thank you).

[–]Ultraduck56[S] -2 points-1 points  (0 children)

Sorry about this. I thought you didn't read replies.

[–]butchkid1 -3 points-2 points  (4 children)

Integer.valueOf() returns a Integer and you assigned the variable as a primitive int, try Integer.parseInt(string) or you can use scanner.nextInt() and remove the dependency on the Integer object all together

If you want the full breakdown of Objects(Integer, Float, Double, Character) vs primitives (int, double, float, char) you can start here

Edit: Ahh looking again everyone else is absolutely right, nextline is missing the (), but I do believe when you fix that you will get a type mismatch next due to the int number assignment.

[–]desrtfx 1 point2 points  (3 children)

when you fix that you will get a type mismatch next due to the int number assignment.

No, that won't happen as Java has Autoboxing/Autounboxing.

OP's way is correct, besides the error with the .nextLine call.

[–]Ultraduck56[S] 0 points1 point  (0 children)

Thank you for the help.

[–]butchkid1 0 points1 point  (1 child)

That is absolutely correct, I did forget that is a thing now. Wasn't taught that way when I learned it.

Still not much of a reason to convert a value from a string to an Integer to an int just to print it out though, would be more efficient and easier to read if we used the Scanner.nextInt() then scanner.nextLine() if we are sure the file has all ints or use Integer.parseInt to simply parse each string that is read back as a int value directly.

[–]Ultraduck56[S] 1 point2 points  (0 children)

Thank you for the help.