How to display high value from array calculation - android

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();
}

Related

Getting repeating loop infinit

I am using nested for loop for getting from arraylist and that loop run infinte
private ArrayList prepareData()
{
ArrayList arrayList = new ArrayList<>();
for(int i=0;i<android_image_urls.length;i++){
Androidversionclass androidversion = new Androidversionclass();
androidversion.setAndroid_image_url(android_image_urls[i]);
arrayList.add(androidversion);
for (int k= 0; k < android_image_second.length; k++) {
Androidversionclass androidversionclass1 = new Androidversionclass();
androidversion.setAndroid_image_second(android_image_second[k]);
arrayList.add(androidversion);
}
for (int l = 0; l < android_image_thirds.length; l++) {
Androidversionclass androidversionclass2 = new Androidversionclass();
androidversion.setAndroid_image_thirds(android_image_thirds[l]);
arrayList.add(androidversion);
}
for (int m = 0; m < android_image_fourth.length; m++) {
Androidversionclass androidversionclass3 = new Androidversionclass();
androidversion.setAndroid_image_fourth(android_image_fourth[m]);
arrayList.add(androidversion);
}
}
return arrayList;
}

display Multidimensional Arrays 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] + ";");
}
}

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);
}

getIntent().getStringArrayListExtra(NAME) return null if the NAME doesn't equal "Scores"

I have really really strange problem.
Activity4:
protected void ToActivity5() {
Index = 0;
index = 0;
ArrayList<String> scores = new ArrayList<>();
Bundle b = new Bundle();
for(int i = 0; i < Count; i++) {
scores.add(i, Integer.toString((int) (score[i] * 100.0)) + "%");
}
Intent intent = new Intent(Activity4.this, Activity5.class);
b.putStringArrayList("Score", scores);
b.putBoolean("Second", second);
if(!second) {
for(int i = 0; i < tpairs.size(); i++)
for(int j = 0; j < tpairs.get(i).size(); j++)
pairs.add(tpairs.get(i).get(j));
b.putParcelableArrayList("TPairs", pairs);
}
for(int i = 0; i < 7; i++)
score[i] = 0.0;
//if(second)
//finish();
second = true;
intent.putExtras(b);
startActivity(intent);
}
Activity5:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_5);
setTitle(R.string.title_5);
Intent intent = getIntent();
ArrayList<String> percents = intent.getStringArrayListExtra("Score");
ArrayList<TwoStrings> arrayList = new ArrayList<>();
for(int i = 0; i < percents.size(); i++)
arrayList.add(i, new TwoStrings(getResources().getTextArray(R.array.StatisticDays)[i].toString(), percents.get(i)));
//tpairs = getIntent().getParcelableArrayListExtra("TPairs");
//tpairs.size();
//ArrayList<String> ssd = intent.getStringArrayListExtra("Scores");
lv5_1 = (ListView)findViewById(R.id.lv5_1);
TwoStringsAdapter adapter = new TwoStringsAdapter(this, R.layout.layout_5_1, arrayList);
lv5_1.setAdapter(adapter);
}
And the problem is that when I put something with "Score" name then I can get it in the next activity but if the name is different e.g. "Second" or whatever then I can't get the object in Activity5 (I get null object). Please help me. Unfortunately I haven't found any similar problem. That's why I write about the problem here. Thanks for any help!
Use this
Bundle intent = getIntent().getExtras();
ArrayList<String> percents = intent.getStringArrayList("Score");

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);

Categories

Resources