abstract console input class and class deriving from it

Hi,

i have created a simple class that reads and writes from the console. As i may want to program other code in the future that has a console interface, i want to keep this class as generic as possible.

But when i implement it (extend it), i get an error like so when compiling:

DerivingClass.java:32: non-static method checkForInput() cannot be referenced from a static context

checkForInput();

I'm not sure that the way i'm trying to make the ConsoleInterface generic is a good anyway.

What would be a good way to achieve this and how can i solve the error?

The ConsoleInterface.java code:

import java.lang.*;

import java.util.*;

publicabstractclass ConsoleInterface{

BufferedReader console_in =null;

PrintWriter console_out =null;

String line =null;

publicvoid checkForInput(){

try{

console_in =new BufferedReader(new InputStreamReader(System.in) );

console_out =new PrintWriter(new OutputStreamWriter(System.out) );

while ( (line = console_in.readLine()) !=null ){

processInterfaceCommand();

}

printExitStatement();

console_in.close();

console_out.close();

}

catch (IOException e){

e.printStackTrace(console_out);

}

}

publicabstractvoid processInterfaceCommand();

publicabstractvoid printExitStatement();

}

The DerivingClass.java code

import java.net.*;

import java.io.*;

publicclass DerivingClassextends ConsoleInterface{

public DerivingClass(){

System.out.println("Starting DerivingClass ");

}

publicvoid processInterfaceCommand(){

// repeat incoming text

console_out.println("You wrote " + line);

console_out.flush();

// ending?

if ( line.equalsIgnoreCase("BYE") ){

return;

}

elseif ( line.equalsIgnoreCase("HELLO") ){

console_out.println("Hello back at ya ");

console_out.flush();

}

}

publicvoid printExitStatement(){

console_out.println("Have a nice day :)");

console_out.flush();

}

publicstaticvoid main(String[] args)throws IOException{

DerivingClass serv =new DerivingClass();

// do other stuff before going in check loop

// ...

checkForInput();

}

}

[4788 byte] By [ wsbenedictv] at [2007-9-30 9:12:29]
# 1

Hi, I'm not overly great at Java but it looks like you are trying to call the checkTheInput() method from the this object of the class. However because you are in a static method there is no such object, you should call the checkTheInput() from the serv object that you created, hope this works,

Fitz.

fitz at 2007-7-2 21:50:41 > top of Java-index,Administration Tools,Sun Connection...
# 2
hhhm. stupid me. That was it. Now it works.Thanks!
wsbenedictv at 2007-7-2 21:50:41 > top of Java-index,Administration Tools,Sun Connection...