Solved

ArrayIndexOutOfBoundException

Posted on 2016-11-06
9
30 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 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
that worked. how to center align for a given number? it could be 3 digit or 5 digit etc?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
any solution on this?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
i am not able to figure out myself the space issue
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
noX challenge 17 76
Modeling a class in java 5 31
Systems talking to each other 5 107
solarwind tftp server 2 30
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now