我正在编写一个程序,用于计算每个字母字符(将小写字母转换为大写字母)出现在数据文件中的次数。
import java.io.*;
import java.util.*;
public class CharDistribution{
public static void main(String[] args) throws FileNotFoundException{
File file = new File (args[0]);
String[] letters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int numbers[] = new int [26];
Scanner input=new Scanner(file);
input.useDelimiter("");
while (input.hasNext()){
String nextLetter = input.next().toUpperCase( );
for (int i = 0; i <letters.length; i++);
{
String letter = letters[i].toUpperCase( );
if (letters.equals(nextLetter)){
numbers[i] = numbers[i] + 1;
}
}
for (int i = 0; i <letters.length; i++);
{
System.out.println(letters[i].toUpperCase( ) + " - " + numbers[i]);
}
}
}
}
由于某种原因,程序无法找到我在for循环中使用的变量i。
我在循环后使用i的所有5次给我这个错误“找不到符号”。我已经看了十几次,似乎无法找到代码的错误。你看到我可能有什么问题吗?
答案 0 :(得分:0)
for
后面有一个分号,它会终止它,使下面的块与它无关,因此无法解析i
。只需删除它,你就可以了:
for (int i = 0; i <letters.length; i++) // ; removed here
{