0
Can anyone tell me the errors?
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)));
}
}
+ 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)));
}
}
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);
}
}
0
đ
°đ
č đ
đ
đ
đ
đ
Ł why both round and ceiling function are used at a time?
0
đ
°đ
č đ
đ
đ
đ
đ
Ł Can you please tell me basically what were the errors in my program?
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
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