display Multidimensional Arrays android - android

I'm using android studio and with opencv 3.4.0. I want to display a Multidimensional Array in text view. I have an a array "candidats_result" and I copied her values into a Multidimensional array " finale". after runing my code I got this result in emulator display. what I have to change in my code to get the text displayed
double [][] R_finale = new double[20][20];
int ZZ = 0;
int ZE = 0;
int EE = 0;
for(int i=0;i<19;i++){
for(int j=0;j<19;j++){
R_finale[i][j] = candidats_result[ZZ];
ZZ++;
}
}
String [][] finale = new String [20][20];
//showing the array in android
for(int i=0;i<19;i++) {
for (int j = 0; j < 19; j++) {
finale[i][j] = Double.toString(R_finale[i][j]);
}
}
String [][] details = new String[20][20];
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";");
}
}
textView.setText(String.valueOf(builder));

Notice below lines:
for ( int i = 0 ; i<20;i++) {
for (int j = 0; j < 20; j++) {
details[i][j] = String.valueOf(R_finale [i][j]);
builder.append(details + ";"); //should be builder.append(details[i][j] + ";");
}
}

Related

Filter number from Array of string and get the index of last two greater number in android

I have a ArrayList that has value like [Value,Sum3,121,data8input,in:21::7,7.00,9.01] and I want to extract only number as the output should be like this [3,121,8,21,7,7.00,9.01] and then have to rearrange ascending and then get the index of last two number as result will be [21,121].
My tried code below,
for (int i = 0; i < arrayString.size(); i++) {
Pattern p = Pattern.compile("-?\\d+(,\\d+)*?\\.?\\d+?");
List<String> numbers = new ArrayList<String>();
Matcher m = p.matcher(arrayString.get(i).getvalue);
numbers.addAll(m);
for (int j = 0; j < numbers.size(); j++) {
Log.d("REMEMBERFILTER", allCollection.get(i).getTextValue());
}
}
}
do something like this, though it is not exactly memory efficient as I am using another list.
ArrayList<String> tempList = new ArrayList<>();
for (int i = 0; i < yourArrayList.size(); i++) {
tempList.add(yourArrayList.get(i).replaceAll("[^0-9]", ""));
}
//Arrange in ascending order
Collections.sort(tempList);
//Also try to remove those indexes which has only letters with
tempList.removeAll(Arrays.asList("", null));
for (int i = 0; i < tempList.size(); i++) {
Log.d("+++++++++", "" + tempList.get(i));
}
//You can get the last two or any element by get method of list by //list.size()-1 and list.size()-2 so on
This is a way to do it, finalArray has the 2 numbers you want:
String[] str = new String[] {"Value", "Sum3", "121", "data8input", "in:21::7", "7.00,9.01"};
StringBuilder longStringBuilder = new StringBuilder();
for (String s : str) {
longStringBuilder.append(s).append(" ");
}
String longString = longStringBuilder.toString();
String onlyNumbers = " " + longString.replaceAll("[^0-9.]", " ") + " ";
onlyNumbers = onlyNumbers.replaceAll(" \\. ", "").trim();
while (onlyNumbers.indexOf(" ") > 0) {
onlyNumbers = onlyNumbers.replaceAll(" ", " ");
}
String[] array = onlyNumbers.split(" ");
Double[] doubleArray = new Double[array.length];
for (int i = 0; i < array.length; i++) {
try {
doubleArray[i] = Double.parseDouble(array[i]);
} catch (NumberFormatException e) {
e.printStackTrace();
doubleArray[i] = 0.0;
}
}
Arrays.sort(doubleArray);
int numbersCount = doubleArray.length;
Double[] finalArray;
if (numbersCount >= 2) {
finalArray = new Double[]{doubleArray[numbersCount - 2], doubleArray[numbersCount - 1]};
} else if (numbersCount == 1) {
finalArray = new Double[]{ doubleArray[0]};
} else {
finalArray = new Double[]{};
}
for (Double number : finalArray) {
System.out.println(number);
}

Create a arraylist from output of a for loop

