The task is: write java program to find percentage of words distribution between parts of speech. The text is in the file duomenys.txt. And words are marked like that: nouns - D, adjectives - B, verbs - V and prepositions - P in the end of word. For example, "the house is big". Marked sentence: the houseD isV bigB. This is what I have, there are errors at lines 29-32 and from line 40.
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Main {
private static FileReader FileReader(File file) {
throw new UnsupportedOperationException("Not yet implemented");
}
int[] frequencies = new int[ 4 ];
private static int NOUN = 0;
private static int ADJ = 1;
private static int VERB = 2;
private static int PREP = 3;
public static void main ( String [] args ) throws IOException {
String filename = "duomenys.txt";
File file = new File( System.getProperty( "user.dir" ), filename);
FileReader fin = FileReader( file );
Scanner sc = new Scanner( fin );
int wordCount = 0;
while( sc.hasNext() ) {
String s = sc.nextLine();
String[] words = s.split(" ");
for( int i = 0; i < words.length; i++) {
frequencies[ NOUN ] += words[ i ].endsWith( "D" ) ? 1 : 0;
frequencies[ ADJ ] += words[ i ].endsWith( "B" ) ? 1 : 0;
frequencies[ NOUN ] += words[ i ].endsWith( "V" ) ? 1 : 0;
frequencies[ PREP ] += words[ i ].endsWith( "P" ) ? 1 : 0;
wordCount++;
}
}
fin.close();
String[] partsOfSpeech = {"nouns", "adjectives", "verbs", "prepositions"};
for( int i = 0; i < partsOfSpeech.length; i++) {
double percentage = frequencies[i] / wordCount;
System.out.println( "There are " + frequencies[ i ] + " " + partsOfSpeech[ i ] + " (" + percentage + ")");
}
}
The first error is here: frequencies[ NOUN ] += words[ i ].endsWith( "D" ) ? 1 : 0;
And the error is: non-static variable frequencies cannot be referenced from a static context.