Here i have a piece of code that reads in a text file, however it only outputs how many letters there are in the word(s). I want it to output the percentage of words instead. Could anyone help me figure this out? Would highly appreciate it!
public static void fileanalysis(){
Scanner scan;
try {
scan = new Scanner(new File("G:/test.txt")); //change directory to load in text file
}
catch (Exception e) {
System.out.println("File was not found ");
return;
}
while(scan.hasNextLine()) {
String text2 = scan.nextLine();
System.out.println ("\n--------------------------------------------------------------------------------------------------------");
System.out.println (" Words loaded in from the file: " + text2);
System.out.println ("--------------------------------------------------------------------------------------------------------");
System.out.print("\n");
System.out.print("");
char uinput;
for(int i = 97; i<123 ; i++){
uinput = (char)i;
System.out.print(" " + uinput );
}
System.out.println ("\n--------------------------------------------------------------------------------------------------------");
for(int i = 97; i<123 ; i++)
{
uinput = (char)i;
System.out.format(" "+frequencies(text2,uinput));
}
System.out.print("\n\n");
main(null);
}
}
public static int frequencies(String input, char uinput){
int count = 0;
for (int i = 0; i < 26; i++)
{
if (input.charAt(i) == uinput)
{
count++;
}
}
return count;
}
Aucun commentaire:
Enregistrer un commentaire