简体   繁体   English

将单词的整数表示形式转换回字符串

[英]Converting the integer representation of a word back to a String

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. 一切都按预期工作,除了Integer.toString()没有给我想要的结果。 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 输出是:OneTwoThreeFourSixSevenEightNine 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! 我必须手动对每行8个空格进行缩进,并且必须有一种更简单的方法!

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 ! 因此97 110和100是'a','n'和'd'的字符代码,而33和35是! 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. 请注意,每次调用写入例程时都创建一个新的StringBuffer非常浪费且效率低下。 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. 由于只向其添加一个字符串/字符,因此您最好直接打印该字符串/字符,而不是通过StringBuffer复制。

您正在输出sb.append(Integer.toString(c))中不是数字的字符的ASCII码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM