Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7536267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:31:38+00:00 2026-05-30T06:31:38+00:00

This is a homework assignment and I’m having trouble with my output. Everything works

  • 0

This is a homework assignment and I’m having trouble with my output. Everything works as expected except the Integer.toString() isn’t giving me the result I want. It is still outputting just a bunch of numbers when I want them to be converted to the actual word. Here’s the code and output:

    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 is: OneTwoThreeFourFiveSixSevenEightNine 97110100 33 97110100 35 1310

can somebody please tell me how to format code easier in this forum? I had to manually 8 space indent each line and there’s got to be an easier way!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:31:39+00:00Added an answer on May 30, 2026 at 6:31 am

    For characters that aren’t digits, you’re taking the character code and converting it to a number. So 97 110 and 100 are the character codes for ‘a’, ‘n’, and ‘d’ while 33 and 35 are ! and #.

    What you probably want for your default case is just:

    default: sb.append((char)c); break;
    

    Note that creating a new StringBuffer each time the write routine is called is extremely wasteful and inefficient. Since you’re only ever appending one string/char to it, you might as well just print that string/char directly rather than copying through a StringBuffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this sounds like a homework assignment, but it isn't. Lately I've been
(No, this isn't a homework assignment, I just found the bug and thought it
I am working on a homework assignment, and I have almost everything done except
This is part of a homework assignment. I've got several questions asking find the
Disclaimer: This is for a homework assignment, but the question is not regarding the
I'm doing a basic homework assignment which looks like this: While input <> -1
(this is indirectly a part of a much larger homework assignment) I have something
Full disclosure : This is for a homework assignment. This is driving me nuts.
Basically i am having problems with my code - this is homework so would
This was a homework assignment for my students (I am a teaching assistant) in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.