Friday, March 27, 2015

Reverse String, reverse words order, reverse chars in words

This example show various methods to reverse chars in String, reverse words order, and reverse chars in words but keep words in order.


package javareversestring;

import java.util.List;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaReverseString {

    public static void main(String[] args) {
        String original;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a string:");
        original = scanner.nextLine();
        
        String reverseString = "";
        for(int i=original.length()-1; i>=0; i--){
            reverseString= reverseString + original.charAt(i);
        }
        System.out.println(reverseString);
        
        System.out.println(new StringBuffer(original).reverse().toString());
        System.out.println(reverseWords(original));
        System.out.println(reverseCharsInWords(original));
    }

    //reverse words
    static private String reverseWords(String src){
        String reversed = "";
        
        Stack<String> stackString = new Stack<>();
        StringTokenizer stringTokenizer = new StringTokenizer(src);
        
        while(stringTokenizer.hasMoreTokens()){
            stackString.push(stringTokenizer.nextToken());
        }
        
        while(!stackString.empty()){
            reversed += stackString.pop() + " ";
        }
        
        return reversed;
    }
    
    //reverse chars in words
    static private String reverseCharsInWords(String src){
        String reversed = "";
        
        List<String> listString = new Stack<>();
        StringTokenizer stringTokenizer = new StringTokenizer(src);
        
        while(stringTokenizer.hasMoreTokens()){
            listString.add(stringTokenizer.nextToken());
        }
        
        while(!listString.isEmpty()){
            String s = listString.remove(0);
            reversed += new StringBuffer(s).reverse().toString() + " ";
        }
        
        return reversed;
    }
}

Thursday, March 26, 2015

Professional Java EE Design Patterns

Professional Java EE Design Patterns

Master JavaEE Design Pattern implementation to improve your coding efficiency Professional JavaEE Design Patterns is the ultimate guide to working more efficiently with JavaEE, and the only resource that covers both the theory and application of design patterns in solving real-world problems. The authors guide readers through both the fundamentals and little-known secrets of JavaEE6/7, presenting patterns throughout, and demonstrating how they are used in day-to-day programming. As the standard computing platform in community-driven enterprise software, JavaEE provides an API and runtime environment above and beyond JavaSE. Written for the experienced JavaEE developer hoping to improve code quality and efficiency, the book contains time saving patterns, including: Implementation and problem-solving with design patterns Connection between existing non-JavaEE design patterns and new JavaEE concepts Unique organization that prioritizes the most relevant patterns before expanding into advanced techniques Individually-based focus that fully explores each pattern Unlike most JavaEE books that simply offer descriptions or recipes, this book drives home the actual implementation and application to keep square pegs away from round holes. For the programmer looking for a comprehensive guide that is actually useful in the everyday workflow, Professional JavaEE Design Patterns is the definitive resource on the market.

Tuesday, March 24, 2015

Print Unicode in Java

Example to print unicode on Java:

package javaunicode;

public class JavaUniCode {

    public static void main(String[] args) {
        String unicode = "\u01FC";
        System.out.println(unicode);
        System.out.println("Ǽ");
        System.out.println("\u03A9");
        System.out.println("Ω");
    }
}