I'm wanting to set a final variable after starting the constant. This works if the variable is not final, but that kind of defeats the purpose.
Can I do something similar to this?
public static final String ASDF = null;
{
ASDF = "asdf";
}
My situation:
public static JSONArray CATEGORIES = null;
{
String str = "";
str += "\"" + FIRST_CATEGORY + "\"";
try {
CATEGORIES = new JSONArray("[" + str + "]");
} catch (JSONException e) {
e.printStackTrace();
}
};
You can use a static initialization block - at least, you can in regular Java SE (I'm assuing android can too):
public static final JSONArray CATEGORIES;
static {
String str = "\"" + FIRST_CATEGORY + "\"";
try {
CATEGORIES = new JSONArray("[" + str + "]");
} catch (JSONException e) {
e.printStackTrace();
}
}
Note that you are not first intializing CATEGORIES to null, it is left uninitialized until the static block happens.
Although, you're probably going to want to hard-fail if the intialization generates an exception, because otherwise you'll have an improperly initialized variable (serious problems possible).
And, unless the JSONArray class is immutable, declaring the instance final is ~sorta pointless.
Figured it out. I'm not exactly sure what's going on here, but it works.
public static final JSONArray CATEGORIES = new JSONArray() {
{
put(FIRST_CATEGORY);
// etc eg. put(SECOND_CATEGORY);
}
};
you can choose to not include "= null" on your variable declaration. just make sure that you assign a value to your variable once - be it inside an if-else, a loop, or whatever - the compiler will detect if you're breaking this rule, and won't let your program compile.
public static JSONArray CATEGORIES = null;
{
String str;
str += "\"" + FIRST_CATEGORY + "\"";
try {
CATEGORIES = new JSONArray("[" + str + "]");
} catch (JSONException e) {
e.printStackTrace();
}
};
Related
I know this question is asked a lot, but I am genuinely stumped, I have looked at other methods, and tried everything...
My coding problem is as follows, I have a public class in java, and I made a string array in the global scope. I tried to add to it using array.add("somestringhere"), but it spits out an error saying (the title)... One more thing to note is this class is inside the main class for the file. so let's see if I can show it here:
public class see_schedule_activity extends AppCompatActivity {
.
..
.
.
.
// you can make this class as another java file so it will be separated from your main activity.
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
// set your json string url here
String yourJsonStringUrl = "http://www.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxphp";
// contacts JSONArray
JSONArray dataJsonArr = null;
#Override
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... arg0) {
try {
// I have a intent passed from teh main class
Bundle extras = getIntent().getExtras();
String[] tasks = extras.getStringArray("tasks");
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray("schedule");
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
activityid = c.getString("activityid");
String yesno = c.getString("yesno");
String start = c.getString("start");
String stop = c.getString("stop");
String plusminus = c.getString("plusminus");
Log.e("Hash Map", "Routine ID #" + activityid);
if (!TextUtils.isEmpty(activityid) && TextUtils.isDigitsOnly(activityid)) {
routine = hm.get(Integer.valueOf(activityid));
} else {
routine = "";
}
// TODO: Make the PHP work with a variable residentID
//TODO: only make a string if the yes is checked
//TODO: make the routine name
// Log.e(TAG, "Value for yesno:" + yesno);
if (c.getString("yesno").equals("Yes")) {
schedule = routine + " starts at " + start + " and ends at " + stop + " with plusorminus " +
plusminus + " minutes ";
tasks.add(schedule);// This lines yeilds the problem
Log.e(TAG, "" + schedule);
}
// // show the values in our logcat
// Log.e(TAG, "activityid: " + activityid
// + ", yesno: " + yesno
// + ", start: " + start
// + ", stop: " + stop
// + ", plusminus: " + plusminus);
}
Intent intent = new Intent(getApplicationContext(),see_schedule_activity.class);
intent.putExtra("tasks_filled",tasks);
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String strFromDoInBg) {
}
}
I would appreciate any help, I'm a newb to programming and android studio, and php..so yeah thanks in advance, Justin
tasks is an String array as you defined
String[] tasks = extras.getStringArray("tasks");
And Array doesn't have any add method.
You need to convert tasks to a list like following
List<String> list = Arrays.asList(tasks);
Then you can use list.add(schedule)
Currently I am working on a way to run java code in string form. So here is how I did it.
import java.util.HashMap;
import java.util.Map;
import groovy.lang.GroovyClassLoader;
public class GroovyStackOverflow {
public static void main(String[] args) {
GroovyClassLoader gcl = new GroovyClassLoader();
String codeSnippet = "double calculatedAnswer = (Double)"
+ "contextMap.get(\"doubleValue\") * (Double)contextMap.get(\"doubleValue\");"
+ " calculatedAnswer = Math.sqrt(calculatedAnswer); "
+ "calculatedAnswer = calculatedAnswer * calculatedAnswer;"
+ "System.out.println(calculatedAnswer);"
+ " return calculatedAnswer;";
StringBuilder sb = new StringBuilder();
sb.append("public class ScriptImplementor implements ScriptEvaluator { public Object evaluate(Map contextMap) {");
sb.append(codeSnippet);
sb.append("} }");
Class<?> clazz = gcl.parseClass(sb.toString());
ScriptEvaluator scriptEvaluator = null;
double calculatedAnswer = 100.0;
try {
Map contextMap = new HashMap();
contextMap.put("doubleValue", (double)100.0);
contextMap.put("threadId", "thread"+100);
contextMap.put("hashCode", 100);
scriptEvaluator = (ScriptEvaluator) clazz.newInstance();
scriptEvaluator.evaluate(contextMap);;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
public interface ScriptEvaluator {
public Object evaluate(Map contextMap);
}
Problem is that it fails in following case.
import java.util.HashMap;
import java.util.Map;
import groovy.lang.GroovyClassLoader;
public class GroovyStackOverflow {
public static void main(String[] args) {
GroovyClassLoader gcl = new GroovyClassLoader();
String codeSnippet = "double calculatedAnswer = (Double)"
+ "\n "
+ "contextMap.get(\"doubleValue\") * (Double)contextMap.get(\"doubleValue\");"
+ " calculatedAnswer = Math.sqrt(calculatedAnswer); "
+ "calculatedAnswer = calculatedAnswer * calculatedAnswer;"
+ "System.out.println(calculatedAnswer);"
+ " return calculatedAnswer;";
StringBuilder sb = new StringBuilder();
sb.append("public class ScriptImplementor implements ScriptEvaluator { public Object evaluate(Map contextMap) {");
//sb.append(codeSnippet.replaceAll("\n", " "));
sb.append(codeSnippet);
sb.append("} }");
Class<?> clazz = gcl.parseClass(sb.toString());
ScriptEvaluator scriptEvaluator = null;
double calculatedAnswer = 100.0;
try {
Map contextMap = new HashMap();
contextMap.put("doubleValue", (double)100.0);
contextMap.put("threadId", "thread"+100);
contextMap.put("hashCode", 100);
scriptEvaluator = (ScriptEvaluator) clazz.newInstance();
scriptEvaluator.evaluate(contextMap);;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
public interface ScriptEvaluator {
public Object evaluate(Map contextMap);
}
I don't understand why it fails and what this error message means-
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'class java.lang.Double' with class 'java.lang.Class' to class 'double'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:163)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.doubleUnbox(DefaultTypeTransformation.java:88)
at ScriptImplementor.evaluate(script15126616543572010791987.groovy:1)
at GroovyStackOverflow.main(GroovyStackOverflow.java:33)
After uncommenting this code //sb.append(codeSnippet.replaceAll("\n", " ")); it works. But please suggest a better way to handle it. Also why it does not give error while parsing class?
And what other surprises I can expect like this?
You hit a difference between Java and Groovy.
In Java a statement is ended by a semicolon.
In Groovy a satement is ended by a semicolon, or by a linebreak if the statement already is a complete statement.
In your case this means the code
double calculatedAnswer = (Double)
contextMap.get("doubleValue") * (Double)contextMap.get("doubleValue")
is two statements.
The first of these statements is double calculatedAnswer = (Double).
In Groovy you can also omit .class to reference a class, so Double.class can be written as Double.
So what you do in that statement is, that you assign the Double class object to a double variable. The parentheses are just no-ops here.
This of course fails like the message says, as the Double class object cannot be case automatically to a double.
You can explicitly escape a linebreak to make it not end a statement like in
double calculatedAnswer = (Double)\
contextMap.get("doubleValue") * (Double)contextMap.get("doubleValue")
which would work like you expected.
But there can of course be other cases where Groovy and Java are different.
Always remember, the Groovy syntax is close to the Java syntax, yet not identical.
Afair each valid Java code is also valid Groovy code, but not necessarily with the exact same meaning as you can see in this example.
I am trying to add an object inside an object using recursion. My object contains an arrayList and I am trying to add my objects to this arrayList. But instead of adding a new object, my objects are being replaced.
My code which is doing this: This is where the logic of adding an object is being done. But it is being replaced instead.
private ArrayList<SubChapters> recursiveSubChapters(ReportingTree tree, LinkedHashMap<String, HashMap<String, String>> linkedHashMap, Boolean isSubTree){
SubChapters subChapters = new Subchapters();
ArrayList<SubChapters> alchildUnits = new ArrayList<SubChapters>();
final String chapterId = linkedHashMap.get(tree.getUnitID()).get("unit_num");
final String chapterName= linkedHashMap.get(tree.getUnitID()).get("unit_name");
if (!isSubTree) {
subChapters.set(chapterId);
subChapters.setTreeName(chapterName);
}
final ArrayList<ReportingTree> branches = tree.getBranches();
if (branches != null) {
subChapters.hasSubUnits(true);
for (ReportingTree subTree: branches) {
subChapters.setSubChapters(recursiveSubChapters(subTree, linkedHashMap, false));
//This is where the logic of adding an object is being done. But it is being replaced instead.
}
alchildUnits.add(subChapters);
}
return alchildUnits;
}
My guess is that I am messing somewhere in the loop here but I am not able to figure out where I am messing up. Thanks in advance for any suggestions or help.
My subChapters class:
public String subChapterID;
public String subChapterName;
public boolean isSubTree= false;
public ArrayList<SubChapters> subChapters;
and getters and setters.
I have coded the same solution to return a string and see the order on a jsp. It works just fine. I am not able to apply the same to my issue here.
private String recursive(ReportingTree tree, LinkedHashMap<String, HashMap<String, String>> listUnitInfo, boolean isTop) {
final String unitID = tree.getUnitID();
final HashMap<String, String> unit = listUnitInfo.get(unitID);
String output = "";
if (!isTop) {
output += "<li>" + unit.get("unit_num") + "/" + unit.get("unit_name") + "";
}
final ArrayList<ReportingTree> branches = tree.getBranches();
if (branches != null) {
if (isTop) {
output += "<li>" + unit.get("unit_num") + "/" + unit.get("unit_name") + "";
}
output += "<ul>\n";
for (ReportingTree subTree : branches) {
output += recursive(subTree, listUnitInfo, false);
}
output += "</ul>";
} else {
if (isTop) {
output += "<li>No units match your criteria.";
}
}
output += "</li>\n";
return output;
}
What you're doing is subChapters.setSubChapters, what I think you're trying to do is
subChapters.addSubChapters.
The reason why it works with the strings is because you're using += to add
the new string to the old string. Doing setSubChapters would be the same as using = with the strings.
addSubChapters would be a method that should add something to an ArrayList variable inside your subChapters class.
Is it possible, to resolve mathematical functions dynamically, e.g by the use of a given API?
Given there is a function
a = b + c
is there a way to write something comparable to:
Function func = new Function("a=b+c");
Result result = func.resolve("a=1", "b=?", "c=a");
System.out.println(result.getValue());
Ideally, resolve(Object... args) should accept further classes of type Function.
EDIT 1: The API should be includable into a Java EE environment such as jBossAS.
EDIT 2: Actually I want to solve equations with one unknown variable, but I want to avoid hard coded functions. Thus, for the function
a+b=c
I don't want to write the functions hard coded
getA(int b, int c){...}
getB(int a, int c){...}
getC(int a, int b){...}
Instead, as #Berylium says, I want to evaluate the expression dynamically.
EDIT 3: I'm trying symja right now and I think I'm getting closer, but I have troubles with the syntax.
try {
F.initSymbols(null);
EvalUtilities util = new EvalUtilities();
StringBufferWriter buf = new StringBufferWriter();
String input = "....";
IExpr result = util.evaluate(input);
OutputFormFactory.get().convert(buf, result);
String output = buf.toString();
System.out.println("Evaluation for " + input + " is " + output);
} catch (final Exception e) {
Can you help me with the input syntax?
EDIT 4: Got it :-) For input
String input = "Solve[{a==10,c==20,a+b==c},{a,b,c}]";
the output is
Evaluation for Solve[{a==10,c==20,a+b==c},{a,b,c}] is {{a->10,c->20,b->10}}
Any embeddable Java scripting engine will do:
For example, using BeanShell:
final Interpreter interp = new Interpreter();
try {
interp.eval("System.out.println(\"Hello, world\");");
final String s = "Hello, world (2)";
interp.set("test", s);
interp.eval("System.out.println(test);");
System.out.println("3+4=" + interp.eval("3+4"));
interp.set("a", 4);
interp.set("b", 5);
System.out.println("a + b = " + interp.eval("a + b"));
final String script1 =
"public int f(int a) { return a * a; }; System.out.println(f(4));";
interp.eval(script1);
final String script2 =
"public int f(int a) { return a * a; }; f(4)";
System.out.println(interp.eval(script2));
final String script3 =
"import java.util.Date; Date date = new Date(); date";
System.out.println(interp.eval(script3));
final String script4 =
"class X { public int a; } X x = new X(); x.a = 5; x.a";
System.out.println(interp.eval(script4));
} catch (EvalError e) {
e.printStackTrace();
}
One advantage is that BeanShell uses Java syntax which is quite close to Java. So there is no need to learn/use another language/syntax.
After adding symja JAR to the build path, the following code prints the output below:
Code:
public static void main(String[] args) {
try {
F.initSymbols(null);
EvalUtilities util = new EvalUtilities();
StringBufferWriter buf = new StringBufferWriter();
String input = "Solve[{a==10,c==20,a+b==c},{a,b,c}]";
IExpr result = util.evaluate(input);
OutputFormFactory.get().convert(buf, result);
String output = buf.toString();
System.out.println("Evaluation for " + input + " is " + output);
} catch (final Exception e) {
e.printStackTrace();
} finally {
// Call terminate() only one time at the end of the program
ComputerThreads.terminate();
}
}
Output:
Evaluation for Solve[{a==10,c==20,a+b==c},{a,b,c}] is {{a->10,c->20,b->10}}
I am new to eclipse plugin development and I am trying to convert a IMethod to a string representation of the full method name. I.E.
my.full.package.ClassName.methodName(int param, String string)
so far I have had to hand roll my own solution. Is there a better way?
private static String getMethodFullName(IMethod iMethod)
{
String packageString = "[Default Package]";
try {
IPackageDeclaration[] declarations = iMethod.getCompilationUnit().getPackageDeclarations();
if(declarations.length > 0)
{
packageString = declarations[0].getElementName();
}
} catch (JavaModelException e) {
}
String classString = iMethod.getCompilationUnit().getElementName();
classString = classString.replaceAll(".java", "");
String methodString = iMethod.getElementName() + "(";
for (String type : iMethod.getParameterTypes()) {
methodString += type + ",";
}
methodString += ")";
return packageString + "." + classString + "." + methodString;
}
You can get the Fully qualified name for the type using
method.getDeclaringType().getFullyQualifiedName();
This is probably easier than accessing the package from the compilation unit. The rest of you function looks correct.
One small point: you should use StringBuilder to build up the string instead of adding to a standard String. Strings are immutable so addition creates loads of unrecesary temparary objects.
private static String getMethodFullName(IMethod iMethod)
{
StringBuilder name = new StringBuilder();
name.append(iMethod.getDeclaringType().getFullyQualifiedName());
name.append(".");
name.append(iMethod.getElementName());
name.append("(");
String comma = "";
for (String type : iMethod.getParameterTypes()) {
name.append(comma);
comma = ", ";
name.append(type);
}
name.append(")");
return name.toString();
}
Thanks to iain and some more research I have come up with this solution. It seems like something like this should be built into the JDT....
import org.eclipse.jdt.core.Signature;
private static String getMethodFullName(IMethod iMethod)
{
StringBuilder name = new StringBuilder();
name.append(iMethod.getDeclaringType().getFullyQualifiedName());
name.append(".");
name.append(iMethod.getElementName());
name.append("(");
String comma = "";
String[] parameterTypes = iMethod.getParameterTypes();
try {
String[] parameterNames = iMethod.getParameterNames();
for (int i=0; i<iMethod.getParameterTypes().length; ++i) {
name.append(comma);
name.append(Signature.toString(parameterTypes[i]));
name.append(" ");
name.append(parameterNames[i]);
comma = ", ";
}
} catch (JavaModelException e) {
}
name.append(")");
return name.toString();
}
I am not sure it would take into account all cases (method within an internal class, an anonymous class, with generic parameters...)
When it comes to methods signatures, the classes to look into are:
org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedMethodsOperation
org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2
You need to get the jdt.core.dom.IMethodBinding, from which you can extract all what you need.
If you have a MethodInvocation, you can:
//MethodInvocation node
ITypeBinding type = node.getExpression().resolveTypeBinding();
IMethodBinding method=node.resolveMethodBinding();