Results 1 to 3 of 3
  1. #1
    konman795 is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default 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?

    Java 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();
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    Last edited by Fubarable; 02-24-2012 at 03:36 AM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default 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".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Abstract class, implementation, getName()?
    By manalinik in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 03:08 PM
  2. Accessing abstract class subclass's data members
    By Claymz in forum New To Java
    Replies: 23
    Last Post: 04-18-2011, 12:26 PM
  3. Replies: 4
    Last Post: 04-14-2011, 12:42 PM
  4. Replies: 4
    Last Post: 02-19-2011, 05:32 AM
  5. Replies: 2
    Last Post: 11-21-2008, 01:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •