-3

for example these will be my arrays. I will input some symptoms and then it will provide me with x ordered based on the number of symptoms found in s.

String [] x=new String[] {
            "Allergic Rhinitis",
            "Diabetes",
            "Diarrhea",
            "Dysmenorrhea",
            "Anemia"
 };

String [] s=new String[] {
"Runny nose,Nasal congestion,Itchy eyes,Sneezing,Cough,Itchy nose,Sinus pressure,Facial pain,Decreased sense of smell or taste",
"Unexplained weight loss,Increase frequency of urination,Increase volume of urine,Increase thirst,Overweight",
"Abdominal cramps,Fever,Feeling the urge to defecate,Fatigue,Loss of appetite,Unintentional weight loss",
"Cramping pain extending to the lower back and thighs",
"Fatigue,Weakness,Pale skin,Fast or irregular heartbeat,Shortness of breath,Chest pain,Dizziness,Cognitive problems,Headache"
   }
Prudhvi
  • 2,276
  • 7
  • 34
  • 54
XHime
  • 9
  • 1
  • 1
    can you please elaborate with your question, to be clear? – Pavan Feb 26 '15 at 03:14
  • I just changed the question. Is that clear enough? – XHime Feb 26 '15 at 03:20
  • How are you mapping symptoms to diseases? – Prudhvi Feb 26 '15 at 03:39
  • possible duplicate of [In Java, how can I test if an Array contains a certain value?](http://stackoverflow.com/questions/1128723/in-java-how-can-i-test-if-an-array-contains-a-certain-value) –  Feb 26 '15 at 03:41
  • I already know how to get the array containing the value I searched. What I need is to arrange them by number of values found. Let's say I've put Runny Nose, Sinus Pressure, Fatigue, Weakness, Shortness of Breath then it will show Allergic Rhinitis(with 2 symptoms found) and Anemia(with 3 symptoms found). The output in my program is alphabetical but I want it to be arranged by number of symptoms found like the Anemia should be first then Allergic Rhinitis. – XHime Feb 26 '15 at 03:55

2 Answers2

0

Sounds like you need a mapping of diseases to their symptoms. Here's a rudimentary example using collection classes in java.util package to demonstrate how you might declare such a mapping and use it.

    // setup a map of diseases to their symptoms
    Map<String, List<String>> symptomsByDisease = new HashMap<String, List<String>>();
    symptomsByDisease.put("Allergic Rhinitis", Arrays.asList("Runny nose"));
    symptomsByDisease.put("Anemia", Arrays.asList("Runny nose", "Nasal congestion"));
    symptomsByDisease.put("Diabetes", Arrays.asList("Nasal congestion", "Itchy eyes"));

    // accept symptoms from user input
    List<String> userSymptoms = Arrays.asList("Itchy eyes", "Nasal congestion");

    // map diseases to their count of symptoms matching user input
    final Map<String, Integer> countsByDisease = new HashMap<String, Integer>();
    for (Map.Entry<String, List<String>> diseaseSymptoms: symptomsByDisease.entrySet())
    {
        Set<String> matchingSymptoms = new HashSet<String>(diseaseSymptoms.getValue());
        matchingSymptoms.retainAll(userSymptoms);
        countsByDisease.put(diseaseSymptoms.getKey(),
            Integer.valueOf(matchingSymptoms.size()));
    }

    // sort diseases by descending count of matching symptoms
    List<String> diseaseNames = new ArrayList<String>(symptomsByDisease.keySet());
    Collections.sort(diseaseNames, new Comparator<String>() {
        @Override public int compare(String disease1, String disease2) {
            int count1 = countsByDisease.get(disease1).intValue();
            int count2 = countsByDisease.get(disease2).intValue();
            int compare = count2 - count1; // descending symptom match
            if (compare == 0) { // default to alphabetical disease name
                compare = disease1.compareTo(disease2);
            }
            return compare;
        }
    });

    // show results
    System.out.println(diseaseNames);
gknicker
  • 5,509
  • 2
  • 25
  • 41
0

I am not completely sure what you are asking but I think you have two arrays of Strings one containing names of illnesses and the other containing the symptoms of each of those diseases. I am guessing that the symptoms in s correspond to the illnesses in x at the same index. So Diabetes's symptoms are Unexplained weight loss,Increase frequency of urination,Increase volume of urine,Increase thirst, Overweight.

So I think your question is how do you get the number of symptoms from that String and compare it to the number of symptoms from the other illnesses. (I don't have enough rep to comment so I might as well give a full answer to what I think your question is).

For this task you need to count the number of symptoms first. To do that I just counted the number of commas in the String and added one, this assumes that the symptoms are separated by commas and don't end with a comma, but it seems like that is the format.

     int sympNum[] = new int[s.length];
     for(int i=0;i<s.length;i++)
        {
            for(int j=0;j<s[i].length();j++)
            {
                if(s[i].charAt(j)==',')
                    sympNum[i]++;
            }
            sympNum[i]++;
        }

Now that we know the number of each you want to sort the array and then print the illnesses accordingly. Well that is a little tricky because the array of the number of lengths only relates to the array of illnesses by the indexes. I made a new array of the symptom numbers sorted and then just compared that to the unsorted array which related back to original array of diseases because they have the same index.

     int[] sorted = Arrays.copyOf(sympNum, sympNum.length);
     Arrays.sort(sorted); //sorts the array into ascending order

     for(int i=sorted.length-1;i>=0;i--) //you want descending so count backwards
     {
         int spot = 0;
         while(sorted[i]!=sympNum[spot])
             spot++;
         System.out.println(x[spot]); //when they match it prints the illnesses that corresponds to that number of symptoms
         sympNum[spot] = Integer.MAX_VALUE; //I did this so that if there are multiple diseases with the same number of illnesses one doesn't get printed multiple times
     }

It would really be better to make the illnesses objects each with it's own array of Strings containing symptoms and also an int with the number of symptoms.

Next time please word your question more thoughtfully so we can figure out what you are asking. I hope that instead of just copying my code and turning this in you learn about object oriented programming and try your own version.

http://docs.oracle.com/javase/tutorial/java/javaOO/



Lubed Up Slug
  • 168
  • 1
  • 11