java - Getting error: "non static method cannot be referenced from a static context" while calling function from main -


i have function

private arraylist<letters> getlettersinfo(string input) {         arraylist<letters> al = new arraylist<letters>();         (char c : alphabet.tochararray())         {             letters l = new letters();             l.setletter(character.tostring(c));             int count = countoccurrences(input, c);             l.setcount(count);             l.setfrequency(count/28);             al.add(l);         }         return al; } 

then try in main:

arraylist<letters> al = new arraylist<letters>(); al = getlettersinfo(plaintext); for(letters l : al) {        system.out.print("letter: " + l.getletter() + ", "                 + "count: " + l.getcount() + ", "                 + "frequency: " + l.getfrequency()); } 

but non static method cannot referenced static context. read somethings error seems ok. help?

change function to:

private static list<letters> getlettersinfo(string input) 

and while @ suggest following refactoring:

for(letters l: getlettersinfo(plaintext)) {        system.out.print(string.format("letter: %s, count: %d, frequence: %d",            l.getletter(),            l.getcount(),            l.getfrequency()); } 

you new java , should later learn static , first prioritize understanding basics. therefore propose create separate class holds main method , put other stuff in different classes.

in case main like

public static void main(string[] args) {     info info = new info(); // newly created class     for(letters l: info.getlettersinfo(plaintext))     {         // ...     } } 

or (with info inlined):

public static void main(string[] args) {     for(letters l: new info().getlettersinfo(plaintext))     {         // ...     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -