Can anyone tell me the errors? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
26th Apr 2021, 3:12 PM
Atul
Atul - avatar
7 Answers
+ 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