0

print 25 for n=3 Happy sad happy where n is no of elements it seems to print total_score as 0 every time also mention the reason i am facing this problem thanks for the help in advance

import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    public class Mood {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int n= sc.nextInt();
            HashMap<Integer, String> hmap = new HashMap<>();
         /* for (int i = 0; i < n; i++) {
                Integer a = sc.nextInt();
                String b = sc.next();
    
                hmap.put(a, b);
            }*/
    
            String[] string = new String [n];
            sc.nextLine();
            for (int i = 0; i < n; i++)
            {
                string[i] = sc.nextLine();
            }
           /* HashMap<String, Integer> mood = new HashMap<>();
            mood.put("Happy",10);
            mood.put("Sad",5);
            mood.put("Neutral",2);
            mood.put("None",1);*/
            int Total_score=0;
            for (int i = 0; i <n ; i++) {
                if(string[i]=="Happy"){
                    Total_score=Total_score+10;}
                else if (string[i]=="Sad"){
                    Total_score=Total_score+5;}
                else if (string[i]=="Neutral"){
                    Total_score=Total_score+2;}
                else if (string[i]=="None"){
                    Total_score=Total_score+1;}
                else
                    break;
            }
            System.out.println(Total_score);
    
        }
    }
Price
  • 3
  • 1
  • Can you share your `string`? Is it `["Happy", "sad", "Happy"]`? I guess it goes immediately into `else break` for some reason. – Watanabe.N Mar 12 '22 at 03:36
  • yes the string is ["Happy", "sad", "Happy"] and it gives the same and without the else condition – Price Mar 12 '22 at 03:39

1 Answers1

0

Use this to compare strings myStr1.equals(myStr2) , i fell in this strap in java before

mazen amr
  • 90
  • 6
  • This should go into comments, wait..., did OP accept this as answer? – Watanabe.N Mar 12 '22 at 04:21
  • first he accept it , second why it should go it comments it help him fix the error it is the solution , @Watanabe.N – mazen amr Mar 12 '22 at 04:28
  • I thought many possible causes (strings is not correctly read from a file, or capital case, and others...). OP is new to SO and I thought he wrongly accepted it. Finally it seems to be turned out that string comparison was the only cause. Forget about my comment. – Watanabe.N Mar 12 '22 at 06:42