Question for you guys, what am I doing wrong here? I am able to enter the 5 digits in one input and I just want it to display the first character; just to test to see if it works. Afterwards I will be displaying all 5 characters like "1 2 3 4 5" spaced out between each digits...


Error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 50
at java.lang.String.charAt(Unknown Source)
at PracticeArithmeticOperators.main(PracticeArithmeticOperators.java:12)

import javax.swing.JOptionPane;

public class PracticeArithmeticOperators
{
	public static void main(String[] args)
	{
		String input;
		int input1;
		
		input = JOptionPane.showInputDialog(null, "Enter the five digits: ");
		input1 = Integer.parseInt(input);
		input.charAt(input.charAt(0));
	}
}

Recommended Answers

All 4 Replies

Is there a way to have one dialog, and have it display "12345" as "1 2 3 4 5 "

while using a for loop?

import javax.swing.JOptionPane;

public class PracticeArithmeticOperators
{
	public static void main(String[] args)
	{
		String input;
		int input1;
		
		input = JOptionPane.showInputDialog(null, "Enter the five digits: ");
		for ( int i = 0; i < 5; i++ )
		{
			input.charAt(i);
			JOptionPane.showMessageDialog(null, input.charAt(i));
		}
	}
}

Or...

import javax.swing.JOptionPane;

public class PracticeArithmeticOperators
{
	public static void main(String[] args)
	{
		String input;
		
		input = JOptionPane.showInputDialog(null, "Enter the five digits: ");
			input.charAt(0);
			input.charAt(1);
			input.charAt(2);
			input.charAt(3);
			input.charAt(4);
			
			JOptionPane.showMessageDialog(null, input.charAt(0), input.charAt(1));
	}
}

Anyone know how to get the additional arguments to work without getting any errors? I can get this to work without displaying the GUI. I can write it in console with a for loop and + "" with no problem but I would like to know how to do this.. any help at all?

In your 2nd code, you did it correctly to use a loop, but there are a couple things you need to do with it.

1)You are hard-coded and expect that user will always enter a 5 digit number. As a result, whenever a user enters any string length less than 5, your program will throw ArrayOutOfBoundException because it attempts to access non-existing index. What you may need to do is to compute the maximum length before you enter the loop.

input = JOptionPane.showInputDialog(null, "Enter the five digits: ");
  // if the input length is greater than 5, max is 5; otherwise, the max is the input length
  int maxLen = input.length()>5 ? 5 : input.length();
  for ( int i = 0; i<maxLen; i++ ) {
    ...
  }

2)When you want to display the whole new string, you need to compose the string before you display. What you are doing now is to display each character in the loop. Therefore, you need another string variable to be display, and move the showMessageDialog() out to be below the loop.

String str="";  // empty one
  for... {
    ... // concatenate the string with each character and a white space
  }
  JOptionPane.showMessageDialog(null, str);  // display the composed string
Member Avatar for hfx642

Since "input" is a string, just display the string 1 character at a time.
(ie. Don't bother converting into an integer.)
A for loop using the substring() method will work great for this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.