22

I have a frustrating issue which could be easy. Arrays in java seem to be frustratingly not intuitive.

I have an String array called title it has several titles

here is part of the array

    private String[] title = {
        "Abundance",
        "Anxiety",
        "Bruxism",
        "Discipline",
        "Drug Addiction"
    }

This part seems OK in as the code compiles and runs just fine now I want to create another array BASED on this array. the new arrays will be made static text concatenated with data from this array and then more static text.

I define the two static strings

    String urlbase = "http://www.somewhere.com/data/";
    String imgSel = "/logo.png";

so i added the declaration for the new array

    String[] mStrings;

and then I create a basic for loop to iterate through and create the elements of the new array

    for(int i=0;i<title.length;i++) {
        mStrings[i] = urlbase + title[i].replaceAll("[^a-zA-Z]", "").toLowerCase() + imgSel;
    }

the loop takes the array value and strips out non alpha chars and makes it lowercase thus

Drug Addiction

becomes

drugaddiction

I want to end up with something like this

mStrings[0]="http://www.somewhere.com/data/abundance/logo.png"
mStrings[1]="http://www.somewhere.com/data/anxiety/logo.png"
mStrings[2]="http://www.somewhere.com/data/bruxism/logo.png"
mStrings[3]="http://www.somewhere.com/data/discipline/logo.png"
mStrings[4]="http://www.somewhere.com/data/drugaddiction/logo.png"

I tried several different attempts at declaring mStrings but all were incorrect when I leave it out Eclipse suggests this

 String[] mStrings; 

Now this seems like it should be fairly easy and correct but when I enter anything after it I get an error that says

 Syntax error on token ";", { expected after this token

Since it is one to one with the other array I tried this in the declaration but it also fails

    String[] mStrings[title.length];  

just to give it a quantity

I am thinking the error is someplace in the declaration but I can't seem to find any docs that lay it out clearly.

It seems like it is expecting not only a declaration but also loading of the array to occur which is what I do not want but I also tried loading it with three elements but it still didn't work right

As I stated though I want to load it in the for loop

Any help will be appreciated.

I did try to set the array size but got the same error

here is the exact code

maybe it is elsewhere

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

ListView list;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    list=(ListView)findViewById(R.id.list);
    adapter=new LazyAdapter(this, mStrings);
    list.setAdapter(adapter);

    Button b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(listener);
}

@Override
public void onDestroy()
{
    list.setAdapter(null);
    super.onDestroy();
}

public OnClickListener listener=new OnClickListener(){

    public void onClick(View arg0) {
        adapter.imageLoader.clearCache();
        adapter.notifyDataSetChanged();
    }
};


private String[] title = {
        "Abundance",
        "Anxiety",
        "Bruxism",
        "Discipline",
        "Drug Addiction"
}


String urlbase = "http://imobilize.s3.amazonaws.com/giovannilordi/data/";
String imgSel = "/logo.png";    
String[] mStrings = new String[title.length];

ERROR SHOWS HERE

for(int i=0;i<title.length;i++) {
    mStrings[i] = urlbase + title[i].replaceAll("[^a-zA-Z]", "").toLowerCase() + imgSel;
}

screenshot of error in eclipse
(source: imobilizeit.com)

Glorfindel
  • 19,729
  • 13
  • 67
  • 91
Jeff Janes
  • 841
  • 1
  • 7
  • 22
  • [Third result for "Array string declaration java" in Google.](http://stackoverflow.com/questions/1200621/how-to-declare-an-array-in-java) – Kevin Coppock Nov 02 '12 at 04:13
  • I have been trying the suggestions mentioned by @Ram kiran but I still see the &^%$ error Again the error is ----Syntax error on token ";", { expected after this token---- The second error flag says to add another '}' to close the class body which makes me think something is incorrect in the syntax of that first array of strings, title is that correct? – Jeff Janes Nov 02 '12 at 15:54
  • Try cleaning your project. It's probably just Eclipse screwing up. I spent 15-20 minutes hunting down a syntax error one time only to realize it was Eclipse misinterpreting it. – Kevin Coppock Nov 02 '12 at 16:28
  • I have tried cleaning it restarting it anything else I can think of I am really at a loss...any other ideas...anyone – Jeff Janes Nov 02 '12 at 17:40

6 Answers6

25

use:

String[] mStrings = new String[title.length];
Trần Sĩ Long
  • 447
  • 3
  • 14
16

You are not initializing your String[]. You either need to initialize it using the exact array size, as suggested by @TrầnSĩLong, or use a List<String> and then convert to a String[] (in case you do not know the length):

String[] title = {
        "Abundance",
        "Anxiety",
        "Bruxism",
        "Discipline",
        "Drug Addiction"
    };
String urlbase = "http://www.somewhere.com/data/";
String imgSel = "/logo.png";
List<String> mStrings = new ArrayList<String>();

for(int i=0;i<title.length;i++) {
    mStrings.add(urlbase + title[i].toLowerCase() + imgSel);

    System.out.println(mStrings[i]);
}

String[] strings = new String[mStrings.size()];
strings = mStrings.toArray(strings);//now strings is the resulting array
Phil
  • 34,061
  • 21
  • 117
  • 154
6

Declare the array size will solve your problem

 String[] title = {
            "Abundance",
            "Anxiety",
            "Bruxism",
            "Discipline",
            "Drug Addiction"
        };
    String urlbase = "http://www.somewhere.com/data/";
    String imgSel = "/logo.png";
    String[] mStrings = new String[title.length];

    for(int i=0;i<title.length;i++) {
        mStrings[i] = urlbase + title[i].toLowerCase() + imgSel;

        System.out.println(mStrings[i]);
    }
Ram kiran
  • 20,129
  • 14
  • 55
  • 74
  • as I stated I did try that exact thing but I still get the error----Syntax error on token ";", { expected after this token----- – Jeff Janes Nov 02 '12 at 04:41
  • i did exactly that and the error still shows so I added the entire file maybe it is another error that just shows in the location that is close to the statement. The only way the error does not show up is scrap trying to load it programatically and simply load it at the time of declaration like the title array is done but that defeats the purpose. trying to get away from hardcoding the values – Jeff Janes Nov 02 '12 at 05:17
4

I think the beginning to the resolution to this issue is the fact that the use of the for loop or any other function or action can not be done in the class definition but needs to be included in a method/constructor/block definition inside of a class.

Jeff Janes
  • 841
  • 1
  • 7
  • 22
2

You can write like below. Check out the syntax guidelines in this thread

AClass[] array;
...
array = new AClass[]{object1, object2};

If you find arrays annoying better use ArrayList.

Community
  • 1
  • 1
san
  • 1,872
  • 13
  • 22
2

As Trần Sĩ Long suggested, use

String[] mStrings = new String[title.length];

And replace string concatation with proper parenthesis.

mStrings[i] = (urlbase + (title[i].replaceAll("[^a-zA-Z]", ""))).toLowerCase() + imgSel;

Try this. If it's problem due to concatation, it will be resolved with proper brackets. Hope it helps.

MysticMagicϡ
  • 27,509
  • 15
  • 68
  • 114