Error: “; Expected Not a statement ” - java

For the line data[] = {nam, addres, adminStaf, phoneNumbe, typ}; NetBeans gives me this error
"; Expected Not a statement"
I don't know why this is not working, help will be appreciated.
public Object[] getObjects() {
Object[] data = {};
for (int i = 0; i < Storage.getAgencies().size(); i++)
{
String nam = Storage.getAgencies().get(i).getName();
String addres = Storage.getAgencies().get(i).getAddress();
String adminStaf = Storage.getAgencies().get(i).getAdminstaff();
String phoneNumbe = Storage.getAgencies().get(i).getPhonenumber();
String typ = Storage.getAgencies().get(i).getType();
data[] = {nam, addres, adminStaf, phoneNumbe, typ};
}
return data;
}

The syntax you're using is only allowed when initializing an array. You should do the following instead:
data = new String[] {nam, addres, adminStaf, phoneNumbe, typ};

Related

How to convert a string contains array format to java array or list?

I have a string like this:
String res = '["A","B","0","1"]';
How to convert it to an array or list in Java to become like this:
String[] r = {"A","B","0","1"};
Since your string is a correct Json string, you may use the gson library:
String s = "[\"A\",\"B\",\"0\",\"1\"]";
Gson gson = new GsonBuilder().create();
String[] arr = gson.fromJson(s, String[].class);
// {"A","B","0","1"}
Without using Gson, You can get the desired result by following below approach -
String inputStr = "[\"A\",\"B\",\"0\",\"1\"]";
String[] strArray = inputStr.split("[^\\w\\d]");
List<String> list = new ArrayList<>();
for (String str : strArray) {
if (str != null && !str.isEmpty()) {
list.add(str);
}
}
System.out.println(list);
Output will be: [A, B, 0, 1]
String res = "[A,B,0,1]";
res = res.replace("[", "");
res = res.replace("]", "");
res = res.replaceAll(",", "");
String [] arrayOutput = new String [res.length()];
char [] ar = res.toCharArray();
for(int i = 0; i< ar.length; i++)
{
char buffer = ar[i];
arrayOutput[i] = String.valueOf(buffer);
}

Search in ArrayList and count

In this code I want to search in an ArrayList but my code returns an incorrect result and I can not resolve this problem.
ReceivedItemStructure structure:
public class ReceivedItemStructure {
public String mLastID;
public String mUserID;
public String mSmsBody;
public String mMobileNumber;
public String mDate;
public String mSenderName;
public String mSmsNumber;
public String mContactName;
public String getmLastID() {
return mLastID;
}
}
My Code:
int countSMS = 0;
String smsReceivedSender = "";
String r = new JsonService(config_username, config_password, 0, 20, G.F_RECEIVE_SMS).request();
JSONArray data_array = new JSONArray(r);
for (int i = 0; i < data_array.length(); i++) {
JSONObject json_obj = data_array.getJSONObject(i);
String mId = json_obj.getString("id_recived_sms");
for (ReceivedItemStructure rf:items){
if( ! mId.equals(rf.getmLastID()) ) {
countSMS++;
}
}
}
My problem is this line :
if( ! mId.equals(rf.getmLastID()) ) {
if mId = 2000 and rf.getmLastID() = 1000 then count must be ++
Loop through your list and do a contains or startswith.
ArrayList<String> resList = new ArrayList<String>();
String searchString = "man";
for (String curVal : list){
if (curVal.contains(searchString)){
resList.add(curVal);
}
}
You can wrap that in a method. The contains checks if its in the list. You could also go for startswith.
Ok so to clarify please try to debug your code like this:
int countSMS = 0;
String TAG = "Debugger";
String smsReceivedSender = "";
String r = new JsonService(config_username, config_password, 0, 20, G.F_RECEIVE_SMS).request();
JSONArray data_array = new JSONArray(r);
Log.i(TAG, "items size is " + items.size());
for (int i = 0; i < data_array.length(); i++) {
JSONObject json_obj = data_array.getJSONObject(i);
String mId = json_obj.getString("id_recived_sms");
Log.i(TAG, "Trying to compare " + mId);
for (ReceivedItemStructure rf:items){
Log.i(TAG, "iteration step of " + rf.getmLastID);
if( ! mId.equals(rf.getmLastID()) ) {
countSMS++;
Log.i(TAG, "they are equal, count is " + countSMS);
}
}
}

Why am I getting duplicates when combining multiple ArrayLists?

Why I am getting duplicate entries in my ArrayList<String[]>?
allStepsJSONStringArray contains an array of single strings in the format of JSON
I loop through and pass each JSON string to a function that writes it to a temporary internal file
I read the file
Then pass it to getStepsArray() which breaks down the JSON string and puts each entry into a String[]
Loop to add to master ArrayList - allStepsArray
for (int i = 0; i < allStepsJSONStringArray.size(); i++) {
writer.writeToInternal(allStepsJSONStringArray.get(i));
reader.readFromInternal(writer.filename);
stepsArray = reader.getStepsArray();
for (int s = 0; s < stepsArray.size(); s++) {
allStepsArray.add(stepsArray.get(s));
}
}
getStepsArray()
public ArrayList<String[]> getStepsArray() {
try {
JSONObject jObject = new JSONObject(jsonString);
JSONArray jArray = jObject.getJSONArray("steps");
String stepOrder = null;
String stepName = null;
String stepType = null;
String stepId = null;
String checklistId = null;
String checklistName = null;
for (int i = 0; i < jArray.length(); i++) {
stepOrder = jArray.getJSONObject(i).getString("order");
stepName = jArray.getJSONObject(i).getString("name");
stepType = jArray.getJSONObject(i).getString("type");
stepId = jArray.getJSONObject(i).getString("id");
checklistId = jObject.getString("checklistId");
checklistName = jObject.getString("checklistName");
stepsArray.add(new String[] {stepOrder, stepName, stepType, stepId, checklistName, checklistId});
}
} catch (Exception e) {
e.printStackTrace();
}
return stepsArray;
}
Word for word:
Because you don't seem to ever reset stepsArray. The second time you add elements to it, the previous elements will still be there and will get added to allStepsArray again.

How to convert java arraylist to json array store data using java and jsp and extjs

is there any way to convert array list to json store input data using jsp and java for extjs.
means i need to get data for json store from jsp page and java arraylis
Here you go hope this helps you... Here im getting state values from db..
String response = "id,State#";
List stateList = new ArrayList<>();
//DB call
for (int i = 0; i < stateList.size(); i++) {
response += stateList.get(i).gets_code() + ",";
response += stateList.get(i).getS_name() + "#";
}
StringTokenizer st = new StringTokenizer(response , "|");
String finalMsg = null;
String str1 = null;
while (st.hasMoreElements()) {
String token = st.nextToken();
finalMsg = token;
}
JSONObject object = new JSONObject();
stbuffer.append("{\"root\":[");
String[] data = finalMsg.split("#");
int len = data.length;
String[] headings = data[0].split(",");
for (int x = 1; x < len; x++) {
String[] data1 = data[x].split(",");
int len1 = data1.length;
for (int y = 0; y < len1; y++) {
object.put(headings[y], data1[y]);
}
stbuffer.append(object);
stbuffer.append(",");
}
stbuffer.append("]}");
String result = stbuffer.toString();
result = result.replace(",]", "]");

split variables from texte

this method runs an exception and i didn't find why.
private void loadTrace () {
BufferedReader reader = new BufferedReader(
new StringReader(logTextArea.getText()));
String str;
try {
while(reader != null)
{
str =reader.readLine();
String [] splitted = str.split("\\|");
String b = splitted[1].trim();
String c = splitted[2].trim();
String d = splitted[3].trim();
String Chemin;
String Type = "action" ;
String Description;
if (d!=null) {
Description=d;
}
else Description ="Afficher onglet";
if (c!= null) {
Chemin= b+"."+c;
}
else Chemin =b;
String trace =Type+" "+Description +" "+Chemin ;
ArrayList<String> p = new ArrayList<String>();
p.add(trace);
System.out.println(p);
}
}
catch(IOException e) {
e.printStackTrace();
}
}
Without knowing the exception I can guess one of the potential issue is in these lines:-
String [] splitted = str.split("\\|");
String b = splitted[1].trim();
String c = splitted[2].trim();
String d = splitted[3].trim();
You are accessing splitted without checking if it is null or size so you may run into ArrayIndexOutOfBound exception if splitted length is less than 3. So modify the code this way-
String [] splitted = str.split("\\|");
if(splitted!=null && splitted.length==3){
String b = splitted[0].trim();
String c = splitted[1].trim();
String d = splitted[2].trim();
}
The NullPointerException you get now that you've fixed the ArrayIndexOutOfBound is because of the test you use in your while loop:
while(reader != null)
{
...
}
reader will always be non-null so this loop will never end. You need to test if
reader.readLine()
returns null (indicating EOF).
I guess you get an ArrayIndexOutOfBoundException, right?
(You need to tell us, what Exception you receive)
The problem could be in the following lines. You should check the size of the array and not just "hope" that it has three parts.
String b = splitted[1].trim();
String c = splitted[2].trim();
String d = splitted[3].trim();

Categories