Solved

ArrayIndexOutOfBoundException

Posted on 2016-11-06
9
91 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 50
maven module vs maven project 3 46
servlet and mdb, jms error 1 29
Crystal Reports Licensing Questions 4 34
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

791 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