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
Related
I have a Decoded URL String that is created dynamically that is in the following form.
field1==Z;field2==abc;field3==000;field4==100000154;field5==XLPO;field6==Z3&limit=2
I want to be able to dynamically pass in a key and obtain its relavent value. Say if we pass in "field2" then we should get "abc".
Example:
public class ComplexQueryParamProcessor {
public static void main(String[] args) {
String URL = "field1==Z;field2==abc;field3==000;field4==100000154;field5==XLPO;field6==Z3&limit=2";
String value = getValueFromURLString(URL, "field4"); // should return "100000154"
System.out.println(value);
}
public static String getValueFromURLString (String URL, String key){
String value = null;
//do something and extract "value" of "key" from URL
return value;
}
}
Any help would be greatly appreciated.
Thanks to schred I was able to figure it out using a combination of indexOf() and split()
public static String getValueFromURLString (String URL, String key){
String value = null;
//do something and extract "value" of "key" from URL
int index = URL.indexOf(key);
String s1 = URL.substring(index, URL.length());
String s2 = s1.split(";" , 2)[0];
value = s2.split("==", 2)[1];
return value;
}
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
I am new in this Java journey, at College they are asking me to
"Define five String variables in the main method called: shipmentNum, supplierName, revDate, revTime, employeeNum." And assign the following text:99, Costco, 12/15/2011, 10:25 AM, 33."
I have this so far, but is giving an error message: "the local variable shipmentNum is never read", I don't see why am I getting this error message.
package c1;
public class ShipmentApp {
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println("99");
System.out.println("Costco");
System.out.println("12/15/2011");
System.out.println("10:25 AM");
System.out.println("33");
}
}
What you are seeing is a compiler warning, not an error. This is basically Java trying to help you find flaws in your code by analyzing what you wrote and detecting common mistakes.
In this case, Java recognized that you assigned values to a bunch of variables, but after that never use those variables again.
You probably want to write out the values of the variables, not the assigned values again.
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum );
System.out.println(supplierName );
System.out.println(revDate );
System.out.println(revTime );
System.out.println(employeeNum );
}
Thats warning not error but try this.
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
}
or just try:
package c1;
public class ShipmentApp{
public static void main(String[] args){
System.out.println("99");
System.out.println("Costco");
System.out.println("12/15/2011");
System.out.println("10:25 AM");
System.out.println("33");
}
}
What you are receiving are warnings because you are not actually ever reading any of the variables you have declared. You could correct this by passing the variables to println instead of typing out the strings twice.
Being that these are only warnings, they should not affect the ability of the program to compile and/or execute. While you could conceivably ignore such warnings, it's usually wise to at least analyze what they are being caused by.
It's a warning that the java compiler tells you that you defined a variable but never used it.
The purpose of a variable, is that it stores information that will be used at some later point in your code. Java gives you a warning, because if you define a variable but never use it you've likely made a mistake. This is because variables that are never used are basically nonsense, and should never have been declared.
Try these print statements:
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
That's is not an Error,It is just a warning shown by the IDE or compiler. There is no issue in this code to compile and run.
You may want to do as follows
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
You are writing the text out, not the value of the variables. It isn't an error, just a warning. You should probably change your code to this:
package c1;
public class ShipmentApp{
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
}
}
this message is just to notice you, you have some variables haven't been used. It is not a error. if you dont want to see this warning or you cannot stand it, you can add #supresswarnings annotation at the beginning of the class. or you just follow others' suggestion to use the variables you have created.
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.
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