0

I have a set of code that, currently, uses a pre-set list of 30 manually inputed strings made up of numbers, which can be seen below:

    String[] elementsToAdd2 = { "100", "510", "170", "214", "268", "398",
            "235", "802", "900", "723", "699", "1", "16", "999", "890",
            "725", "998", "978", "988", "990", "989", "984", "320", "321",
            "400", "415", "450", "50", "660", "624" };

I want to replace this with something else that can produce any amount of random string numbers that I want, between a certain pre-set size. What I had in mind was to use the following code in its place:

    public class HashFunction {
   .
   .
   .
    private Random r = new Random();
    public String randomString(int limit)
     {
         int n = r.nextInt(limit);
         return n+"";
     }
   .
   .
   .
public static void main(String[] args) {

    HashFunction theFunc = new HashFunction(30); // this is where you can control the number of spaces in the hash table!!!
            String[] elementsToAdd2 = new String[30];

            for (int i = 0; i <= elementsToAdd2.length; i++)
                {
                  String[i] elementsToAdd2 = randomString(1000);   

                }

When I read this, I take it to mean that the array string value at "i" will be assigned to the random value that will be between 0 and 1000. However, I instead get an error message. It says that a ";" was expected, and that I have incompatible types. I think that Java is reading this as saying that I am trying to set an array of strings equal to single string, even though I included the "i" within "String[i]" to specify where in the array to place the value.

If anyone can help me understand my problem, please let me know. Thank you!

p.s. This code creates hash tables, in case that is relevant to the problem I am encountering. I can provide the code if you'd like, but I wanted to isolate the problem and only show what is relevant to it.

1
  • 1
    Not String[i] elementsToAdd2 = ... but elementsToAdd2[i] = .... And the loop should not run while i <= elementsToAdd2.length, but while i < elementsToAdd2.length. Commented Mar 3, 2014 at 11:00

3 Answers 3

3

String[i] elementsToAdd2 = randomString(1000);

Is wrong. String[i] is used to define an array like you have already did, here

String[] elementsToAdd2 = new String[30];

but you are trying to assign to it a value which is not array as it should be an array element. So to add an element to an array, you use the array name and index and set it to the required value

elementsToAdd2[i] = randomString(1000);


Additionally

Array Size

This code will cause an ArrayOutOfBoundsIndexException as you are trying to access elements in the array that are one greater than it's length with

i <= elementsToAdd2.length

As an arrays length is equal to how many locations it has but these location indexes start at 0, meaning the last element is always length-1, use

i < elementsToAdd2.length

Method Access (pointed out by @Dominic)

The method you are accessing randomString() from is static (as it's your main method) but that method is an instance variable (is associated with an object), you need to access it via the object it belongs to i.e.theFunc, like so

 theFunc.randomString(1000)
Sign up to request clarification or add additional context in comments.

2 Comments

You also need to call theFunc.randomString(1000) instead of randomString(1000)
Thanks guys! I implemented exactly what Ross Drew and Dominic advised, and now everything is working properly. I really appreciate you taking the time to teach me.
0

remove this String[i] elementsToAdd2 = randomString(1000) statment from the loop use loop varaible with array obejct not with string

for (int i = 0; i <elementsToAdd2.length; i++)
                {
                   elementsToAdd2[i] = randomString(1000);   

                }

1 Comment

Your code does not compile. Your String elementsToAdd2[I] = randomString(1000); will at best define loop local variable and will not affect the main array.
0

In your for loop you say:

for (int i = 0; i <= elementsToAdd2.length; i++)
{
    String[i] elementsToAdd2 = randomString(1000);   
}

This should be:

for (int i = 0; i <= elementsToAdd2.length; i++)
{
   elementsToAdd2[i] = theFunc.randomString(1000);   
}

You assign the string to be the elementsToAdd2 value at position i, rather than the entire Array, which causes the incompatible type error. You also need to call the randomString method of theFunc instance rather than just the method call on it's own.

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.