patternjavaMinor
Separating digits in an Integer
Viewed 0 times
digitsseparatinginteger
Problem
I was reading a Java book and I came up with this kind of problem and I tried to solve it:
Problem: Write an application that inputs one number consisting of five digits from the user, separates the number into its individual
digits and prints the digits separated from one another by three
spaces each. For example,
Input:
Output:
Solution:
My questions are:
-
Did I do something wrong?
-
Is there any way to solve this problem more efficiently?
Problem: Write an application that inputs one number consisting of five digits from the user, separates the number into its individual
digits and prints the digits separated from one another by three
spaces each. For example,
Input:
42339Output:
4 2 3 3 9Solution:
package practice;
import java.io.IOException;
import static java.lang.System.in;
import java.util.InputMismatchException;
import java.util.Scanner;
public class practice {
public static void main(String[] args) throws IOException {
try(Scanner input = new Scanner(System.in)){
System.out.print("");
int a = input.nextInt();
char[] newString = Integer.toString(a).trim().toCharArray();
if (newString.length 5) {
System.out.println("Input should be exactly 5.");
return;
} else {
for (int i = 0; i < newString.length; i++) {
System.out.print(newString[i] + " ");
}
}
System.out.println();
} catch (InputMismatchException e){
System.out.println("Error: Invalid input.");
} finally {
in.close();
}
}
}My questions are:
-
Did I do something wrong?
-
Is there any way to solve this problem more efficiently?
Solution
Input and validation
Using a
You don't need to close the
Note that neither the
The error messages aren't quite right. The first message should read:
The validation is not quite strict enough: a negative four-digit integer will also be accepted.
Output
Strictly speaking, the output is not quite right, in that you also append three spaces after the fifth digit. On screen, the output will look the same, but if you pipe the output to a program that examines the bytes, you'll see the trailing spaces.
You don't need to call
Suggested solution
With Java 8, the simplest solution is to use
Using a
Scanner is a good idea, and using a try-with-resources block is the right thing to do.You don't need to close the
Scanner or the underlying System.in. The whole point of the try-with-resources block is that resources will be automatically closed for you.Note that neither the
Scanner(Readable) constructor nor Scanner.nextInt() will ever throw an IOException. The only exception you need to catch (but didn't) is NoSuchElementException. (Well, also InputMismatchException, but that is just a subclass of NoSuchElementException.) Your program will crash with a NoSuchElementException if you send it an EOF at the prompt.The error messages aren't quite right. The first message should read:
"Input should be exactly 5 digits." The second message is unhelpful: you should tell the user what would be considered valid input.The validation is not quite strict enough: a negative four-digit integer will also be accepted.
Output
Strictly speaking, the output is not quite right, in that you also append three spaces after the fifth digit. On screen, the output will look the same, but if you pipe the output to a program that examines the bytes, you'll see the trailing spaces.
You don't need to call
String.trim().newString is not a good name: it's not a string, and there is no oldString. If I had to pick a name, I'd call it digits.Suggested solution
With Java 8, the simplest solution is to use
String.join().public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
int n = input.nextInt();
if (!(1e5 <= n && n < 1e6)) {
System.out.println("Input should be exactly 5 digits.");
return;
}
System.out.println(String.join(" ", String.valueOf(n).split("")));
} catch (NoSuchElementException e) {
System.out.println("Error: No integer was given.");
}
}Code Snippets
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
int n = input.nextInt();
if (!(1e5 <= n && n < 1e6)) {
System.out.println("Input should be exactly 5 digits.");
return;
}
System.out.println(String.join(" ", String.valueOf(n).split("")));
} catch (NoSuchElementException e) {
System.out.println("Error: No integer was given.");
}
}Context
StackExchange Code Review Q#92332, answer score: 4
Revisions (0)
No revisions yet.