Reputation: 43
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.
Upvotes: 0
Views: 787
Reputation: 1
ArrayList<NiceListItem> santaNiceList = new ArrayList<NiceListItem>();
final int TOTAL_NUMBER_OF_PRESENTS = 4 ;
int totalPeople=0;
while (totalPeople<5){
//it will pick a random name
Name randomPerson = new Name();
randomPerson.setName(randomizeFirstName(),randomizeLastName());
// it will randomize the presents
ArrayList<String> randomPresents =new ArrayList<String>();
randomPresents.clear();
for (int index = 0; index < TOTAL_NUMBER_OF_PRESENTS; index++){
randomPresents.add(randomizePresent());
}
//it will adds up together
santaNiceList.add(new NiceListItem(randomPerson,randomPresents));
// it will keeps counting
totalPeople++;
}
for (int index = 0; index < santaNiceList.size(); index++) {
System.out.println(santaNiceList.get(index));
}
}
//list of presents for NiceList
public static String randomizePresent(){
String possiblePresents[] = {"Shoes" , "Computer", "Bicycle", "Clothes" ,"Cellphone", "Skateboard"};
int randomId = random(0, possiblePresents.length-1);
return possiblePresents[randomId];
}
//a list of First Name on NiceList
public static String randomizeFirstName(){
String possibleFirstName[] = {"Raymond" , "Anne" , "Rey" , "Eric", "Ralph"};
int randomId = random(0, possibleFirstName.length-1);
return possibleFirstName[randomId];
}
//a list of Last Name on Nicelist from First Name
public static String randomizeLastName(){
String possibleLastName[] = {"Manalo", "Syversten", "Capillas","Hallil","Reyes"};
int randomId = random(0, possibleLastName.length-1);
return possibleLastName[randomId];
}
//this statement will check the rig
public static int random(int min, int max){
SecureRandom rnd = new SecureRandom();
int rand = rnd.nextInt(max * min + 1)+ min;
rnd = null;
return rand;
Upvotes: 0
Reputation: 1837
I will try to explain. "Static" variable and methods belong to the class. They are shared by all objects (or "instances") of that class. "Instance" variable and methods belong to a particular object. There is a different copy for every object in the system. So for example:
class MySimpleObject {
private int myCount = 0;
private static int everybodyCount = 0;
public void add() {
myCount++;
everybodyCount++;
}
public void print() {
System.out.println("myCount = " + myCount + ", everybodyCount = " + everybodyCount);
}
}
Now you write a main method like this:
public static void main(String[] args) {
MySimpleObject first = new MySimpleObject();
MySimpleObject second = new MySimpleObject();
first.add();
second.add();
second.add();
}
if you were now to call first.print(), you would see that myCount = 1 and everybodyCount = 3. If you call second.print() you would see that myCount = 2 and everybodyCount = 3. See how the instance data (myCount, not static) is kept separate, but the static data (everybodyCount) is shared? That's the idea.
So, the point is, if you are in a "static" method, you are operating on the class level. You can still access the static data, because that is data everybody shares. You cannot access "instance" data, because there is no instance. ie, if I ask you to tell me "everybodyCount" right now, easy. You say 3. If I ask you to tell me "myCount" you say, "depends, which instance are you talking about?". Right? Now you get it. I hope. Good luck. :)
Upvotes: 0
Reputation: 62789
Try making frequencies static for a start. This should work, or at least improve your situation. Then work towards @Robert Harvey's better solution.
One of the "Tricks" is to get your stuff to work then only make Very Small Changes--one line at a time perhaps--and make sure it always works between each change.
That way if you break something you know exactly what it broke.
If you get a different error message after changing "frequencies" to a static, let me know what that error message is.
Also, just a comment:
frequencies[ NOUN ] += words[ i ].endsWith( "D" ) ? 1 : 0;
Is not as readable as:
if( words[ i ].endsWith( "D" ) )
frequencies[ NOUN ]++;
or (if you must have it on one line)
if( words[ i ].endsWith( "D" ) ) frequencies[ NOUN ]++;
If you concentrate on making your code simple to read at a glace rather than as short as possible you will find things a lot easier.
Don't get frustrated--or if you do take a break and come back later, it'll make more sense then. (That's another programmer trick--when working on a hard problem all night the solution will generally come to you in the shower the next day)
Upvotes: 2
Reputation: 8857
You are trying to access some non-static properties in a static method:
If you have the following class
public class Test{
public int Property;
public static voidDoSomething(){
//You cannot access Property here, because
//it's not static. It needs to be initialized first.
}
}
In your case, you have the static method main
, and you try to access the non-static method frequencies
.
To avoid these problems, homework exercises usually do something like this:
public class Main{
public int Property;
public void Start(){
///you can access Property here
}
public static void main ( String [] args ){
new Main().Start();
}
}
That way you avoid the problem you're trying to access properties in your static void main
Upvotes: 5