Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Convert Expressions to RPN format using tree

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Convert Expressions to RPN format using tree

    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


    import 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));
     
    	}
     
    }
    code RPN converter.jpgError RPN Converter.jpg
    Last edited by johndoe123; November 15th, 2018 at 10:23 AM. Reason: Made some changes

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Convert Expressions to RPN format using tree

    Please copy the contents of the command prompt window and paste it here to show what you are asking about.
    Add some comments showing what you want the output to be.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    johndoe123 (November 15th, 2018)

  4. #3
    Junior Member
    Join Date
    Nov 2018
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Convert Expressions to RPN format using tree

    I resolved this issue with the repeated the last integer. Thanks to all who took a look at it.

Similar Threads

  1. convert date format from 2017-09-21 to jtextbox
    By androidtvmedan in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 21st, 2017, 10:47 AM
  2. how to convert any date formats into yyyyMMdd format
    By anamika s in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 5th, 2014, 06:41 AM
  3. Convert a file in UTF8 format to UTF16 on command-line
    By gimley in forum Java Theory & Questions
    Replies: 1
    Last Post: July 20th, 2014, 09:05 PM
  4. How to convert date to different format
    By sebE23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 30th, 2013, 03:44 AM
  5. How Convert a Byte array to a image format
    By perlWhite in forum Algorithms & Recursion
    Replies: 7
    Last Post: February 19th, 2011, 03:16 PM