2014-10-27 93 views
0

我尝试了多个版本,包括在StackOverflow上找到的几个解决方案,但我总是获取数字而不是控制台中的字符。对于我的uni中的作业,我们需要反转字符串中的字符。但创建新的字符串似乎并不那么容易。Java字符到字符串

我试图用一个StringBuilder,

StringBuilder builder = new StringBuilder(); 
// ... 
builder.append(c); // c of type char 

字符串连接,

System.out.print("" + c); // c of type char 

甚至将String.valueOf(),

System.out.print(String.valueOf(c)); // c of type char 

,并用显式转换再次他们每个人到char。但我总是得到序列中字符的序号,而不是控制台中输出的实际字符。我如何正确地从char s建立一个新的字符串?

/** 
* Praktikum Informatik - IN0002 
* Arbeitsblatt 02 - Aufgabe 2.6 (Buchstaben invertieren) 
*/ 

public class H0206 { 

    public static String readLine() { 
     final StringBuilder builder = new StringBuilder(); 
     try { 
      // Read until a newline character was found. 
      while (true) { 
       int c = System.in.read(); 
       if (c == '\n') 
        break; 
       builder.append(c); 
      } 
     } 
     catch (java.io.IOException e) { 
      ; // We assume that the end of the stream was reached. 
     } 
     return builder.toString(); 
    } 

    public static void main(String[] args) { 
     // Read the first line from the terminal. 
     final String input = readLine(); 

     // Create a lowercase and uppercase version of the line. 
     final String lowercase = input.toLowerCase(); 
     final String uppercase = input.toUpperCase(); 

     // Convert the string on the fly and print it out. 
     for (int i=0; i < input.length(); ++i) { 
      // If the character is the same in the lowercase 
      // version, we'll use the uppercase version instead. 
      char c = input.charAt(i); 
      if (lowercase.charAt(i) == c) 
       c = uppercase.charAt(i); 
      System.out.print(Character.toString(c)); 
     } 
     System.out.println(); 
    } 

} 
+4

'char'类型的'c'?我在你的例子中看到的是一个'int'。另外,'System.in.read()'做了什么? – 2014-10-27 15:36:16

+0

['String#valueOf()'shoud work](http://stackoverflow.com/questions/8172420/how-to-convert-a-char-to-a-string-in-java)...你是否确定你的角色不是数字,例如''4''? – sp00m 2014-10-27 15:38:24

+0

'c'可以保存一个字符数据,但肯定是'int'类型而不是'char'。像这样将它转换为char:'char ch =(char)c;'。 – icza 2014-10-27 15:40:18

回答

0

下面是一个示例如何反转一个字符串,还有另外一个选择,我觉得这个是比较说教

public static void main(final String[] args) { 
    String text = "This is a string that will be inverted"; 
    char[] charArray = text.toCharArray(); 
    char[] invertedCharArray = new char[charArray.length]; 
    for (int i = 1; i <= charArray.length; i++) { 
     char c = charArray[charArray.length - i]; 
     invertedCharArray[i - 1] = c; 
    } 
    System.out.println(text); 
    System.out.println(new String(invertedCharArray)); 
} 
2

我与你提供的示例代码中看到的问题是在这里:

int c = System.in.read(); 
if (c == '\n') 
    break; 
builder.append(c); 

您将调用方法Stringbuilder.append(int)。正如javadoc所说,“整体效果就好像参数通过String.valueOf(int)方法转换为字符串,然后该字符串的字符被附加到该字符序列中”。铸造整数值为char这样会导致所需的行为:

int c = System.in.read(); 
if (c == '\n') 
    break; 
builder.append((char) c); 
0
try { // Read until a newline character was found. 
     while (true) { 
      int c = System.in.read(); 
      if (c == '\n') break; 
      builder.append(c); 
     } 

从你给的样品,我可以看到,这是造成问题的原因。由于您正在读取char输入作为int,因此它会将字符转换为其序数值,以便可以将其作为整数进行存储(因此可以使用)。

public final class StringBuilder你打电话给append(int i),它返回int。如果需要int c = System.out.read(); 以将其声明为整数,则可以将c转换为char。

char ch = (char)c; 
builder.append(ch) 

如果需要,这留下C作为一个整数,并存储它的“原始值”(这是什么,然后将其变成了int,即按下的键)的一个变量,如果需要。如果您只需要将其添加到字符串中,而不出于任何原因重新使用它,则可以使用append((char) c)直接将c转换为字符。