1

I need help with this basic program. My friend had given me most of this code but not the import statements. Towards the end where it says list.add(finalCombined.get(counter3)); , it gives me an error. The error I get is

Cannot find Symbol 
Symbol: variable list
location: class ArrayUtils

I'm very confused at this. I added which import statements I think I need. Thank you

import java.util.ArrayList;
import java.util.Random;
import java.util.Collections;
import java.lang.String;

public class ArrayUtils {
    public void randomStrings(ArrayList<String> arrayList,int nbrOfStrings,int vowelCnt, int strSize){
        ArrayList <String> finalCombined = new ArrayList();
        ArrayList <String> finalCombined = new ArrayList();
        for (int cnt = 0; cnt < nbrOfStrings; cnt++)
        {
            int cnt2 = 0;
            int gn, size, vowelUsed;
            Random n1 = new Random();
            size = 122-96;
            //char cs[] = new char[strSize];
            //String cs;
            //cs = "";
            ArrayList <Character> cs = new ArrayList();
            int counter = 0;
            vowelUsed = 0;

            while (counter < 1)
            {
                vowelUsed = n1.nextInt(vowelCnt + 1);
                if (vowelUsed == 0)
                {
                }
                else
                {
                    counter = 2;
                }

            }

            while (cnt2 < (strSize - vowelUsed))
            {
                gn = n1.nextInt(size) + 97;

                if (gn == 97 | gn == 101 | gn == 105 | gn == 111 | gn == 117 | gn == 121)
                {
                }

                else
                {
                    cs.add((char)gn);
                    //cs += ((char)gn + "");
                    cnt2 ++;
                }
            }
            while (cnt2 < strSize)
            {
                gn = n1.nextInt(size) + 97;
                if (gn == 97 | gn == 101 | gn == 105 | gn == 111 | gn == 117 | gn == 121)
                {
                    cs.add((char)gn);
                    //cs += ((char)gn + "");
                    cnt2 ++;
                }
            }
            //int check;
            //check = list.add(cs[cnt]);
            Collections.shuffle(cs);

            String combined;
            combined = "";
            //System.out.println(cs);

            int counter2 = 0;
            while (counter2 < strSize)
            {
                combined += cs.get(counter2); 
                counter2 ++;
            }
            finalCombined.add(combined);
            counter2 = 0;
            combined = "";

        } // end # strings    

        for (int counter3 = 0; counter3 < nbrOfStrings; counter3++)
        {
            list.add(finalCombined.get(counter3));
        }

    } // end method
}
QBrute
  • 4,405
  • 6
  • 34
  • 40
Anthony D
  • 19
  • 2

4 Answers4

2
list.add(finalCombined.get(counter3));

You haven't declared any variable named list, so this line can't pass compilation.

Perhaps you meant

arrayList.add(finalCombined.get(counter3));

since your randomStrings method has an argument called arrayList that you are not using at all.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

i think you need to change list to arrayList in line

list.add(finalCombined.get(counter3));

because list is not declared any where in your code and arrayList has not been used.

Prashant
  • 2,556
  • 2
  • 20
  • 26
0

You have not define variable called list. However you have declared the array list finalCombined with same name twice. One variable name should be change as list. Also you have to call to method through main method as below.

new ArrayUtils().randomStrings(new ArrayList<String>(), 6, 3, 3);

I'm just wandering what is the purpose of parameter arrayList in the method randomStrings. There is no use of this list with in the method randomStrings.

Your output looks like below:

[aeo]
[aeo, yii]
[aeo, yii, isu]
[aeo, yii, isu, aea]
[aeo, yii, isu, aea, aaa]
[aeo, yii, isu, aea, aaa, iek]
0

Always declare variables properly and wisely which helps your code to be more readable and understandable. It means you are actually reducing the stress on the compiler ;)

A small suggestion : use an IDE while coding which can help you in compile time errors like this. Moreover they are so friendly in suggesting solutions and optimising code as well..!

FiReTiTi
  • 5,597
  • 12
  • 30
  • 58