26th Apr 2021, 3:12 PM
Atul
Atul - avatar
7 Respostas
+ 1
Atul First get number of words then replace all space and count letter. Then finally divide no_of_letters by no_of_words Here is complete solution: public class Program { public static void main(String[] args) { Scanner sc=new Scanner (System.in); int f=0,count=0; String n=sc.nextLine(); int len = n.split(" ").length; n = n.replaceAll("\\s", ""); //n=" "+n; for (int i=0;i<n.length();i++){ char c=n.charAt(i); if(Character.isLetter(c)){ ++f; } //if(c==' '){ //count ++; //} } double s=(double) f / len; //int ac=(int)s; System.out.println(Math.round(Math.ceil(s))); } }
27th Apr 2021, 8:22 AM
AÍąJ
AÍąJ - avatar
+ 1
Atul Your solution was also right but you just need to do some changes. See here import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner (System.in); int f=0,count=0; String n=sc.nextLine(); n=" "+n; for (int i=0;i<n.length();i++){ char c=n.charAt(i); if(Character.isLetter(c)){ ++f; } if(c==' '){ count ++; } } double s=(double) f / (count); System.out.println(Math.round(Math.ceil(s))); } }
27th Apr 2021, 8:27 AM
AÍąJ
AÍąJ - avatar
0
import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner (System.in); int f=0,count=0; String n=sc.nextLine(); n=" "+n; for (int i=0;i<n.length();i++){ char c=n.charAt(i); if(Character.isLetter(c)){ ++f; } if(c==' '){ count ++; } } double s=(double)(Math.ceil(f/count)); int ac=(int)s; System.out.println(ac); } }
26th Apr 2021, 3:13 PM
Atul
Atul - avatar
0
🅰đŸ…č 🅐🅝🅐🅝🅣 why both round and ceiling function are used at a time?
27th Apr 2021, 3:13 PM
Atul
Atul - avatar
0
🅰đŸ…č 🅐🅝🅐🅝🅣 Can you please tell me basically what were the errors in my program?
27th Apr 2021, 3:21 PM
Atul
Atul - avatar
0
Atul There was little mistake. You were doing Math.ceil(f / count) So here f and count both are integer and when you divide then output maybe like this For example f = 3, count = 4 So f / count = 3 / 4 = 0 So in this case Math.ceil will give 0 but it should not be like that. You should cast f to double and then divide by count. So (double) f / count = 3.0 / 4 = 0.75 If you do Math.ceil then output will be 1.0
27th Apr 2021, 3:37 PM
AÍąJ
AÍąJ - avatar
0
Atul Why round and ceiling method at same time? Because Math.ceil will give double value but you have to get output as Integer so Math.round will give integer value. Math.round(Math.ceil(3.4)) = Math.round(4.0) = 4
27th Apr 2021, 3:40 PM
AÍąJ
AÍąJ - avatar