so my homework is asking me only for class methods, but it’s requiring i turn in a .java and a .class file. well i’ve got code that theoretically should work, but it won’t compile no matter what i try. for example here’s one:
public class findFourLetterWord(String[] strings) {
public static void main(String[] args) {
for (int i = 0; i < strings.length; i++)
if (strings[i].length()==4)
return strings[i];
return null;
}
}
and here’s the errors i’m getting:
8 errors found:
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 9]
Error: The public type findFourLetterWord must be defined in its own file
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 9]
Error: Syntax error on token "(", { expected
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 9]
Error: Syntax error on token ")", ; expected
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 9]
Error: Syntax error, insert "}" to complete Block
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 11]
Error: Cannot make a static reference to the non-static field strings
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 12]
Error: Cannot make a static reference to the non-static field strings
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 13]
Error: Cannot make a static reference to the non-static field strings
File: D:\School\CSC 2310\hw5_elemmons1\FindFourLetterWord.java [line: 14]
Error: Void methods cannot return a value
any suggestions?
Assuming you are running Eclipse as your IDE, if you rearrange your code to something like this:
and then save the changes (which automatically triggers the compilation), you can run your compiled program (
.classfile) from a command line, with a few arguments (namely contained inargs), being the words search list:You’ll get the first 4-letter word among the words specified (abba).
This way you essentially invoked
mainmethod of the class (supplying it with a fewStringarguments), which is the entry point for each Java program.As others already said, try to pick up on the basics first.