Update Main.java

reduce code redundancy.