[英]How do I count number of spaces, vowels, and characters?
import java.util.*;
import java.lang.String;
public class counter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int space = 0,vowel = 0,chara = 0,i;
System.out.println(" Enter String ");
String s =in.nextLine();
for( i = 0; i < s.length(); i++)
{
char ch = in.next().charAt(i);
if(ch == ' ')
space++;
if(ch == 'e' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowel++;
else
chara++;
System.out.println("Number of Vowels = "+vowel);
System.out.println("Number of Spaces = "+space);
System.out.println("Number of Char = "+chara);
}
}
}
What is the problem? 问题是什么? I have put three counters to count.
我已经设置了三个柜台。 I am coding in Eclipse and whenever I check the console I am not able to count the characters.
我正在Eclipse中进行编码,每当检查控制台时,我都无法计数字符。 It is just accepting inputs and not doing anything else.
它只是接受输入而没有做任何其他事情。
Remove char ch = in.next().charAt(i);
删除
char ch = in.next().charAt(i);
, and replace the other instances of ch
with s.charAt(i)
,并用
s.charAt(i)
替换ch
的其他实例
The first charAt
check should also be a
, you have e
twice. 第一个
charAt
检查也应该是a
,您有两次e
。
Then move System.out.println...
outside of the loop. 然后将
System.out.println...
移到循环外。
You should change s = in.next().charAt(i);
您应该更改
s = in.next().charAt(i);
to String s =in.nextLine()
and put System.out.println
part outside of for loop. 到
String s =in.nextLine()
并将System.out.println
部分放在for循环之外。
Also there is double 'e' (with the help of @Ted Hopp): 还有一个双“ e”(在@Ted Hopp的帮助下):
ch == 'e' || ch == 'e'
ch == 'e' || ch == 'e'
to ch == 'e' || ch == 'a'
ch == 'e' || ch == 'e'
到ch == 'e' || ch == 'a'
ch == 'e' || ch == 'a'
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int space = 0,vowel = 0,chara = 0,i;
System.out.println(" Enter String ");
String s =in.nextLine();
for( i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if(ch == ' ')
space++;
if(ch == 'e' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowel++;
else
chara++;
}
System.out.println("Number of Vowels = "+vowel);
System.out.println("Number of Spaces = "+space);
System.out.println("Number of Char = "+chara);
}
Just a couple of typo errors. 只是几个拼写错误。 Change your code with,
更改您的代码,
String s = in.nextLine().toLowerCase();
And 和
char ch = s.charAt(i);
And 和
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
In your code you are using lowercase letters to compare with the user input. 在您的代码中,您使用小写字母与用户输入进行比较。 So you should convert the user input to lower case first.
因此,您应该首先将用户输入转换为小写。 Your current code ignores all uppercase vowels(E, A, I ...).
您当前的代码将忽略所有大写的元音(E,A,I ...)。 Use
toLowerCase()
. 使用
toLowerCase()
。
By using in.next()
the Scanner
is waiting for an input. 通过使用
in.next()
, Scanner
正在等待输入。 Since you have already taken an input using nextLine()
you can use that. 由于您已经使用
nextLine()
接受了输入, nextLine()
可以使用它。
Next one is obviously a typographical error. 下一个显然是印刷错误。 Vowels are A, E, I, O, U.
元音是A,E,I,O,U。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.