import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Vowels
{
public static void main(String[] args) throws FileNotFoundException
{
String vowels = "aeiou";
int[] counters = new int[vowels.length()];
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
console.useDelimiter("");
while (console.hasNext())
{
char ch = console.next().charAt(0);
ch = Character.toLowerCase(ch);
if(ch == 'a')
{
counters[0]++;
}
if(ch == 'e')
{
counters[1]++;
}
if(ch == 'i')
{
counters[2]++;
}
if(ch == 'o')
{
counters[3]++;
}
if(ch == 'u')
{
counters[4]++;
}
}
for (int i = 0; i < vowels.length(); i++)
{
System.out.println(vowels.charAt(i) + ": " + counters[i]);
}
}
}
当我运行文件时,只有在检测到数百个文件时它才会检测到3个文件。我没有看到任何可能导致问题的代码问题,请帮忙。我假设它必须介于我的分隔符和结束之间,因为剩下的不在书中。
答案 0 :(得分:1)
您的分隔符的工作方式与您认为的不同。假设您打算阅读inputFileName
,您可以构建File
和Scanner
来执行此操作(请记住关闭Scanner
块中的finally
,或者try-with-resources
statement)。您还可以根据counters
中元音的位置确定正确的vowels
索引。最后,您可以使用格式化的io作为输出循环。像,
String vowels = "aeiou";
int[] counters = new int[vowels.length()];
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.nextLine().trim();
try (Scanner scan = new Scanner(new File(inputFileName))) {
while (scan.hasNextLine()) {
for (char ch : scan.nextLine().toLowerCase().toCharArray()) {
int p = vowels.indexOf(ch);
if (p >= 0) {
counters[p]++;
}
}
}
}
for (int i = 0; i < vowels.length(); i++) {
System.out.printf("%c: %d%n", vowels.charAt(i), counters[i]);
}
答案 1 :(得分:0)
您的代码中有2个问题,
inputFileName
时,输入已分配给console
。这使\r\n
只带回车(new Scanner(System.in)
)。console.hasNext()
,您的程序将等待用户输入更多输入,并且永远不会结束。 while
永远不会是假的。你能做些什么,在inputFileName
循环中从File file = new File(<path to file>);
Scanner sc = new Scanner(file);
获取每个字符,然后增加元音数。
如果你想从文件中获取输入,你应该
console
然后你的while循环就可以了。并记得在阅读文件后关闭#in setting added this
SOCIALACCOUNT_ADAPTER = 'trip2.users.adapter.SocialAdapter'
# and then override the Adapter
class SocialAdapter(DefaultSocialAccountAdapter):
def populate_user(self,
request,
sociallogin,
data):
"""
Hook that can be used to further populate the user instance.
For convenience, we populate several common fields.
Note that the user instance being populated represents a
suggested User instance that represents the social user that is
in the process of being logged in.
The User instance need not be completely valid and conflict
free. For example, verifying whether or not the username
already exists, is not a responsibility.
"""
username = data.get('username')
first_name = data.get('first_name')
last_name = data.get('last_name')
email = data.get('email')
name = data.get('name')
user = sociallogin.user
emailtouname = email.split('@')[0] + "_" + email.split('@')[1].split('.')[0]
user_email(user, valid_email_or_none(email) or '')
name_parts = (name or '').partition(' ')
user_field(user, 'first_name', first_name or name_parts[0])
user_field(user, 'last_name', last_name or name_parts[2])
user_username(user, username or emailtouname)
return user
。