Solved

ArrayIndexOutOfBoundException

Posted on 2016-11-06
9
97 Views
Last Modified: 2016-11-29
Hi,

import java.util.Arrays;
import java.util.Scanner;

public class TestClass {
	public static void main(String[] args) {
		// getting input
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please enter number:");
		// reading input number as string
		String input = scanner.nextLine();
		// creating array of integers with length same as input
		int integerArray[] = new int[input.length()];
		// forming string array by splitting above string array with delimitter
		// ""
		String inputStringArray[] = input.split("");// string array {2,3,4}
		//
		for (int i = 0; i < inputStringArray.length; i++) {
			// converting to integer array{1,2,3}
			integerArray[i] = Integer.parseInt(inputStringArray[i]);
		}
		// sort the integerArray
		Arrays.sort(integerArray);
		// loop integerArray till its length

		
	int len = integerArray.length;//same as space
		int sp=len;
		for (int i = 0; i <= len; i++) {
			// Printing i spaces at the beginning of each row

			for (int j = 0; j <= sp; j++) {
				System.out.print(" ");
			}
			// not sure what we are doing in below inner loop?? please advise
			for (int j = 0; j < i+1 ; j++) {
				System.out.print(integerArray[i]);
			}
			System.out.println();
			sp--;
		}
	}
	// TODO Auto-generated method stub
	/*int i, j, sp = 5;
	// int j;
	for (i = 1; i <= 4; i++) 
	{
		for (j = 0; j <= sp; j++) 
		{
			System.out.print(" ");
		}
		for (j = 1; j <= i; j++) 
		{
			System.out.print(i + " ");
		}
		System.out.println();
		sp--;
	}*/

}

Open in new window


Please enter number:
123
    1
   22
  333
 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
      at TestClass.main(TestClass.java:36)


when i run above example why i am getting ArrayIndexOutOfBoundException.

i want to see below output when i pass 123 as console input

  1
 22
333

please advise
0
Comment
Question by:gudii9
  • 6
  • 2
9 Comments
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41876561
quick try...
try change:
for (int i = 0; i <= len; i++) {

Open in new window

to:
for (int i = 0; i < len; i++) {

Open in new window

and see if that work?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41876564
currently numbers are like right aligned as attached.
    1
  22
333
 
i want more like center aligne
formatOutpu.png
0
 
LVL 7

Author Comment

by:gudii9
ID: 41876570
that worked. how to center align for a given number? it could be 3 digit or 5 digit etc?
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 7

Author Comment

by:gudii9
ID: 41876571
quick try...
try change:
for (int i = 0; i <= len; i++) {

Select all
 
Open in new window
to:
for (int i = 0; i < len; i++) {

Select all
 
Open in new window
and see if that work?
how did you find that is issue out of all 4 different for loops there?
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41876590
how did you find that is issue out of all 4 different for loops there?
can't tell you how.. but based on some experience i guess, cheers.

how to center align for a given number? it could be 3 digit or 5 digit etc?
check this out:

How To Create Pyramid Of Numbers In Java?
http://javaconceptoftheday.com/how-to-create-pyramid-of-numbers-in-java/
0
 
LVL 7

Author Comment

by:gudii9
ID: 41877043
i already checked that link and if you see bottom most commented code from my original post it is from same site. I am not able to tweak that code to suite my requirement. please advise
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41877374
You shouldn't really be opening a new question, since it's really part of your original one
0
 
LVL 7

Author Comment

by:gudii9
ID: 41879310
any solution on this?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41879311
i am not able to figure out myself the space issue
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 69
maven module vs maven project 3 68
Java: The Public Class Main 4 44
Website checklist for browser compatibility? 2 37
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question