I am trying to get a data set as [{a,b,c,d} {e,f,g,h}] but I'm getting it as [a,b,c,d,e,f,g,h] using below mentioned code. What should I do to achieve that. any help will be highly appreciated. I'm new to android so please be kind enough to answer me. thanks in advance
List<String> CartItemEntityList = new ArrayList<String>();
for (int i = 0; i < mCartList.size(); i++) {
if (!mCartList.isEmpty()) {
Product product = mCartList.get(i);
int quantity = product.quantity;
double subTotal = product.subTotal;
CartItemEntity = product.items;
for (int j = 0; j < CartItemEntity.size(); j++) {
Item item = CartItemEntity.get(j);
subMenuCode = item.subMenuCode;
mainMenuCode = item.mainMenuCode;
price = item.price;
CartItemEntityList.add(mainMenuCode);
CartItemEntityList.add(String.valueOf(price));
CartItemEntityList.add(String.valueOf(quantity));
CartItemEntityList.add(subMenuCode);
}
}
System.out.println("CartItemEntityList " + CartItemEntityList);
}
I was able to achieve this using json serialization and this is my answer
JSONObject jsonObj = new JSONObject();
jsonObj.put("MainMenuCode",
item.getMainMenuCode());
jsonObj.put("Price", item.getPrice());
jsonObj.put("Quantity", product.getQuantity());
jsonObj.put("SubMenuCode",
item.getSubMenuCode());
String a = jsonObj.toString();
CartIddtemEntityList.add(a);
you have to create array of arraylist.
Try something like below:
ArrayList<ArrayList<String>> group = new ArrayList<ArrayList<String>>(mCartList.size());
for (int i = 0; i < mCartList.size(); i++) {
List<String> CartItemEntityList = new ArrayList<String>();
if (!mCartList.isEmpty()) {
Product product = mCartList.get(i);
int quantity = product.quantity;
double subTotal = product.subTotal;
CartItemEntity = product.items;
for (int j = 0; j < CartItemEntity.size(); j++) {
Item item = CartItemEntity.get(j);
subMenuCode = item.subMenuCode;
mainMenuCode = item.mainMenuCode;
price = item.price;
CartItemEntityList.add(mainMenuCode);
CartItemEntityList.add(String.valueOf(price));
CartItemEntityList.add(String.valueOf(quantity));
CartItemEntityList.add(subMenuCode);
}
}
System.out.println("CartItemEntityList " + CartItemEntityList);
group.add(CartItemEntityList);
}

for loop overextending index

