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. How do I get it to compare the actual string and print out the correct words for it?

import java.io.*;

public class NumStream extends OutputStream
{
    public void write(int c) throws IOException
    {  
        //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();
    }
}

Recommended Answers

All 2 Replies

So I have actually made some progress here. The problem I'm getting now is my output is showing the numerical representation of the words from pw.println. How do I get it show the actual word and not the integer representation?

import java.io.*;

public class NumStream extends OutputStream
{
    public void write(int c) throws IOException
    {  
        StringBuffer sb = new StringBuffer();
        switch(c)
        {
            case ' ': sb.append(" ");
                break;
            case '1': sb.append("One");
                break;
            case '2': sb.append("Two");
                break;
            case '3': sb.append("Three");
                break;
            case '4': sb.append("Four");
                break;                
            case '5': sb.append("Five");
                break; 
            case '6': sb.append("Six");
                break;
            case '7': sb.append("Seven");
                break;
            case '8': sb.append("Eight");
                break;     
            case '9': sb.append("Nine");
                break; 
            case '0': sb.append("Zero");
                break;
            default:    sb.append(Integer.toString(c));
                break;
        }
        System.out.print(sb);
    }
    public static void main(String[] args) 
    {
        NumStream ns = new NumStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(ns));
        pw.println("123456789 and ! and # ");
        pw.flush();
    }
}

The output I'm getting is:

OneTwoThreeFourFiveSixSevenEightNine 97110100 33 97110100 35 1310

Member Avatar for ztini

So, you can actually just cast your int value to a char. The int provided for the write(int) is the decimal value of the character.

Here's a full list of characters (0-255): http://mindprod.com/jgloss/ascii.html

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;


public class NumStream extends OutputStream {

	public void write(int c) throws IOException {  	
		String out = null;
		switch (c) { 
		case '0': out = "zero"; 	break;
		case '1': out = "one"; 		break;
		case '2': out = "two"; 		break;
		case '3': out = "three";	break;
		case '4': out = "four"; 	break;
		case '5': out = "five"; 	break;
		case '6': out = "six"; 		break;
		case '7': out = "seven"; 	break;
		case '8': out = "eight"; 	break;
		case '9': out = "nine"; 	break;
		}
		System.out.print(out == null ? (char) c : out);		
	}
	
	public static void main(String[] args) { 
		PrintWriter pw = new PrintWriter(new NumStream());
		pw.println("0123456789 and ! and # ");
		pw.flush();
	}
}

output:

zeroonetwothreefourfivesixseveneightnine and ! and #
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.