从文件中扫描 Java 中的每个字符
Scanning each character in Java from a file
我有一个程序可以扫描文本文件并识别每一行是否以元音开头,但是,我需要能够扫描每个字符以确定该行是否有任何元音和不只是在开始。
String x = "";
Set<String> vowells = new HashSet<>();
vowells.add("a");
vowells.add("e");
vowells.add("i");
vowells.add("o");
vowells.add("u");
while (sc.hasNextLine()) {
readLine = new Scanner(sc.nextLine());
x = readLine.nextLine();
numberOfLines++;
if(vowells.contains(x.toLowerCase())) {
numberOfVowells++;
}
}
我尝试过使用 while sc.hasNext
并尝试过使用 splitregex 但我没有运气,所以我想也许我的错误很小,这里有人可以帮助我吗?
这个问题似乎有点令人费解,但如果您只想在文件中查找元音字母,则可以逐行查找。下面是一些示例代码:
int numberOfVowells = 0;
String x = "";
String letter = "";
Set<String> vowells = new HashSet<>();
vowells.add("a");
vowells.add("e");
vowells.add("i");
vowells.add("o");
vowells.add("u");
File myFile = new File("Hello.txt");
Scanner sc = new Scanner(myFile);
while(sc.hasNextLine())
{
//take it in line by line
x = sc.nextLine();
//cycle through each character in the line
for(int i = 0; i < x.length(); i++)
{
letter = x.substring(i,i);
if(vowells.contains(letter.toLowerCase()))
{
numberOfVowells++;
}
}
}
我想如果你想计算以元音开头的行数,你可以只添加一个 if 语句,当 i 为零且它是一个元音时。
您只需使用一个 Scanner
即可读取文件。
File file = new File("the/path/to/your/file");
Scanner scanner = new Scanner(file);
然后你可以使用hasNextLine()
和nextLine()
逐行读取你的文件内容
String currLine;
while(scanner.hasNextLine()) {
currLine = scanner.nextLine();
//do something with currLine
}
在你的情况下,你似乎想检查当前行是否以元音开头,所以我建议首先将 currLine
全部小写并修剪。
currLine = currLine.toLowerCase().trim();
然后您可以使用 startsWith
和 charAt
等字符串函数进行检查。
Scanner filescan = new Scanner(new File("**your document/file**"));
我有一个程序可以扫描文本文件并识别每一行是否以元音开头,但是,我需要能够扫描每个字符以确定该行是否有任何元音和不只是在开始。
String x = "";
Set<String> vowells = new HashSet<>();
vowells.add("a");
vowells.add("e");
vowells.add("i");
vowells.add("o");
vowells.add("u");
while (sc.hasNextLine()) {
readLine = new Scanner(sc.nextLine());
x = readLine.nextLine();
numberOfLines++;
if(vowells.contains(x.toLowerCase())) {
numberOfVowells++;
}
}
我尝试过使用 while sc.hasNext
并尝试过使用 splitregex 但我没有运气,所以我想也许我的错误很小,这里有人可以帮助我吗?
这个问题似乎有点令人费解,但如果您只想在文件中查找元音字母,则可以逐行查找。下面是一些示例代码:
int numberOfVowells = 0;
String x = "";
String letter = "";
Set<String> vowells = new HashSet<>();
vowells.add("a");
vowells.add("e");
vowells.add("i");
vowells.add("o");
vowells.add("u");
File myFile = new File("Hello.txt");
Scanner sc = new Scanner(myFile);
while(sc.hasNextLine())
{
//take it in line by line
x = sc.nextLine();
//cycle through each character in the line
for(int i = 0; i < x.length(); i++)
{
letter = x.substring(i,i);
if(vowells.contains(letter.toLowerCase()))
{
numberOfVowells++;
}
}
}
我想如果你想计算以元音开头的行数,你可以只添加一个 if 语句,当 i 为零且它是一个元音时。
您只需使用一个 Scanner
即可读取文件。
File file = new File("the/path/to/your/file");
Scanner scanner = new Scanner(file);
然后你可以使用hasNextLine()
和nextLine()
逐行读取你的文件内容
String currLine;
while(scanner.hasNextLine()) {
currLine = scanner.nextLine();
//do something with currLine
}
在你的情况下,你似乎想检查当前行是否以元音开头,所以我建议首先将 currLine
全部小写并修剪。
currLine = currLine.toLowerCase().trim();
然后您可以使用 startsWith
和 charAt
等字符串函数进行检查。
Scanner filescan = new Scanner(new File("**your document/file**"));