How to find the maximum positive integer (whole number) using String concatenation in Java?
How to find the maximum positive integer (whole number) using String concatenation in Java?
This article is part of a tutorial.
Tutorial Index page - Java Data Modelling
Introduction
This article is about designing and writing a Java program that will take the number of digits as its input and will find the maximum possible whole number with that number of digits. For example,
| No of digits (Input) | Maximum Integer (Output) |
|---|---|
| 1 | 9 |
| 5 | 99999 |
| 10 | 9999999999 |
| 50 | 99999999999999999999999999999999999999999999999999 |
Techniques
I am going to solve this problem by using two different techniques but ultimately produce the one desired output. These techniques are:
- Using String concatenation
- Using Mathematical calculation
This article focusses on the first technique - Using String concatenation. Check out the next article that focuses on the second method - Using Mathematical calculation.
Using String concatenation
This is the easy and straight forward technique of the two which also requires the least mathematical background. In this method all we are going to do is to identify the pattern of our desired output and try to come up with a algorithm that will produce this pattern based on a dynamic/runtime input.
So, looking at the table above can you spot any patterns? These are the ones I have noticed.
- The output is always and only made up of the number 9 or the character '9'.
- The number of 9's in the output is eactly equal to the input number which is the number of digits.
If you are happly with these patterns then we shall continue to formulate an algorithm based on these patterns (rules).
Algorithm
This algorithm will find the maximum Integer for a given number of digits using String concatenation and is based on the two rules we have identified in the above section.
Step 1: declare and initialise a String variable - maxNumberString
Step 2: Loop through Step 3, x times where x = number of digits (input)
Step 3: Append / Concatenate the character '9' to the String variable maxNumberString
Step 4: Convert the String variable - maxNumberString into a number which is the maximum integer (output) for the given number of digits (inout).
Step 5: Have a KitKat!
Implementation
Before I show the code, I would like to highlight three design points that might be of interest to you.
- Since the String class is immutable and so memory expensive, the StringBuilder class is used for the concatenation.
- The looping is implemented as an enhanced for loop.
- When the number of digits (input) is less than zero which is considered as an error condition -1 is returned. You can throw an exception in this case instead.
With this said, go to the next page for the complete Java program that produces the maximum integer for a given number of digits using String concatenation along with its console output.
Using Mathematical calculation
This method is a bit more complicated but lots more fun as we are going to refresh our mathematics knowledge to come up with an algorithm. Please check out the next article to read more about this technique and to find a fully working Java program that uses this technique.
Feedback or Questions?
We welcome feedback and questions and will try our best to attend to it as quickly as possible!
Please note that you would have to register before you can post in our forums and this is purely to guard us from the spam-bots. Be assured that we do not send spam mails and our website registration only takes minutes.
