我是Java新手,我有这个反向代码。我的问题是如何使用BufferedReader以便在我写希腊字母时(如ξηφαιπασς)它还将返回反向希腊字母。我得到的只是一些正方形。我第一次在这里写一个问题所以请原谅我有任何错误。
import java.io. *; //用于I / O
////////////////////////////////////////////////// //////////////
class StackX
{
private int maxSize;
private char [] stackArray;
private int top;
// ------------------------------------------------ ------------- public StackX(int max)//构造函数
{
maxSize = max;
stackArray = new char [maxSize];
顶部= -1;
}
// ------------------------------------------------ -------------public char pop() // take item from top of stack { return stackArray[top--]; } //-------------------------------------------------------------> public boolean isEmpty() // true if stack is empty { return (top == -1); } //------------------------------------------------------------- } // end class StackX //////////////////////////////////////////////////////////////// class Reverser { private String input; // input string private String output; // output string > public Reverser(String in) // constructor { input = in; } //-------------------------------------------------------------> public String doRev() // reverse the string { int stackSize = input.length(); // get max stack size StackX theStack = new StackX(stackSize); // make stack for(int j=0; j<input.length(); j++) { char ch = input.charAt(j); // get a char from input theStack.push(ch); // push it } output = ""; while( !theStack.isEmpty() ) { char ch = theStack.pop(); // pop a char, output = output + ch; // append to output } return output; } // end doRev() //-------------------------------------------------------------> } // end class Reverser //////////////////////////////////////////////////////////////// class ReverseApp { public static void main(String[] args) throws IOException { String input, output; while(true) { System.out.print("Enter a string: "); System.out.flush(); input = getString(); // read a string from kbd if( input.equals("") ) // quit if [Enter] break; // make a Reverser Reverser theReverser = new Reverser(input); output = theReverser.doRev(); // use it System.out.println("Reversed: " + output); } // end while } // end main() //------------------------------------------------------------- public static String getString() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF8" )); String s = br.readLine(); return s; } //------------------------------------------------------------- } // end class ReverseApp
答案 0 :(得分:0)
将UTF8更改为ISO-8859-7
更改
BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF8" ));
要
BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-7"));
我希望这会对你有所帮助
答案 1 :(得分:0)
如果我收到您的问题,您基本上是在尝试撤消字符串,并且您遇到了unicode字符的问题。为什么不使用StringBuilder类的reverse()方法反转 -
String result=new StringBuilder("ξηφαιπασς").reverse().toString();
答案 2 :(得分:0)
您可以使用库来检查输入文本的编码:http://code.google.com/p/juniversalchardet/