Find maximum positive integer using String concatenation - Java Full Code

A full working Java program with its console output for solving the problem - How to find the maximum positive integer (whole number) using String concatenation in Java ?

Find maximum positive integer using String concatenation - Java Full Code

Download Source Code:
/**
 * $Id: FindMaxIntegerForGivenDigitsUsingStrings.java 5 2009-11-25 14:58:29Z oneyour $
 * 
 * This is an accompanying program for the article
 * <a href="http://www.1your.com/drupal/findmaxintegerinjavausingstringconcatenation
" title="http://www.1your.com/drupal/findmaxintegerinjavausingstringconcatenation
">http://www.1your.com/drupal/findmaxintegerinjavausingstringconcatenation
</a> * 
 * Copyright (c) 2009 - 2010 <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a> nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
import java.math.BigInteger;
import java.text.ParseException;
 
/**
 * 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.
 */
public class FindMaxIntegerForGivenDigitsUsingStrings {
 
  private FindMaxIntegerForGivenDigitsUsingStrings()
 
  {
    // Private constructor as the only method provided by this class is
    // a static method.
  }
 
  /**
   * The main method which is the entry point of this program.
   */
  public static void main(String[] args)
  {
    for (int count = 1; count < 100; count++)
    {
      try
      {
        String maxNumberByString = findMaxPositiveIntegerByStringConcatenation(count);
 
        System.out.println("Digits - '" + count + "' -- Max Number - '" + maxNumberByString + "'");
      }
      catch (ParseException parseException)
      {
        System.err.println("Problems encountered.");
        parseException.printStackTrace();
      }
    }
  }
 
  /**
   *
   * @param digits
   *            The number of digits for which the maximum possible integer will be found.
   * @return
   *            The maximum number (integer) for the given number of digits.
   * @throws ParseException
   *            Problems while finding the maximum maximum number (integer) for the given number of digits.
   */
  private static String findMaxPositiveIntegerByStringConcatenation(int digits) throws ParseException
  {
    if (digits <= 0)
    {
      // Error condition
      return "-1";
    }
 
    StringBuilder maxNumberString = new StringBuilder();
    for (int count =0; count < digits; count++)
    {
      maxNumberString.append(9);
    }
 
    return new BigInteger((maxNumberString.toString())).toString();
  }
}

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.