ok, I'm taking my first Java class and in my first week. I've searched but this is different from .Net so much that I have a headache just getting eclipse set up and how to navigate the environment.

That said, I have a program that would take me 5 minutes to do in Visual Studio that I can't figure out in Java.

Here is the code

public static void main(String[] args) {
		String inputNumber;
		String number1;
		String number2;
		String number3;
		String number4;
		String number5;
		inputNumber = JOptionPane.showInputDialog("Enter a five digit number ");				
		number1 = inputNumber.substring(0,1);
		number2 = inputNumber.substring(1,1);
		number3 = inputNumber.substring(2,1);
		number4 = inputNumber.substring(3,1);
		number5 = inputNumber.substring(4,1);
		JOptionPane.showMessageDialog(null, number1 + "     " + number2 + "     "+ number3 + "     " + number4 + "     "+ number5);


what I'm supposed to do is accept input of 5 numbers and then display them separated by five spaces. So if entered 05852 it would then display 0 5 8 5 2

I'm getting
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at practiceArithmeticOperators.PracticeArithmeticOperators.main(PracticeArithmeticOperators.java:29)"

9 Years
Discussion Span
Last Post by rapture

The second parameter is the ending index of the substring (exclusive), not the number of characters in the substring.

If you want single character substrings, you can do this:

number1 = inputNumber.substring(0,1);		
		number2 = inputNumber.substring(1,2);		
		number3 = inputNumber.substring(2,3);		
		number4 = inputNumber.substring(3,4);		
		number5 = inputNumber.substring(4);

The number of characters in the substring is the end index minus the start index.


The below is from the Java documentation (link above). See red below.


public String substring(int beginIndex,
int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.


"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"

beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
the specified substring.
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

Votes + Comments
Very helpful!

Well thanks,

I knew it was going to be super simple and was overlooking it - I had read that page and totally passed over what I needed there.

(*must stop making assumptions about code when brain is infected with Microsoft patterns . . .)

You wouldn't happen to have an antivirus for that would you?

In VB.Net if I had development and wantd lop it would be substring(5,3) for where to start and how many. I didn't register the difference.

Thanks again for the help . . .

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.