转换整数再一个字presentation回字符串 [英] Converting the integer representation of a word back to a String

查看:107
本文介绍了转换整数再一个字presentation回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个家庭作业,我有我的输出麻烦。一切正常,除了Integer.toString()不给我我想要的结果。当我想他们转换为实际的话它仍然只是输出一串数字。这里的code和输出:

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

输出:OneTwoThreeFourFiveSixSevenEightNine 97110100 33 97110100 35 1310

the output is: OneTwoThreeFourFiveSixSevenEightNine 97110100 33 97110100 35 1310

有人可以告诉我如何格式化code在这个论坛更容易?我不得不手动8空间缩进每一行,并有一定是更简单的方法!

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!

推荐答案

对于不是数字的字符,你正在做的字符code并将其转换为数字。因此,97 110和100字符codeS为'A','N'和'D',而33和35是

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;

请注意,每个写入程序被调用时创建新的StringBuffer是极其低效浪费。因为你永远只追加一根弦/ CHAR到它,你可能也只是打印字符串/字符,而不是直接通过一个StringBuffer复制。

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.

这篇关于转换整数再一个字presentation回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