Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, April 17, 2016

Array String Declaration

Array String Declaration


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

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

screenshot of error in eclipse

Answer by Tr?n S? Long for Array String Declaration


use:

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

Answer by Ram kiran for Array String Declaration


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

Answer by san for Array String Declaration


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.

Answer by Phil for Array String Declaration


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 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 mStrings = new ArrayList();    for(int i=0;i

Answer by MysticMagic? for Array String Declaration


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.

Answer by Jeff Janes for Array String Declaration


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.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.