How to reverse Greek letters with BufferedReader in Java?

929 views Asked by At

I am new to Java and I have this Reverse code. My question is how can I use BufferedReader in order to when I write greek letters (like ξηφαιπασς) it will also return reversed greek letters. At the moment all I get is some squares. First time I write a question here so please forgive me for any mistake.

import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class StackX
{
private int maxSize;
private char[] stackArray;
private int top;
//------------------------------------------------------------- public StackX(int max) // constructor
{
maxSize = max;
stackArray = new char[maxSize];
top = -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
3

There are 3 answers

0
Panagiotis Giannakis On BEST ANSWER

change UTF8 to ISO-8859-7

Change

BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF8" ));

To

BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-7"));

I hope this will help you

0
Kartic On

If I get your question, you are basically trying to reverse a String and you are facing problem with unicode characters. Why don't you reverse using reverse() method of StringBuilder class -

String result=new StringBuilder("ξηφαιπασς").reverse().toString();
0
mrres1 On

You can use a library to check the encoding of the entered text: http://code.google.com/p/juniversalchardet/