I am trying to fix my code it is duplicating the last output twice
For inputs you must have spaces. I have attached an snippet of the output.
My code is converting 1 + 1 into an RPN format 1 1 +
I am grateful for any help provided
code RPN converter.jpgError RPN Converter.jpgimport java.util.ArrayList; import java.util.Scanner; import java.util.regex.Pattern; public class ToPostFix { static ArrayList<String> al= new ArrayList<String>(); static ArrayList<String> alex= new ArrayList<String>(); static String value; static String exp; public static void main(String[] args) { System.out.println("Enter your inputs"); Scanner in = new Scanner (System.in); String input = in.nextLine(); String[] tokens = input.split("\\s"); in.close(); for (int i=0; i<tokens.length; i++) { validate(tokens[i]); } for (String x : tokens) { al.add(x); } firstcheck(); secondcheck(); thirdcheck(); String[] tokens2= alex.get(alex.size()-1).split("\\s"); TreeNode myTreeNode = new TreeNode(value, null); myTreeNode.setValue((tokens2[1])); myTreeNode.setLeft(new TreeNode(tokens2[0], myTreeNode)); myTreeNode.setRight(new TreeNode(tokens2[2], myTreeNode)); tree(alex,myTreeNode); ToPostFix.postOrder(myTreeNode); } static void firstcheck() { for(int i = 0; i < al.size() - 1; i++) { if(al.get(i).equals("(")) { String exp; exp = al.get(i+1) +" "+ al.get(i+2)+" " + al.get(i+3); alex.add(exp); al.remove(i); al.remove(i); al.remove(i); al.remove(i); al.remove(i); al.add(1, "exp" + (alex.size()-1)); } } } static void secondcheck() { //user regex for that for(int j = 0; j < al.size() - 1; j++) { if(al.get(j).equals("*") || al.get(j).equals("/") || al.get(j).equals("%")) { String exp; exp = al.get(j-1) +" "+ al.get(j)+" " + al.get(j+1); alex.add(exp); al.remove(j - 1); al.remove(j - 1); al.remove(j - 1); al.add(j - 1, "exp" + (alex.size()-1)); } } } static void thirdcheck() { for(int k = 0; k <= al.size() - 1; k++) { if(al.get(k).equals("+") || al.get(k).equals("-")) { String exp; exp = al.get(k-1) +" "+ al.get(k)+" " + al.get(k+1); alex.add(exp); al.remove(k - 1); al.remove(k - 1); al.remove(k - 1); al.add(k-1, "exp" + (alex.size()-1)); k -= 1; } System.out.println(al.size()); } } static void tree(ArrayList<String>alex, TreeNode myTreeNode) { if(myTreeNode.getLeft().getValue().substring(0,1).equals("e")) { String[] tokens2= alex.get(alex.size()-1).split("\\s"); TreeNode newNode = new TreeNode(tokens2[1], myTreeNode); newNode.setLeft(new TreeNode(tokens2[0], newNode)); alex.remove(alex.size()-1); newNode.setRight(new TreeNode(tokens2[2],newNode)); myTreeNode.setLeft(newNode); tree(alex,newNode); } if(myTreeNode.getRight().getValue().substring(0,1).equals("e")) { String[] tokens2= alex.get(alex.size()-1).split("\\s"); TreeNode newNode = new TreeNode(tokens2[1], myTreeNode); newNode.setLeft(new TreeNode(tokens2[0], newNode)); alex.remove(alex.size()-1); newNode.setRight(new TreeNode(tokens2[2],newNode)); myTreeNode.setRight(newNode); tree(alex,newNode); } } static void postOrder(TreeNode myTreeNode) { if(myTreeNode != null) { postOrder(myTreeNode.left); postOrder(myTreeNode.right); System.out.printf( myTreeNode.value); } } static boolean validate (String s) { String regex = "[*/+\\-%]|[()]|\\-?[0-9][\\.0-9]*"; // double forward slash is used to escape, example it would think it was going from 0-9 //* means 0 or more //. means floating point //-? allows negative numbers // ? is used as optional //if (false) //return System.out.println("The input is wrong"); return (Pattern.matches(regex, s)); } }