Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array String Declaration

Tags:

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)

like image 935
Jeff Janes Avatar asked Nov 02 '12 04:11

Jeff Janes


People also ask

How do you initialize a string array in Java?

There are two ways to declare string array - declaration without size and declare with size. There are two ways to initialize string array - at the time of declaration, populating values after declaration. We can do different kind of processing on string array such as iteration, sorting, searching etc.

How can you declare an array of strings in C?

Use 2D Array Notation to Declare Array of Strings in C Note that one extra character space for the terminating null byte should be considered when copying the character string to the array location. Thus, one can declare a two-dimensional char array with brackets notation and utilize it as the array of strings.

What is array of strings give an example?

It is actually a two dimensional array of char type. Example: char names[6][30]; In above example, names is an array of strings which can contain 6 string values. Each of the string value can contain maximum 30 characters.


2 Answers

use:

String[] mStrings = new String[title.length]; 
like image 176
Trần Sĩ Long Avatar answered Oct 29 '22 02:10

Trần Sĩ Long


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 
like image 28
Phil Avatar answered Oct 29 '22 01:10

Phil