+ 1

Creating a method that can sum digits of an integer

Hello, I have been trying to solve this common problem in Java courses: Sum the digits in an integer Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n) For example, sumDigits(234) returns 9 (2 + 3 + 4). (Hint: Use the % opera- tor to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 ( = 4). To remove 4 from 234, use 234 / 10 (= 23). Use a loop to repeatedly extract and remove the digit until all the digits are extracted. Write a test program that prompts the user to enter an integer and displays the sum of all its digits. This is the code that I have: public class sumDigitMetho{ public static void main(String[] args) { System.out.println(sumDigitMetho(234)); } public static int sumDigitMetho(long n){ long result = 0; for(long x = n; x <=n ;x++){ x=x%10/10; result += x; return x; } } } Every time I compile I get the response: "incompatible types: possible lossy conversion from long to int. return x; What am I doing wrong? I know I do not have the user input yet Thanks, Sei

8th Jun 2022, 5:27 AM
Sei
10 Respuestas
+ 3
Your sumDigitMethof is as integer declared but return a long. It must be long too.
8th Jun 2022, 6:02 AM
JaScript
JaScript - avatar
+ 3
Also you need to return outside the loop
8th Jun 2022, 7:47 AM
A͢J
A͢J - avatar
+ 3
Sei So far you have many problem. 1 - take parameter as a int because int is enough to handle such a long value 234 2 - x = n; x <= n; causes infinite loop because 234 <= 234 will be always true 3 - change result as int 4 - x = x % 10 / 10 is wrong because 234 % 10 / 10 = 4 / 10 = 0 So x would be 0 after 1st iteration and result would be 0 5 - return should be outside the loop 6 - for loop works for fix length of iteration as we don't know so this problem can be easily solve using while loop public static int sumDigitMetho(int n) { int result = 0; while (n > 0) { //x=x%10/10; result += n % 10; // return x; n = n / 10; } return result; }
8th Jun 2022, 2:19 PM
A͢J
A͢J - avatar
+ 3
Sei , This code shows one solutions way: https://code.sololearn.com/cuA27LS8xH8e/?ref=app
8th Jun 2022, 5:36 PM
JaScript
JaScript - avatar
+ 2
An "easy" solution is to change all of your long values to ints. Are the numbers going to be so large that you'll need a long?
8th Jun 2022, 5:36 AM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Sei , just check on how big the numbers can reasonably get. If you need long, go for long. I really doubt a sum of digits would overflow an int. But the function must return the same type it is declared. You declared an int function and tried to return a long. Make it equal.
9th Jun 2022, 4:38 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Side note: in the for loop, you're changing the loop variable (x) both in the loop declaration and inside it. This smells trouble.
8th Jun 2022, 11:24 AM
Emerson Prado
Emerson Prado - avatar
+ 1
You guys are awesome! Thank you all for your help. I'm not in a formal class at the moment, just myself using a textbook. But I do have another question: Some of the posts have suggested that I use int instead of long why is this? I know in the example I substituted 234 for the formal parameters but it could have been a much larger number. Thanks Again
8th Jun 2022, 4:01 PM
Sei
+ 1
JaScript, Thanks!
8th Jun 2022, 6:04 PM
Sei
+ 1
You are welcome Sei!
8th Jun 2022, 6:26 PM
JaScript
JaScript - avatar