ok so I have to make a consoleprint interface class and two other classes that implement it
one class simply will let me print the text and the other u can see is string splitter. So far I have had problems with my tester
public interface ConsolePrint {
public void printInfo(String infoToPrint);
}
public class SimplePrint implements ConsolePrint {
public void printInfo (String infoToPrint) {
printInfo("Heading this is not fancy");
}
}
public class FancyPrint implements ConsolePrint {
public void printInfo (String printInfo) {
for (String splitter: printInfo.split("", 1)){
System.out.println(splitter);
}
}
}
Heres the tester that I am getting problems from
import java.util.*;
public class ConsolePrintTest {
public static void main(String[] args) {
}
SimplePrint mySp = new SimplePrint();
FancyPrint myFp = new FancyPrint();
myFp.printInfo(); <-----error appearing here
}
Any help would be great thanks
You need to move your method call into your main
method (or at least some method). Currently it's not in a method - only declarations (of fields, methods, nested types etc) can be at the top level of a class.
Additionally, you're not currently passing an argument to the printInfo
method, but
So this would be fine:
public class ConsolePrintTest {
public static void main(String[] args) {
FancyPrint myFp = new FancyPrint();
myFp.printInfo("some string");
}
}
Note that:
mySp
declaration as that variable was never usedYou're not currently using the fact that the classes implement the interface. You might want to consider:
ConsolePrint printer = new FancyPrint();
printer.printInfo("some string");
As noted in comments, your SimplePrint
implementation recurses unconditionally too, so that's the nest problem to fix.
See more on this question at Stackoverflow