Subclass and Abstract method implementation
Hello all, I have an assignment in which I need to subclass and implement an abstract method (java.io.OutputStream) to create an output stream called NumStream. The NumStream class converts digits to strings. We are given most of the code it seems and I need to only implement one area. It seems when I type System.out.println("test"); it prints it 24 times which means it's just looping through the number of characters in the String. How do I get it to compare the actual string and print out the correct words for it?
Code:
import java.io.*;
public class NumStream extends OutputStream
{
public void write(int c) throws IOException
{
System.out.println("test"); //prints "test" the same number of times as I have characters from the pw.println input.
//What goes here?
}
public static void main(String[] args)
{
NumStream ns = new NumStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(ns));
pw.println("123456789 and ! and # ");
pw.flush();
}
}
Re: Subclass and Abstract method implementation
Consider using one of the variations on System.out.print -- or even System.out.print(...) itself. But you'll need to test c and act depending on its value (an if block will work well here). Consider casting it first to (char) and testing the result as a char.
Re: Subclass and Abstract method implementation
That's a Unicode character going into the write method, so there should be no need to convert it to see whether it is a digit.
There's a method on Character that will determine that.
Essentially the Character API is the one you want to be playing with.
This is assuming I've understood the requirements.
eg. the String "123something4" should print out like "onetwothreesomethingfour".