0

I am looping to write data to an array of Strings. But what I want is that I create another loop where I will loop through the data of values as much as amount, but I would like to have some guidance on how to do so.

String[] s = new String[20];
String[] values = { "A", "B", "C", "D" };
final int amount = 2;

for (int i = 0; i < s.length; i++) {
    s[i] = String.format("%s%04d", values[0], i); //TODO create another loop?
}
System.out.println(Arrays.toString(s));

The preferred output should be:

A0000, A0001, B0002, B0003, C0004, ...

The actual output is:

A0000, A0001, A0002, A0003, A0004, ...
5
  • Please specify the the actual output, show the complete output you want. Commented Oct 6, 2015 at 9:44
  • @rakeb.void I have edited the original post. Commented Oct 6, 2015 at 9:45
  • Your "preferred" and "actual" outputs totally do not agree. Please reconcile this. Commented Oct 6, 2015 at 9:46
  • Still not clear, from where the I came into output IA20000? Why you used values[0] in String.format? What will be the purpose of "B", "C", "D" in values? Commented Oct 6, 2015 at 9:51
  • System.out.println(Arrays.toString(s)); would get you the desired output to some degree, but the code you've shared doesn't show what you put in the classes variable that you're using as output provider... Commented Oct 6, 2015 at 9:52

3 Answers 3

2

Problem : your looping is wrong.

try this:

 public static void main(String[] args) {

          int k=0;
          String[] s = new String[20];
            String[] values = { "A", "B", "C", "D" };
            final int amount = 2;
            for (int i = 0; i < values.length; i++){

                 for (int j = 0; j < amount; j++){ //data of values as much as amount
                  s[k++] = String.format("%s%04d", values[i], k); 
                 }
            }
            System.out.println(Arrays.toString(Arrays.copyOf(s,k)));
     }

output:

[A0001, A0002, B0003, B0004, C0005, C0006, D0007, D0008]
Sign up to request clarification or add additional context in comments.

Comments

1

You need to have 2 loops one to loop through the actual values array then another one is the amount (number of times).

Try this

    List<String> a = new ArrayList<String>();
    String[] values = { "A", "B", "C", "D" };
    final int amount = 2;
    int incrementVariable = 0;
    for (int i = 0; i < values.length; i++){
        for(int j = 0; j< amount; j++){
            a.add(String.format("%s%04d", values[i], incrementVariable)); 
            incrementVariable +=1;
        }
    }
    System.out.println(a);
}

Output:

[A0000, A0001, B0002, B0003, C0004, C0005, D0006, D0007]

Comments

0

I like while loops better :)

String[] s = new String[10];
String[] values = {"A", "B", "C", "D"};
final int amount = 2;

for (int i = 0; i < values.length; ++i) {
    int buffer = 0;
    while(buffer <= amount) {
        int index = i * amount + buffer;
        s[index] = values[i] + String.format("%04d", index);
        buffer++;
    }
}

System.out.println(Arrays.toString(s));

[A0000, A0001, B0002, B0003, C0004, C0005, D0006, D0007, D0008, null]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.