私のプログラムが何をしているのかを知る必要があります。私はプログラムのこのFrankensteinを一緒にパッチを当てるために他の質問に対する他の答えを使用しました。JOPtionPane文字数のテキストファイル
もう1つのことは、コードがテキストファイル内の文字カウントを集めるべきであり、それが1文字であるため、何をすべきかわからないということです。文はその非常に高く評価されると思い、
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class File_Letter_Counter{
public static void main(String[] args) throws IOException{
//Declare variables.
//Hold fileName variable.
String fileName;
//Set accumulator value.
int accumulator=0;
//Set fileName equal to user input.
fileName=JOptionPane.showInputDialog("Enter the name of a file: ");
//Set letter for file search.
String letter=JOptionPane.showInputDialog("Enter a letter: ");
//Set char variable to string input's letter.
char character=(Character.toLowerCase(letter.charAt(0)));
//Open file.
File file=new File (fileName);
//Set new scanner object to read file.
Scanner inputFile=new Scanner(file);
//Loop for every line in file.
while (inputFile.hasNext()){
//Set line equal to the next line that is in the file.
String line=inputFile.nextLine();
/*For every time count is less than the number
of characters in the string, repeat the loop.*/
for(int count=0; count<line.length(); count++){
/*if line 1 is equal to lower case "character" variable
*/
if (line.charAt(count)==Character.toLowerCase(character)
||line.charAt(count)==Character.toUpperCase(character)){
count++;
accumulator++;
}
}
}
JOptionPane.showMessageDialog(null, accumulator);
inputFile.close();
}
}
これは**あなたの**コードです。 **あなたは何をしているのか知っているはずです。 –
デバッガでステップバイステップで実行し、自分で確認してください。 –