2014-10-27 92 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轉換爲字符。