Having a problem in Average Word Length(code coach) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Having a problem in Average Word Length(code coach)

import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String k=sc.nextLine(); int a=k.length(); int x=0; for(int i=0;i<a;i++) { char s=k.charAt(i); if(s==33|s==34|s==39|s==44|s==46|s==58|s==59|s==63|s==32) { continue; } else { x++; } } int y=0; int z=0; for(int i=0;i<a;i++) { if(k.charAt(i)==32) { y++; } } z=y+1; double r=(double)x/z; int b=0; if(r==Math.round(r)) { b=(int)Math.rint(r); } else { b=((int)Math.rint(r))+1; } System.out.print(b); } }

4th Jan 2020, 7:29 PM
R Biswas
5 Answers
+ 1
s pal The first part is correct. Second part: you need to round up. Use Math.ceil(). double r = (double)x/z; System.out.println((int)Math.ceil(r));
4th Jan 2020, 8:42 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Interesting approach, why did you not use regular expressions In order to remove the punctuation from the String? , therefore attempting to remove characters according to int value seems hard coded and less dynamic. Attempt regular expressions maybe ? That could be your identification of punctuation in the input String.
4th Jan 2020, 7:33 PM
Justin Joseph
Justin Joseph - avatar
+ 2
s pal I believe it could be because that test case incorporates punctuation that you need taking into account , unless it could be the way in which you are averaging the word length.
4th Jan 2020, 7:41 PM
Justin Joseph
Justin Joseph - avatar
+ 1
All cases work but only the 4 case shows error Justin Joseph
4th Jan 2020, 7:34 PM
R Biswas
+ 1
Justin Joseph I think the problem maybe in second part and had earlier tried by eliminating all except alphabets, numbers and space so that all other gets out
4th Jan 2020, 7:35 PM
R Biswas