I have something like this :
List<Page> result = new ArrayList<Page>();
Page is a class with 3 string variables;
I have an array as such :
List<String[]> output = new ArrayList<String[]>();
Which is populated like this in a loop:
String[] out = new String[3];
out[0] = "";
out[1] = "";
out[2] = "";
then added to output: output.set(i, out);
How can I assign output (type:String) to result (type:Page)?
I am guessing you are looking for something like this (code requires Java 8 but can be easily rewritten for earlier versions using loop)
List<String[]> output = new ArrayList<String[]>();
// populate output with arrays containing three elements
// which will be used used to initialize Page instances
//...
List<Page> result = output.stream()
.map(arr -> new Page(arr[0], arr[1], arr[2]))
.collect(Collectors.toList());
Related
I have below string output :
["Kolkata","data can be, null",null,"05/31/2020",null]
but I want to have the output like below format in Java
["Kolkata","data can be, null","","05/31/2020",""]
please help me .
I am converting object to json data . Please see the below codes
List<String> test = new ArrayList<>();
List<Object[]> data =query.list();
for (int i = 0; i < data.size(); i++) {
Object[] row = (Object[]) data.get(i);
String jsonString = gson.toJson(row);
test.add(jsonString);
}
I want to apply this on jsonString variable using java 7 as not using java 8
If you have list for example list of like this
List<String> list = Arrays.asList("Kolkata","data can be, null",null,"05/31/2020",null);
list.replaceAll(t -> Objects.isNull(t) ? "''" : t);
System.out.println(list);
Here oputput will be:
[Kolkata, data can be, null, '', 05/31/2020, '']
I am attempting to populate a dropdown from the controller by passing the iteration value to a list. The iteration is done successfully but I keep getting a type mismatch error
List<Skills> mskills = skillsService.getAll();
for(Skills skills : mskills ){
String nameVal = skills.getName();
List<String> matchName = nameVal; //having an issue here
}
return matchName;
how can pass the value of nameVal to the matchName. Kindly assist
There are a few mistakes. matchName list is not visible out of the for loop. Further more, you can't assign a String to a List reference.
Replace your code with:
List<Skills> mskills = skillsService.getAll();
List<String> matchName = new ArrayList<String>();
for(Skills skills : mskills ){
String nameVal = skills.getName();
matchName.add(nameVal); //having an issue here
}
return matchName;
You're trying to assign a String object to a List<String> object. You need to create a list and then add elements to it.
List<Skills> matchName = new ArrayList<>();
List<Skills> mSkills = skillService.getAll();
for(Skill s : mSkills) {
String nameVal = skills.getName();
matchName.add(nameVal);
}
return matchName;
Try this:
List<String> matchName = new ArrayList<String>();
for(Skills skills : mskills ){
String nameVal = skills.getName();
matchName.add(nameVal); //having an issue here
}
return matchName;
I want to save data into master - details table.First portion is for master table and last portion is for details table.I have got java.lang.String cannot be cast to [Ljava.lang.String .How to recover from this problem.How to assign map.get("step_id[]") into a string array String[] WfIds. I want to assign each value into distinct string array.
Controller Code
Map<String,Object> wfManager = new HashMap<String,Object>();
//************************Master data sent from view******************************//
wfManager.put("workflow_code",(request.getParameter("workflow_code")).toUpperCase());
wfManager.put("workflow_name",request.getParameter("workflow_name"));
wfManager.put("workflow_descr",request.getParameter("workflow_descr"));
wfManager.put("object_type_code",request.getParameter("object_type_code"));
//*********************Detail item data sent from view********************************//
wfManager.put("wf_block_id[]", request.getParameter("wf_block_id[]"));
wfManager.put("step_code[]" , request.getParameter("step_code[]"));
wfManager.put("step_name[]", request.getParameter("step_name[]"));
wfManager.put("doa_type_code[]", request.getParameter("doa_type_code[]"));
wfManager.put("doa_type_name[]", request.getParameter("doa_type_name[]"));
Service Code
public Map<String, String> insert(Map<String, Object> map) {
//************************Master data sent from view******************************//
Map<String, String> data = new HashMap<String, String>();
Workflow wf = new Workflow();
wf.setWorkflowCode((String)map.get("workflow_code"));
wf.setWorkflowName((String)map.get("workflow_name"));
wf.setWorkflowDescr((String)map.get("workflow_descr"));
wf.setObjectTypeCode((String)map.get("object_type_code"));
String[] WfIds = (String[]) map.get("step_id[]");
String[] wfBlockIds = (String[]) map.get("wf_block_id[]");
String[] wfsCodes = (String[]) map.get("step_code[]");
String[] stepNames = (String[]) map.get("step_name[]");
String[] doaTypeCodes = (String[]) map.get("doa_type_code[]");
String[] doaTypeNames = (String[]) map.get("doa_type_name[]");
List<WorkflowDetails> wfDetailsList = new ArrayList<WorkflowDetails>();
for(int i = 0; i< wfsCodes.length; i++){
WorkflowDetails wfDetails = new WorkflowDetails();
wfDetails.setWorkflowCode(wf.getWorkflowCode());
wfDetails.setWorkflowName(wf.getWorkflowName());
wfDetails.setWorkflowDescr(wf.getWorkflowDescr());
wfDetails.setWorkflowObjectTypeCode(wf.getObjectTypeCode());
wfDetails.setWorkflowObjectTypeName(wf.getObjectTypeName());
wfDetailsList.add(i,wfDetails);
}
wf.setSteps(wfDetailsList);
id = workflowManagerDAO.insertDoc(wf);
data.put("id", id);
return data;
}
Code for DAO:
#Transactional
#Override
public String insertDoc(Workflow wfManager) {
for(int i = 0; i < wfManager.getSteps().size(); i++){
WorkflowDetails wfDetails = new WorkflowDetails();
wfDetails = wfManager.getSteps().get(i);
sessionfactory.getCurrentSession().save(wfDetails);
sessionfactory.getCurrentSession().flush();
}
String id = (String) sessionfactory.getCurrentSession().save(wfManager);
sessionfactory.getCurrentSession().flush();
return id;
}
If you absolutely need to use request.getParameter(), you will have to convert your arrays to strings using a delimiter character, e.g. convert this
String[] array = { "John", "Peter", "Paul" };
to this
String plainTextArray = "John#Peter#Paul";
Then you will be able to pass you array values as String (the only type that getParameter() understands).
You can then restore them like this
String[] restoredArray = request.getParameter("plainTextArray").split("#");
Maybe you want to have a look at setAttribute() and getAttribute() which let you store any objects (including arrays). You can start here Difference between getAttribute() and getParameter()
I have to get a part of my content provider query in a String[] format. Currently I use:
String[] selectionArgs = {"",""};
selectionArgs[0] = Integer.toString(routeScheduleID);
selectionArgs[1] = runDate.toString();
But in this case I have an unknown number of elements.
How can I change the number at runtime (or use something like an Array and convert back to String[]. Is this possible?
You can use List<String> to get your data and then get the array out of it:
List<String> lst = new ArrayList<String>();
lst.add(Integer.toString(routeScheduleID);
lst.add(runDate.toString());
lst.add(...);
...
String[] selectionArgs = lst.toArray(new String[lst.size()]);
You can use List for this. A List of Strings like this - List<String> str = new ArrayList<String>();
You can use a list to populate the data then convert the list into an array:
List<String> list = new ArrayList<String>();
list.add(item1);
list.add(item2);
...
String[] array = list.toArray(new String[0]);
List<String> selectionArgs = new ArrayList<String>();
selectionArgs.add(Integer.toString(routeScheduleID));
selectionArgs.add(runDate.toString());
selectionArgs.add(...).
...........
String[] array= selectionArgs.toArray(new String[selectionArgs.size()]);
List<String> selectionArgsList = new ArrayList<String>();
selectionArgsList.add("string1");
selectionArgsList.add("string2");
String[] selectionArgs = new String[selectionArgsList.length];
selectionArgsList.toArray(selectionArgs);
I'm using a simple php API (that I wrote) that returns a JSON formatted string such as:
[["Air Fortress","5639"],["Altered Beast","6091"],["American Gladiators","6024"],["Bases Loaded II: Second Season","5975"],["Battle Tank","5944"]]
I now have a String that contains the JSON formatted string but need to convert it into two String arrays, one for name and one for id. Are there any quick paths to accomplishing this?
You can use the org.json library to convert your json string to a JSONArray which you can then iterate over.
For example:
String jsonString = "[[\"Air Fortress\",\"5639\"],[\"Altered Beast\",\"6091\"],[\"American Gladiators\",\"6024\"],[\"Bases Loaded II: Second Season\",\"5975\"],[\"Battle Tank\",\"5944\"]]";
List<String> names = new ArrayList<String>();
List<String> ids = new ArrayList<String>();
JSONArray array = new JSONArray(jsonString);
for(int i = 0 ; i < array.length(); i++){
JSONArray subArray = (JSONArray)array.get(i);
String name = (String)subArray.get(0);
names.add(name);
String id = (String)subArray.get(1);
ids.add(id);
}
//to convert the lists to arrays
String[] nameArray = names.toArray(new String[0]);
String[] idArray = ids.toArray(new String[0]);
You can even use a regex to get the job done, although its much better to use a json library to parse json:
List<String> names = new ArrayList<String>();
List<String> ids = new ArrayList<String>();
Pattern p = Pattern.compile("\"(.*?)\",\"(.*?)\"") ;
Matcher m = p.matcher(s);
while(m.find()){
names.add(m.group(1));
ids.add(m.group(2));
}