Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 2
Java code mistake
Can anyone please😔 tell me what mistake have i done here in my program Average word finder , from code coach. import java.util.Scanner ; public class Program { public static void main(String[] args) { Scanner ab = new Scanner(System.in); String para = ab.nextLine(); int n=0; String p[]= para.split(" "); int t= p.length; for(int i=0; i< t ; i++) { for(int j=0 ; j< p[i].length(); j++) { if(Character.isLetter(p[i].charAt(j))) { n=n+1; } }} int k= (int)Math.ceil(n/t); System.out.println(k); } } https://code.sololearn.com/c75X6qu6FOvF/?ref=app Plz friend find whats the bug here in this program when i did put this code in test run , accept the first case all cases were wrong😞.
5 Answers
+ 3
n/t is an int divided by an int which will result in integer division, truncating any fractional part. So, no rounding will actually take place.
int k= (int)Math.ceil((float)n/t);
+ 2
Did you see the code change I provided in my last post? You just need to convert one or both of the numbers in the division to a float so that it will result in a float.
If you still aren't passing all the test cases after that change, then there is an additional issue.
+ 2
Wow bro thanx very much it really worked , n sorry mine mistake i didn't saw😀
0
Hmm so it seems that i dont need Math.ceil option
I should rather write
K= n/t;
Sop(k);
??
Say bro?
0
But bro that doesn't works either what should i do plz suggest