I'm attempting to iterate through the EditText "name" and check the letters so see if they match any of the letters in the objects of the array uNamesList.
If they do I want to break out of the loop and return the placement of the object that had the matching letter. At the moment I am getting this error, anyone know what might be wrong?
java.lang.StringIndexOutOfBoundsException: length=9; index=9
uNamesList.add("bob");
uNamesList.add("mike");
uNamesList.add("sike");
uNamesList.add("othername");
uNamesList.add("name");
public int getName(EditText name) {
String text = name.getText().toString();
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
match = o;
break;
}
}
}
return match;
}
You get the text inside edit text in wrong way. it should :
public int getName(EditText name) {
String text = name.getText(); //get the text
int match = 10;
for (int i = 0; i < text.toString().length(); i++) { //get the length of the text
for (int o = 0; o < uNamesList.size(); o++) {
char cLetter = name.toString().charAt(i);
String uName = (String)uNamesList.get(o);
Okay, here's the edit for another problem. i dont know if this what you want.
public int getName(EditText name) {
int match = 9999;
String text = name.getText().toString();
boolean found = false;
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
match = o;
found = true;
break;
}
}
if(found) break;
}
return match;
}
The simplest way is just only call return when it found.
public int getName(EditText name) {
int match = 9999;
String text = name.getText().toString();
for (int i = 0; i < text.length(); i++) { //i = current letter in text
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
String uName = (String)uNamesList.get(o);
char uLetter = uName.charAt(i);
if (cLetter == uLetter) {
return o;
}
}
}
return match;
}
change your for loop to the following, you accessing name from the uNameList loop where the length might be different
for (int i = 0; i < name.toString().length(); i++) {
char cLetter = name.toString().charAt(i);
for (int o = 0; o < uNamesList.size(); o++) {
For EditText you need GetText()
name.toString() gives you the java desciption of the object EditText
You are getting a StringIndexOutOfBoundsException most likely due to this line:
char uLetter = uName.charAt(i);
i in this case can be greater than the length of uName

How to sort/order JSON output?

I have a JSON loop and I am able to get objects in the loop, but the problem is my output is not in order:
My expected output is:
Menu Id 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23
but I get results like:
Menu Id: 19,18,23,16,17,14,15,12,13,21,20,22,4,3,11,2,1.
How can I sort this the way I expect?
JSONArray values = menuObject.toJSONArray(names);
for (int i = 0; i< values.length(); i++) {
JSONObject json2 = (JSONObject) values.getJSONObject(i);
//int menu_id = json2.getInt("menu_id");
menu_id = json2.getString("menu_id");
int m_id = Integer.parseInt(menu_id);
if (json2.has("menu_parent")) {
menu_parent = json2.getString("menu_parent");
}
if (m_id < 0) {
//
} else {
id = id + menu_id + ",";
int menu_category = Integer.parseInt(menu_parent);
System.out.println("Menu Id" + id);
if (json2.has("menu_name")) {
menu_list = json2.getString(KEY_MENU).trim();
System.out.println("Menu List" +menu_title);
menu_title = menu_title + menu_list + ",";
}
}
}
I think you can get it sorted this way:
Collections.sort("your data", new Comparator<Item>() {
public int compare(Item i1, Item i2) {
return i1.getCaption().compareTo(i2.getCaption());
}
});
After this is done, it should be sorted out.
Try this:
String menuids = "19,18,23,16,17,14,15,12,13,21,20,22,4,3,11,2,1";
String[] ids = menuids.split(",");
Integer[] result = new Integer[ids.length];
for (int i = 0; i < ids.length; i++) {
result[i] = Integer.parseInt(ids[i].trim());
}
Collections.sort(Arrays.asList(result));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.length; i++) {
Integer intValue = result[i];
sb.append(intValue);
if (i < result.length - 1) {
sb.append(", ");
}
}
String finaldata = sb.toString();
System.out.println(finaldata);

How to display high value from array calculation

I'm trying to build a project expert system with a Certainty factor method in android, but I'm having trouble when displaying the highest value inputted from the user.
here is my code:
public void diagn() {
for (int i = 0; i < listCheckBox.size(); i++) {
if (listCheckBox.get(i).isChecked()) {
idGejala.add(listGejala.get(i).getId_gejala());
}
}
for (int i = 0; i < idGejala.size(); i++) {
Cursor kursor = db.getNilaiCFdanIDpenyakit(idGejala.get(i));
while (!kursor.isAfterLast()) {
Kaidah kaidahku = new Kaidah();
kaidahku.setId_penyakit(kursor.getString(0));
kaidahku.setNilai_CF(kursor.getDouble(1));
kaidah.add(kaidahku);
kursor.moveToNext();
}
}
Penyakit penyakit;
for (int i = 0; i < listPenyakit.size(); i++) {
penyakit = listPenyakit.get(i);
CfPenyakit cfpenyakit = new CfPenyakit();
cfpenyakit.setId_penyakit(penyakit.getId_penyakit());
cfpenyakit.setNama_penyakit(penyakit.getNama_penyakit());
for (int j = 0; j < kaidah.size(); j++) {
Kaidah kaidahku = kaidah.elementAt(j);
if (penyakit.getId_penyakit().equals(kaidahku.getId_penyakit())) {
cfpenyakit.addCF(kaidahku.getNilai_CF());
}
}
if (cfpenyakit.getFinalCF() > 0)
cf.add(cfpenyakit);
}
String[] penyakitku = new String[cf.size()];
String[] cfku = new String[cf.size()];
String[] idPenyakit = new String[cf.size()];
for (int i = 0; i < cf.size(); i++) {
CfPenyakit data = cf.elementAt(i);
penyakitku[i] = data.getNama_penyakit();
idPenyakit[i] = data.getId_penyakit();
double orim = (data.getFinalCF()*100);
cfku[i] = Double.toString(data.getFinalCF() * 100);
cfku[i] = String.format("%.2f", orim) + "%";
}
db.close();
Intent intent = new Intent(this, Diag2.class);
intent.putExtra("penyakit", penyakitku);
intent.putExtra("cf", cfku);
intent.putExtra("idPenyakit", idPenyakit);
startActivity(intent);
finish();
}

Categories

Resources