I need to convert a string array to double array in android. But my app is not getting executed on my device. It shows unfortunately stopped.Following is my code:
String dataa="0.121 0.547 0.875 0.245";
String delimiter = " ";
String spectrainstring[] = dataa.split(delimiter);
int size = spectrainstring.length;
double[] spectraldata={0.0};
for(int i=0;i<size;i++)
{
spectraldata[i]=Double.parseDouble(spectrainstring[i].toString());
}
String dataa = "0.121 0.547 0.875 0.245";
String delimiter = " ";
String spectrainstring[] = dataa.split(delimiter);
int size = spectrainstring.length;
double[] spectraldata = new double[size];
for (int i = 0; i < size; i++) {
spectraldata[i] = Double.parseDouble(spectrainstring[i].toString());
System.out.println(spectraldata[i]);
}
Related
I'm creating a class called "partite", and this is the code
public class Partita {
String HT; //HomeTeam
String AT; //AwayTeam
String dataP; //dataPartita
int HG; //HomeGoal
int AG; //AwayGoal
String FTR; //Full time result
public Partita(){
this.HT = "";
this.AT = "";
this.dataP = "";
this.HG = 0;
this.AG = 0;
this.FTR = "";
}
public Partita(String HT, String AT, String dataP, int HG, int AG, String FTR) {
this.HT = HT;
this.AT = AT;
this.dataP = dataP;
this.HG = HG;
this.AG = AG;
this.FTR = FTR;
}
}
In the main activity I'm creating an ArrayList, putting a list of some "Partita" object, with attributes come from a json file, and then I create an ArrayMap and put the ArrayList inside, like this
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
partite.add(new Partita(jsonPart.getString("HomeTeam"),
jsonPart.getString("AwayTeam"),jsonPart.getString("Date"),
jsonPart.getInt("FTHG"), jsonPart.getInt("FTAG"),
jsonPart.getString("FTR")));
partitemap.put(i, partite.get(i));
Log.i("partita", partite.get(i).HT + " " + partite.get(i).HG + ":" + partite.get(i).AG + " " + partite.get(i).AT);
}
How can I use the ArrayMap instead of ArrayList to get the attributes of an object and use it?
Use this instead (edited):
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
partitemap.put(i, new Partita(jsonPart.getString("HomeTeam"),
jsonPart.getString("AwayTeam"),
jsonPart.getString("Date"),
jsonPart.getInt("FTHG"),
jsonPart.getInt("FTAG"),
jsonPart.getString("FTR")
)
);
Log.d("tag", partitemap.get(i).HT);
}
I want to get one array from PHP that one of the fileds is an array.
How can get basket's data with jsonArray and jsonObject ?
(In the code below, the basket is an array that contains 5 parameters).
It's my array:
[{"orderCode":11514,"orderDate":"2017/05/21","orderPrice":"19200","fullName":"Jack","address":"addr 1","cellphone":"09151515730","basket":[{"b_qty":"4","pid":"8","b_price":"9500","b_discount":"10","title":"obj1"}]
Edit
String b_price ="";
String b_discount="";
String b_qty ="";
String title= "";
String pid="";
try
{
JSONArray jsonArray = new JSONArray(data);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String orderCode = object.getString("orderCode");
String orderDate = object.getString("orderDate");
String orderPrice = object.getString("orderPrice");
String fullName = object.getString("fullName");
String address = object.getString("address");
String cellphone = object.getString("cellphone");
JSONArray orderBasket = object.getJSONArray("basket");
for (int j = 0; j < orderBasket.length(); j++){
JSONObject object1 = orderBasket.getJSONObject(j);
b_qty = object1.getString("b_qty");
pid = object1.getString("pid");
b_price = object1.getString("b_price");
b_discount = object1.getString("b_discount");
title = object1.getString("title");
}
CustomOrderList customOrderList = new CustomOrderList(getApplicationContext());
customOrderList.orderCode.setText(orderCode);
customOrderList.orderDate.setText(orderDate);
customOrderList.orderTotalPrice.setText(orderPrice);
customOrderList.orderFullName.setText(fullName);
customOrderList.orderAddress.setText(address);
customOrderList.orderCellphone.setText(cellphone);
customOrderList.basketPrice.setText(b_price);
customOrderList.basketDiscount.setText(b_discount);
customOrderList.basketTitle.setText(title);
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearOrders.addView(customOrderList);
}
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String orderCode = object.getString("orderCode");
String orderDate = object.getString("orderDate");
String orderPrice = object.getString("orderPrice");
String fullName = object.getString("fullName");
String address = object.getString("address");
String cellphone = object.getString("cellphone");
//Now orderBasket is jsonArray
JSONArray orderBasket = object.getJSONArray("basket");
// So fetch all objects from orderBasket array
for (int j = 0; j < orderBasket.length(); j++){
JSONObject objectbsk = orderBasket.getJSONObject(j);
String b_qty = objectbsk.getString("b_qty");
String pid = objectbsk.getString("pid");
String b_price = objectbsk.getString("b_price");
String b_discount = objectbsk.getString("b_discount");
String title = objectbsk.getString("title");
// Create a orderBasket list for Basket with these keys also as you have created for CustomOrderList and use it
}
CustomOrderList customOrderList = new CustomOrderList(getApplicationContext());
customOrderList.orderCode.setText(orderCode);
customOrderList.orderDate.setText(orderDate);
customOrderList.orderTotalPrice.setText(orderPrice);
customOrderList.orderFullName.setText(fullName);
customOrderList.orderAddress.setText(address);
customOrderList.orderCellphone.setText(cellphone);
customOrderList.orderBasket.setText(orderBasket);
// here you can't do set text since it's jsonarray. So use basketlist to show the data`
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearOrders.addView(customOrderList);
}
So, I have at this point a collections.sort of java values as you can see
and I have two keys that are integers (let's say for the sake of the example that the values of tipo are 1,2 and the values of id are 3 and 4) and I want to sort the result of theyr multiplication:
Something like this:
valA = a.get(KEY_ONE)*a.get(KEY_TWO);
valB = b.get(KEY_ONE)*b.get(KEY_TWO);
Then compare them.
How can I do it??
here is the code that I have at this point.
Collections.sort( jsonValues, new Comparator<JSONObject>() {
private static final String KEY_ONE = "tipo";
private static final String Key_TWO = "id";
#Override
public int compare(JSONObject a, JSONObject b) {
String valA = new String();
String valB = new String();
try {
valA = (String) a.get(KEY_ONE.toString());
valB = (String) b.get(KEY_ONE.toString());
}
catch (JSONException e) {
//do something
}
return valA.compareTo(valB);
}
});
for (int i = 0; i < jsonArray.length(); i++) {
sortedJsonArray.put(jsonValues.get(i));
}
tvJson.setText(sortedJsonArray.toString());
}
}
Thanks in advance !
i have string like these for example
309\306\308\337_338
309\306\337_338
310
311\315_316\336_337
311\315_316\336_337
311\335_336
these strings means list of page number , for example string "309\306\308\337_339" means
pages 309,306,308,337,338,339
i want to pass one of these string to function which return it as string like this
309,306,308,337,338,339
this function do that but in c# , i want to impalement in android
private static string Get_PageNumbers(string str)
{
ArrayList arrAll = new ArrayList();
MatchCollection match;
string[] excar;
string strid, firstNumber, lastlNumber;
int fn, ln;
ArrayList arrID = new ArrayList();
//***In Case The Range Number Between "_"
if (str.Contains("_"))
{
// match_reg = new Regex("(w?[\\d]+)*(_[\\d]+)");
Regex matchReg = new Regex("(w?[\\69]+_[\\d]+)*(q?[\\d]+//)*(a?[\\d]+_[\\d]+)*(y?[\\d]+)*");
match = matchReg.Matches(str);
int count = match.Count;
excar = new string[0];
for (int i = 0; i < count; i++)
{
Array.Resize(ref excar, count);
excar[i] = match[i].Groups[0].Value;
if (excar[i] != string.Empty)
arrID.Add(excar[i]);
}
//******IF Array Contains Range Of Number Like"102_110"
if (str.Contains("_"))
{
for (int i = 0; i < arrID.Count; i++)
{
strid = arrID[i].ToString();
if (arrID[i].ToString().Contains("_"))
{
int idy = strid.LastIndexOf("_");
firstNumber = strid.Substring(0, idy);
if (idy != -1)
{
lastlNumber = strid.Substring(idy + 1);
fn = int.Parse(firstNumber);
arrAll.Add(fn);
ln = int.Parse(lastlNumber);
for (int c = fn; c < ln; c++)
{
fn++;
arrAll.Add(fn);
}
}
}
else
{
arrAll.Add(arrID[i].ToString());
}
}
//******If Array Contain More Than One Number
if (arrAll.Count > 0)
{
str = "";
for (int i = 0; i < arrAll.Count; i++)
{
if (str != string.Empty)
str = str + "," + arrAll[i];
else
str = arrAll[i].ToString();
}
}
}
}
//***If string Contains between "/" only without "_"
else if (str.Contains("/") && !str.Contains("_"))
{
str = str.Replace("/", ",");
}
else if (str.Contains("\\"))
{
str = str.Replace("\\", ",");
}
return str;
}
I think this is easier to do with split function:
public static String Get_PageNumbers(String str) {// Assume str = "309\\306\\308\\337_338"
String result = "";
String[] pages = str.split("\\\\"); // now we have pages = {"309","306","308","337_338"}
for (int i = 0; i < pages.length; i++) {
String page = pages[i];
int index = page.indexOf('_');
if (index != -1) { // special case i.e. "337_338", index = 3
int start = Integer.parseInt(page.substring(0, index)); // start = 337
int end = Integer.parseInt(page.substring(index + 1)); // end = 338
for (int j = start; j <= end; j++) {
result += String.valueOf(j);
if (j != end) { // don't add ',' after last one
result += ",";
}
}
} else { // regular case i.e. "309","306","308"
result += page;
}
if (i != (pages.length-1)) { // don't add ',' after last one
result += ",";
}
}
return result; // result = "309,306,308,337,338"
}
For example this function when called as follows:
String result1 = Get_PageNumbers("309\\306\\308\\337_338");
String result2 = Get_PageNumbers("311\\315_316\\336_337");
String result3 = Get_PageNumbers("310");
Returns:
309,306,308,337,338
311,315,316,336,337
310
if i can suggest different implementation....
first, split string with "\" str.split("\\");, here you receive an array string with single number or a pattern like "num_num"
for all string founded, if string NOT contains "" char, put string in another array (othArr named), than, you split again with "" str.split("_");, now you have a 2 position array
convert that 2 strings in integer
now create a loot to min val form max val or two strings converted (and put it into othArr)
tranform othArr in a string separated with ","
hi to all i have this code have this code which reads a some text and it extracts any strings between the '[' and ']' and it should print it on the screen
String lines[] = {addressString};
for (int i = 0; i < lines.length; i++) {
int be = lines[i].indexOf('[');
int e = lines[i].indexOf(']');
String fields = lines[i].substring(be+1, e);
}
my question is that i want to change the string "fields" to an array string so when i print it
i can print it as fields[0],fields[1],....etc until the end of the text....?
any suggestions...??
Thanks a lot
Is this what you mean?
String lines[] = {addressString};
String fields[] = new String[lines.length];
for (int i = 0; i < lines.length; i++) {
int be = lines[i].indexOf('[');
int e = lines[i].indexOf(']');
fields[i] = lines[i].substring(be+1, e);
}