Eogcloud
Eogcloud

Reputation: 1365

Looped String creation

having a small problem.

I'm trying to create a loop that will create an amount of strings equal to an user-input int value, I've been scratching my head for about half an hour but can't really work out how I'd do this.

int x =Integer.parseInt(JOptionPane.showInputDialog("How many String do you want       to     add to the Queues?"));

for (int i=0; i<x; i++)
{
 String string(i) = (char)(i+65);  
}

I know the inner part of the loop is incorrect, but I wrote it to express what I'm trying to achieve, how do I get the code to recognize, "String" as part of the name string, but i as a char to be added to that namestring for that variable? The hope is to allow someone to enter 5 for example and then have it create

string0 = a, string1 = b, string2 = c etc.

Can anyone help me with this?

Upvotes: 0

Views: 94

Answers (4)

Dan W
Dan W

Reputation: 5782

Try something along the lines of:

someString = someString + i + " = " + (char)(i+65);

This should give you what you want, but if you want it to be a little better, I would look into using a String Builder.

Upvotes: 1

Adrian
Adrian

Reputation: 5681

char crtChar = 'a';
for i=0..x {
  stringArray[i] = ""+crtChar++;
}

This creates an array of strings, each string having a character. First string is "a", next "b" etc. Depending on how many string you are making, you might get non displayable ASCII chars in the string.

Upvotes: 1

Guillaume Polet
Guillaume Polet

Reputation: 47608

Either you use a char array (char[]) initialized to a given known size. Else, use StringBuilder and append all chars to it. When you are done call the toString() method and it will give you the resulting string.

Upvotes: 1

MByD
MByD

Reputation: 137272

I don't want to ruin the homework for you, so here are some hints:

  1. Use an array, it should have the size that returned from the dialog.
  2. Assign the values into the cells of the array.

Upvotes: 2

Related Questions