?
Solved

ArrayIndexOutOfBoundException

Posted on 2016-11-06
9
Medium Priority
?
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 2000 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 53

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 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