How to find the maximum positive integer (whole number) using String concatenation in Java ?

A Java  program to find the maximum possible Integer with the given number of digits. For example this program will find the number 9999 for 4 digits and 999999 for 6 digits. Think this is simple? If so, can you tell me two totally different ways of doing this? One way of doing this is using String concatenation...

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:

  1. Using String concatenation
  2. 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.

  1. Since the String class is immutable and so memory expensive, the StringBuilder class is used for the concatenation.
  2. The looping is implemented as an enhanced for loop.
  3. 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.

 

Comments

bnatey


توبيكات



توبيكات توبيكات



توبيكات اسلاميه



توبيكات اسلاميه



توبيكات حزينه


توبيكات حب


توبيكات رومانسيه



توبيكات اسماء



توبيكات اغاني



توبيكات منوعه



توبيكات مضحكه




توبيكات عتاب

توبيكات عتاب
توبيكات ساخره
توبيكات شعريه

توبيكات اجنبيه

توبيكات جديده
توبيكات بنات
توبيكات 2010
توبيكات اعياد
توبيكات عيد الاضحى
توبيكات مناسبات
توبيكات اعياد ميلاد
توبيكات رأس السنه

توبيكات كريسماس

توبيكات توبيكات
توبيكات كرة قدم
توبيكات كأس العالم
توبيكات مصر
توبيكات إلا رسول الله
توبيكات الاهلي
توبيكات الزمالك

توبيكات

توبيكات

توبيكات اسلاميه

توبيكات حزينه

توبيكات حب

توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني

توبيكات منوعه

توبيكات مضحكه
توبيكات عتاب

توبيكات ساخره

توبيكات شعريه
توبيكات اجنبيه

توبيكات نكت

توبيكات جوال

توبيكات سعوديه

توبيكات شخصيات فنيه
توبيكات ليلة العمر
توبيكات

توبيكات


توبيكات فلاش


توبيكات روعه


توبيكات رائعه


منتديات

دردشة


العاب


العاب بنات

شات



توبيكات مصريه



توبيكات قطريه



دليل مواقع



تحميل
صور



مركز تحميل



برامج

كأس العالم
كأس العالم 2010

العاب
العاب بنات
منتدي
فيديو بنات
برامج
مركز رفع صور
شات
دليل المواقع العربيه
العاب
العاب
دليل مواقع
توبيكات
كتب
مقالات
صور
منتدى أجنبي
منتديات

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

How to find the maximum positive integer (whole number) using String concatenation in Java ? | Our website now yours! - Currenlty Java focussed.

How to find the maximum positive integer (whole number) using String concatenation in Java ?

A Java  program to find the maximum possible Integer with the given number of digits. For example this program will find the number 9999 for 4 digits and 999999 for 6 digits. Think this is simple? If so, can you tell me two totally different ways of doing this? One way of doing this is using String concatenation...

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:

  1. Using String concatenation
  2. 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.

  1. Since the String class is immutable and so memory expensive, the StringBuilder class is used for the concatenation.
  2. The looping is implemented as an enhanced for loop.
  3. 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.

 

Comments

bnatey


توبيكات



توبيكات توبيكات



توبيكات اسلاميه



توبيكات اسلاميه



توبيكات حزينه


توبيكات حب


توبيكات رومانسيه



توبيكات اسماء



توبيكات اغاني



توبيكات منوعه



توبيكات مضحكه




توبيكات عتاب

توبيكات عتاب
توبيكات ساخره
توبيكات شعريه

توبيكات اجنبيه

توبيكات جديده
توبيكات بنات
توبيكات 2010
توبيكات اعياد
توبيكات عيد الاضحى
توبيكات مناسبات
توبيكات اعياد ميلاد
توبيكات رأس السنه

توبيكات كريسماس

توبيكات توبيكات
توبيكات كرة قدم
توبيكات كأس العالم
توبيكات مصر
توبيكات إلا رسول الله
توبيكات الاهلي
توبيكات الزمالك

توبيكات

توبيكات

توبيكات اسلاميه

توبيكات حزينه

توبيكات حب

توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني

توبيكات منوعه

توبيكات مضحكه
توبيكات عتاب

توبيكات ساخره

توبيكات شعريه
توبيكات اجنبيه

توبيكات نكت

توبيكات جوال

توبيكات سعوديه

توبيكات شخصيات فنيه
توبيكات ليلة العمر
توبيكات

توبيكات


توبيكات فلاش


توبيكات روعه


توبيكات رائعه


منتديات

دردشة


العاب


العاب بنات

شات



توبيكات مصريه



توبيكات قطريه



دليل مواقع



تحميل
صور



مركز تحميل



برامج

كأس العالم
كأس العالم 2010

العاب
العاب بنات
منتدي
فيديو بنات
برامج
مركز رفع صور
شات
دليل المواقع العربيه
العاب
العاب
دليل مواقع
توبيكات
كتب
مقالات
صور
منتدى أجنبي
منتديات

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options