參考Converting Letters to Numbers轉換字母與數字2
在文件test.in.rtf
,我已經 'ABCD' 類型。但是,當我運行該程序時,我在test.out.rtf
中獲得了??? ??????????? ???????? plus maybe a few more
。爲什麼是這樣?我錯過了什麼嗎?
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("test.in.rtf"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out.rtf")));
StringTokenizer st = new StringTokenizer(f.readLine());
StringBuilder sb = new StringBuilder();
for (char c : st.nextToken().toCharArray()) {
sb.append((char)(c - 'a' + 1));
}
out.println(sb); // output result
out.close(); // close the output file
System.exit(0);
}
}
我試圖將字母轉換爲數字,其中a = 1,b = 2,c = 3,d = 4,z = 26 ... – Anonymous181
這是爲了樂趣 – Anonymous181
調試是樂趣的一部分:) –