Solved

ArrayIndexOutOfBoundException

Posted on 2016-11-06
9
80 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 50

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 50

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 95
split53 challenge 7 96
HSSFWorkbook cannot be resolved error 10 68
why cannot we forward request once the response is committed 2 42
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

776 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