+ 1

Help me with this avg word count problem.

import java.util.*; public class Program { public static void main(String[] args) { Scanner s=new Scanner(System.in); String str=s.nextLine(); String arr[]=str.split("\\s+"); for(int i=0;i<str.length();i++) if((str.charAt(i)>=32 &&str.charAt(i)<=64)) { str=str.replaceAll("str.charAt(i)",""); } else {} int n=str.length(); int ans=n/arr.length; System.out.println(ans); } }

13th Apr 2020, 6:07 AM
Ajay
Ajay - avatar
1 Respuesta
0
Ajay, your for loop is running but not reacting according to your desire. Since you have used "" for the replacement of char at index i and so the compiler will take this as String type. You can replace that replacing statement with this one: str=str.replace(Character.toString(str.charAt(i)),""); Also, output wants the nearest whole num; and as mentioned in the question if your ans= 5.2, output = 6. And to fulfill that condition, you may use: double n=str.length(); double ans= n/arr.length; if (ans == (int) (Math.floor(ans))){ ans = ans; }else{ ans++; } System.out.println((int)ans); } } I hope it works!
13th Apr 2020, 8:30 AM
Rohit Chaudhary
Rohit Chaudhary - avatar