Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I get this code to run properly? On line 18 I changed it to \" while (lo

ID: 3595411 • Letter: H

Question

 How can I get this code to run properly? On line 18 I changed it to "  while (loopCount >= 1 || 1 <= digitsLoop) { " which got rid of one error but I still get no result after I enter an integer. Thanks    import java.util.*; 
   public class AddDigits{ 
    public static int sumDigits(long lg) {  // method parses and sums the digits of any given lg 
                 int input = (int)lg;                  // convert to int type in order to use % 
    
                // find the length of the number by using String.length() 
                 String inputCheck = "" + input; 
                 int digitsLoop = inputCheck.length(); 
                  
                 int loopCount = 0, digitsTotal = 0; 
    
                 while (loopCount += 1 <= digitsLoop) {  // loop for every digit 
                       digitsTotal += input % 10;         // use % to collect the ones position 
                       input = input / 10;                // divide the input by 10 to trim off the ones 
                 } // end while 
                                  return digitsTotal; 
                    
    } // end sumDigits 
    
    
     public static void main(String [] args){ 
            // Initialize the input object 
            Scanner input = new Scanner(System.in); 
            System.out.print("Enter an integer:"); 
               int userInteger = input.nextInt(); 
    
            // display the sum of the integers digits 
            System.out.println("The sum of the digits in " + userInteger + " = " + sumDigits(userInteger)); 
      } 
 } 

Explanation / Answer

AddDigits.java

import java.util.Scanner;

public class AddDigits {
public static long sumDigits(long lg) { // method parses and sums the digits of any given lg

long remainder, sum = 0;

while (lg != 0) {
remainder = lg % 10;
sum = sum + remainder;
lg = lg / 10;
}
return sum;

} // end sumDigits


public static void main(String[] args) {
// Initialize the input object
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer:");

long userInteger = input.nextLong();


// display the sum of the integers digits
System.out.println("The sum of the digits in " + userInteger + " = " + sumDigits(userInteger));
}
}

________________

Output:

Enter an integer:123456789
The sum of the digits in 123456789 = 45

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote