简体   繁体   中英

How to split up integers using printf in Java

Hello everyone I was having some issue splitting up a user input number using printf (I do have to use printf). My problem is that when I put in say the number 12345 it will print the integers on five separate lines, and also has them in the reverse order. So it would look something like this when I put in the integer 12345:

5

4

3

2

1

But without the spaces (I need those as well). I want it to print like this: 1 2 3 4 5. Here is the code I have so far:

public static void main(String[]args){
    Scanner input = new Scanner(System.in);

    int one;

    System.out.print("Enter the five digit integer you would like to be split up:");
    one = input.nextInt();

    while (one > 0){
        System.out.printf("%d%n", one % 10);
        one = one /10;
}
}

First, in order to avoid printing on separate lines, you should avoid using the %n formatting character in your printf() .

Now, how do you print the digits in the correct order? Well, since you are limited to five-digit numbers, you can do something like this:

    for ( int divisor = 10000; divisor >= 1; divisor /= 10 ) {
        System.out.printf( "%d ", n / divisor);
        n %= divisor;
    }
    System.out.printf( "%n" ); // Just to complete the line

( divisor /= 10 is shortcut for divisor = divisor / 10 , and n %= divisor is shortcut for n = n % divisor ).

So you start by dividing the number by 10000. This will give you the fifth digit from the right. Then you take the remainder and put it in n . This gives you just the remaining four digits. Then the loop will reduce your divisor to 1000, which will take the fourth digit from the right, and you keep doing that until you reach a divisor of 1.

Another approach that does not require knowing that the number is 5 digits long, but requires recursion is to write a method like so:

public static void printSplitNumber( int n ) {

    if ( n == 0 ) {
        return;
    }
    printSplitNumber( n / 10 );
    System.out.printf( "%d ", n % 10);
}

And from your main, call:

    printSplitNumber(n);
    System.out.printf("%n"); // Again, just completing the line.

This recursive method relies on the fact that you print the current digit only after all the rest of the number has been printed. So this causes it to print it to the right of the rest of the digits, giving you the effect you need.

Unless the assignment is to figure out how to split the digits numerically, I think that the simplest approach is to either use Scanner's nextLine() method to get a String, or convert your int to a String, and then split the characters of the String.

substring() is a little heavy - a lighter-weight way to do it is by inspecting character positions, like this:

  public void printDigits(String chars) {
    for(int i = 0; i < chars.length(); i++) {
         System.out.printf("%c ", chars.charAt(i));
    }
  }

This approach uses the substring method as opposed to mathematically manipulating the int value.

int one;

System.out.print("Enter the five digit integer you would like to be split up:");
one = input.nextInt();

String x = Integer.toString(one);
for(int i = 0; i < x.length() - 1; i++)
{ 
  // On last digit in number
  if(i + 1 == x.length())
  {
    System.out.printf("%s ", x.substring(x.length()));
  }
  else
  {
    System.out.printf("%s ", x.substring(i, i + 1));
   }
}

Simplified printf statemnts thanks to @Jerry101's comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM