I've been trying to get this bit of Android code working for a while, and eclipse is giving me the ever so helpful Syntax Error when there shouldn't be one. I checked my brackets and I have the right number, and I believe they are all where they should be. This is code copied and pasted from a tutorial, and slightly tweaked. The code provided, in its entirety, works, but my version of it gives the errors. I didn't change that much.
public class MainActivity extends Activity{
Button Pull_Data;
// Define the TextViews
TextView client_Address1;
TextView client_Address2;
TextView client_Address3;
TextView client_Id;
// XML node keys
static final String KEY_ITEM = "Address"; // parent node
static final String KEY_PERSON = "addedByPerson";
static final String KEY_CITY = "addressCity";
static final String KEY_LINE_ONE = "addressLineOne";
static final String KEY_LINE_TWO = "addressLineTwo";
static final String KEY_STATE = "addressState";
static final String KEY_TYPE_ID = "addressTypeId";
static final String KEY_ZIP = "addressZip";
static final String KEY_CLIENT_ID = "clientId";
static final String KEY_COUNTRY_CODE = "countryCode";
static final String KEY_OBJECT_ID = "objectId";
static final String KEY_RECORD_ADDED_DATE = "recordAddedDate";
static final String KEY_RECORD_UPDATED_DATE = "recordUpdatedDate";
static final String KEY_RECORD_UPDATED_PERSON = "recordUpdatedPerson";
static final String KEY_SYNC_STATUS = "syncStatus"; //Syntax error is flagged here
// XML Data to Retrieve
Address = "";
addedByPerson = "";
addressCity = "";
addressLineOne = "";
addressLineTwo = "";
addressState = "";
addressTypeId = "";
addressZip = "";
clientId = "";
countryCode = "";
objectId = "";
recordAddedDate = "";
recordUpdatedDate = "";
recordUpdatedPerson = "";
syncStatus = "";
// Holds values pulled from the XML document using XmlPullParser
String[][] xmlPullParserArray = {{"Address", "0"},{"addedByPerson", "0"},{"addressCity", "0"},{"addressLineOne", "0"},{"addressLineTwo", "0"},
{"addressState", "0"},{"addressTypeId", "0"},{"addressZip", "0"},{"clientId", "0"},
{"countryCode", "0"},{"objectId", "0"},{"recordAddedDate", "0"},{"recordUpdatedDate", "0"},
{"recordUpdatedPerson", "0"},{"syncStatus", "0"}};
int parserArrayIncrement = 0;
// END OF NEW STUFF
#Override
protected void onCreate(Bundle savedInstanceState){ //Another error here, tagged at the first paren after onCreate
super.onCreate(savedInstanceState);
I have no clue what could be wrong. Structurally the code hasn't changed. The first error by static final String KEY_SYNC_STATUS = "syncStatus"; is a Synatx Error on token ";", { expected after this token
The two errors on the OnCreate method are Syntax Error on token "(" expected ; and Syntax Error on token ")" expected ; Any help is appreciated
Your error is here:
// XML Data to Retrieve
Address = "";
addedByPerson = "";
addressCity = "";
Your missing the types. What is Address, a String? What is addedByPerson?
// XML Data to Retrieve
String Address = "";
String addedByPerson = "";
String addressCity = "";
...
What is this? It is not valid parts of Java class:
//XML Data to Retrieve
Address = "";
Eclipse has it's own compiler that tries to compile file by parts. It is called incremental compiler.
Maybe it just can not split your class to compilable blocks? Try to solve these problems.
Related
While i am trying to convert below code snippet using java8 getting "Local variable sId defined in an enclosing scope must be final or effectively final" error.
Existing code:
String sId = "";
String uId = "";
FinalResp resp = new FinalResp();
List<LiqResp> liqRespList = getResp.getLiqRespList();
for(LiqResp liqResp : liqRespList){
List<ListOfAcq> listAcq = liqResp.getLiqList();
for(ListOfAcq acqList : listAcq){
List<Acq> acqs = acqList.getAcqList();
for(Acq acq :acqs){
if(sId.equalIgnoreCase(acq.getId()) || uId.equalIgnoreCase(acq.getId())){
resp.setId(acqList.getId());
}
}
}
}
return resp;
Below is the snippet which i tried to convert using java8:
String sId = "";
String uId = "";
FinalResp resp = new FinalResp();
List<LiqResp> liqRespList = getResp.getLiqRespList();
liqRespList.parallelStream.forEach(liqResp -> liqResp.getLiqList().parallelStream.
forEach(acqList->acqList.getAcqList().stream.map(acq -> {
return conversion(acq, acqList, sId,uId,resp); //getting error here as "Local variable sId
//defined in an enclosing scope must be final
// or effectively final"
})));
private FinalResp conversion(Acq acq, ListOfAcq acqList, String sId,String uId,FinalResp resp){
if(sId.equalIgnoreCase(acq.getId()) || uId.equalIgnoreCase(acq.getId())){
resp.setId(acqList.getId());
}
return resp;
}
Performance wise is this correct approach.thanks in advance for help.
I am looking for a solution to add inside a String URL a variable (Post parameter).
SharedPreferences sp = getPreferences(MODE_PRIVATE);
String my_variable = "test";
private static final String READ_COMMENTS_URL = "http://xxx/comments.php?usernames= my_variable";
It should eventually possible with string.format. Can anyone give a hint?
SharedPreferences sp = getPreferences(MODE_PRIVATE);
String my_variable = "test";
private static final String READ_COMMENTS_URL = "http://xxx/comments.php?usernames= "+my_variable;
I would use Java String formatting like below:
SharedPreferences sp = getPreferences(MODE_PRIVATE);
String my_variable = "test";
private static final String READ_COMMENTS_URL =
String.format("http://xxx/comments.php?usernames=%s", my_variable);
Here is reference for Formatting Strings:
http://alvinalexander.com/blog/post/java/use-string-format-java-string-output
In your code my_variable is a string not variable because you using it as string inside of double quotes("")
private static final String READ_COMMENTS_URL =
"http://xxx/comments.php?usernames= my_variable";
If you are using variable with string you have to concatenate string with variable
private static final String READ_COMMENTS_URL =
"http://xxx/comments.php?usernames="+my_variable;
If you want to format your string use String class api
static String format(String format, Object... args)
Formats the supplied objects using the specified message format pattern.
private static final String READ_COMMENTS_URL =
String.format("http://xxx/comments.php?usernames=%s", my_variable);
java.text.MessageFormat should work for you - http://developer.android.com/reference/java/text/MessageFormat.html
I'm converting json string to display in android textview, I'm using following code in android
My json file look likes,
{"managementlist":[{"designation":"PRESIDENT","address":"<strong>Sri<\/strong><br>M\/s.Jewellery Mart<br>No:19\/1, Raghavaiah Road,<br>T.Nagar, Chennai - 600 017.","office":"+91 44 42122288","residence":"+91 44 28341155","centrix":"2159,#421155","fax":"+91 44 42122200","mobile":"+91 9940147199","email":"jayan1411#gmail.com","image":"http:\/\/mydomain.org\/images\/ssss.jpg?1419856154","divider":"0"},{"designation":"VICE PRESIDENT","address":"<strong>Sri Yogesh J Shah<\/strong><br>M\/s. Doimands<br>\r\n "Swarna sree", shop-201,<br># 36\/2,Veerappan Street,<br> Chennai -600 079.","office":"+91 44 25385336","residence":"+91 44 26442978","centrix":"#410024","fax":"+91 44 25387772","mobile":"+91 9382616888","email":"yogesh_shah31#yahoo.co.in","image":"http:\/\/mydomain.org\/images\/Sriyogesh.jpg?1419856154","divider":"0"}]}
And my java code looks like,
public class Managements {
private String designation;
private String address;
private String officePhone;
private String residencePhone;
private String centrixNo;
private String faxNo;
private String mobileNo;
private String email;
private String image;
private int header;
get() & set();
}
Json Parser:
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
for (String line = null; (line = bufferedReader
.readLine()) != null;) {
builder.append(line);
}
String returnStr = StringEscapeUtils
.unescapeJava(builder.toString());
String s = returnStr;
JSONObject json = new JSONObject(s);
JSONArray membersarray = json
.getJSONArray("managementlist");
for (int i = 0; i < membersarray.length(); i++) {
JSONObject member = membersarray.getJSONObject(i);
Log.i("JSON OBJECT :", member.getString("mobile")
+ " , " + member.getString("divider"));
String designation = member
.getString("designation");
String address = member.getString("address");
String officePhone = member.getString("office");
String residencePhone = member
.getString("residence");
String centrixNo = member.getString("centrix");
String faxNo = member.getString("fax");
String mobileNo = member.getString("mobile");
String email = member.getString("email");
String image = member.getString("image");
int header = member.getInt("divider");
list_managements.add(new Managements(designation,
address, officePhone, residencePhone,
centrixNo, faxNo, mobileNo, email, image,
header));
}
And in my adapter
addressView.setText(Html.fromHtml(managements.get(position).getAddress()),TextView.BufferType.SPANNABLE);
But my list view display the html string as like
<strong>Sri <\/strong><br>M\/s.Jewellery Mart<br>No:19\/1, Raghavaiah Road,<br>T.Nagar, Chennai - 600 017
This code didn't convert my html string to textview, What is my mistake. Please help anyone.
you need do script like this
// Textview variable
TextView bdeskripsi = (TextView) findViewById(R.id.ddeskripsi);
// Decode htmlentities
String htmlcodeenti = Html.fromHtml(yourdatajson).toString();
// Decode tag html to webview
bdeskripsi.setText(Html.fromHtml(htmlcodeenti));
I have a fixed length String record, i want to replace the string at a specific position with different string value.
String record ="ABCU0MARK 111111118 CFTT 130913 101100023424";
String extractAccountaccountNumber = record.substring(79, 87);
String newAccountNumber = "some value"
record = record.replaceFirst(extractAccountaccountNumber,newAccountNumber);
This may not work if there are duplicate values. Please advice
you just need to assign it to a new String variable, or to itself:
string = string.replace("to", "xyz");
or
String newString = string.replace("to", "xyz");
public class Run1 {
public static final int its = 4;
public static void main(String[] args) {
String record ="ABCU0MARK 111111118 CFTT 130913 101100023424";
String extractAccountaccountNumber = record.substring(46, 55);
System.out.println("extractAccountaccountNumber:"+extractAccountaccountNumber);
String newAccountNumber = "some value";
String result=record.replaceFirst(extractAccountaccountNumber,newAccountNumber);
System.out.println("result:"+result);
}
}
here is result:
extractAccountaccountNumber:FTT
result:ABCU0MARK 111111118 Csome value 130913 101100023424
My Question: It's very specific. I'm trying to think of the easiest way to parse the following text:
^^domain=domain_value^^version=version_value^^account_type=account_type_value^^username=username_value^^password=password_value^^type=type_value^^location=location_value^^id=xxx^^cuid=cuid_value^^
It will appear exactly like that every time. A few requirements:
Not all of those key-value pairs will appear every time.
They may be in a different order
I'm looking for code something like this:
private String[] getKeyValueInfo(String allStuff) {
String domain = someAwesomeMethod("domain", allStuff);
String version = someAwesomeMethod("version", allStuff);
String account_type = someAwesomeMethod("account_type", allStuff);
String username = someAwesomeMethod("username", allStuff);
String password = someAwesomeMethod("password", allStuff);
String type = someAwesomeMethod("password", allStuff);
String location = someAwesomeMethod("location", allStuff);
String id = someAwesomeMethod("id", allStuff);
String cuid = someAwesomeMethod("cuid", allStuff);
return new String[] {domain, version, account_type, username, password, type, location, id, cuid};
}
What I don't know is what someAwesomeMethod(String key, String allStuff) should contain.
What I was thinking: Something like this:
private String someAwesomeMethod(String key, String allStuff) {
Pattern patt = Pattern.compile("(?i)^^" + key + "=(.*?)^^", Pattern.DOTALL);
Matcher matcher = patt.matcher(allStuff);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
What's wrong with that:
I'm worried it'd be a little slow/cumbersome if I had to do this a lot. So I'm looking for any tips/suggestions.
If you have to do it a lot, i'd make a map, something along the lines of
Map<String, String> m = new HashMap<String, String>();
for (String s : stuff.split("\\^\\^")) // caret needs escaping
{
String[] kv = s.split("=");
m.put(kv[0]) = kv[1];
}
then to lookup a key you'd just do m.get("key")
String.split() will work for that
strVar = /* Your big long string */
String[] vars = strVar.split("\\^\\^"); // needs escaping