Frage

I'm a struggling newbie trying to finish a program assignment and am stuck with my output repeating itself with the digits of an integer that should be together on one line, but spaced (Correct Ex:The digits of 1234 are: 1 2 3 4). Here is an example of the incorrect output and below that is my program. Any guidance would be greatly appreciated.

Enter an integer: 1234

The digits of 1234 are: 1

The digits of 1234 are: 2

The digits of 1234 are: 3

The digits of 1234 are: 4

The sum of the digits = 10

My program:

import java.util.*;                                                              

public class IntegertoIndividualtoSum                                                                                                 
{
   public static void main(String[] args)                                                         
   {

      long integer, digit, sum = 0;
      char digitAt;
      String Stringnum;

      Scanner keyboard = new Scanner(System.in);

      System.out.print("Enter an integer: ");
      integer = keyboard.nextLong();

      Stringnum = String.valueOf(Math.abs(integer));

      for (int count = 0; count < Stringnum.length(); count++)
      {
         digitAt = Stringnum.charAt(count);

         digit = Character.getNumericValue(digitAt);

         sum += digit;

         System.out.println("The digits of " + integer + " are: " + digit); 
      }
         System.out.println("The sum of the digits = " + sum);      
    }   
}
War es hilfreich?

Lösung

You are using the method System.out.println, which will print a new-line at the end. You can use System.out.print, which won't do that.

Also, remember that you get the part "The digits of ... are: ..." many times because it is inside the for loop.

System.out.print("The digits of " + integer + " are: ");

for (int count = 0; count < Stringnum.length(); count++) {
    // ...
    System.out.print(digit + " "); // add space
}

System.out.println("\nThe sum of the digits = " + sum); // note new-line character '\n'

Note:

  • You can add a new-line character inside the String you want to print with "\n".

Andere Tipps

Part of your problem is that you're printing the entire line for each digit. You're looping across the length of the string, and printing your output on every iteration.

Your second problem is that System.out.println automatically appends a newline character to the end of the argument you pass in. If you use System.out.print, you'll get your output on one line as you expect it.

That should be enough to get you started.

If you do something inside a loop, it will happen each time you go through the loop.

Think about what you want to do once before entering the loop, what you want to do for each iteration of the loop, and once after completing the loop.

Also, be aware that you can call print(...) rather than println(...) to print without going to a new line at the end, and you can call println() with no arguments to go to a new line without printing anything.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top