0
import java.util.Scanner;
   import java.io.File;
   import java.util.StringTokenizer;
   import java.util.InputMismatchException;
   import java.io.FileNotFoundException;


    public class KashiwabaraNicole16test
   {
   /*******************************************************************************
   *  Outputs integers from user input external files.
   ********************************************************************************/
   
       public static void main( String[] commandlineArguments )throws NullPointerException  {
      //Error Checking For Command Line Arguments..
         if(commandlineArguments.length == 0){
            System.out.println("Please enter the file name as the 1st commandline argument.");
         } 
         else {
         //connect to file (does NOT create new file)
            for(int i=0; i<commandlineArguments.length; i++) {
               File file = new File(commandlineArguments[i]);
            
               Scanner inputFromFile = null;
            
               try {
                  inputFromFile = new Scanner(file);
               } 
                   catch (FileNotFoundException exception) {
                  //Print error message.
                  
                     System.out.print("ERROR: File not found for \"");
                     System.out.println(commandlineArguments[i]+"\"");
                  
                  
                     return;
                  }
            
               String [] myArray = new String[2000];// Create the array to hold all elements that are found in the file
            
               int count = 0;  // number of elements
            
               count = createOutput( inputFromFile, myArray ); // adds each element into array
            
               System.out.println( "Reading from file \"" + commandlineArguments[i] + "\":" );             
                       
               displayArray( myArray, count); // calls displayArray method
            
            }//end of "if" for connecting to file
         }//end of "else" for commandlineArguments
      }//end of main() method
   
       public static int createOutput( Scanner myScanner, String [] stringArray ) {
         int count = 0; // number of elements
      
         String element; //variable of each element
      
      
         while( myScanner.hasNext() )// Loops until end of file
         {
            
            element = myScanner.next();
             
          
            stringArray[ count ] = element; //stores the element into the array using count as the index
            count++; // increase 1
         }
         return count; //returns total number of elements found in the file
      }
   
   
   
       public static void displayArray( String [] stringArray, int count) {
      // For loop to step through each item in the array
      
         for( int i = 0; i < count; i++ ) {
            
                       // Print the index and the corresponding element value
            System.out.println( "Infix expression = " + stringArray[i] );
         	
            String postFixExpression = infixToPostfix(stringArray[i]);
         	
         	
            System.out.println("Postfix expression = " + postFixExpression);
         }
      }
    
      
       public static String infixToPostfix(String infix){
       
         String postfix = "";
      	      //here is the algorithm that is described in the slides 47-49
      //step 1: get parameter infix
         String stringArray = infix;
         StackInterface<String> stack 
            = new ArrayStack<String>();
      
      
                     //step 2: loop through String infix
         for (int i = 0; i < infix.length(); i++)
         {				           
            char oneElement = infix.charAt(i);
            if (oneElement == '(')
               stack.push(infix);
            if (oneElement == '0' || oneElement == '1' || oneElement == '2'|| oneElement == '3' || oneElement == '4'|| oneElement == '5'|| oneElement == '6'||oneElement == '7'
            ||oneElement == '8'||oneElement == '9')
               stack.push(infix);
            
            if (oneElement == '+')
               stack.push(infix);
               
            if (oneElement == '-')
               stack.push(infix);
               
            if (oneElement == '*')
               stack.push(infix);
               
            if (oneElement == '%')
               stack.push(infix);
            if (oneElement == ')')
            {
               stack.pop();
               postfix = postfix + oneElement;
            }
            else
               postfix = postfix + oneElement; 
         	String newLine = new String();
            for (int j = 0; j < postfix.length(); j++)
            {
               
               newLine  = newLine + stack.pop();
            }
            while (stack.empty()) {
               postfix = postfix + stack.pop();

              
            }
         
                     			
          
         
         }  
      //step 2a: get the next character
      //step 2b: if "(" then push character on stack
      //step 2c: if integer, then add to end of String postfix
      //step 2d: if operator, then push it on stack
      //step 2e: (see slides for description)
      //step 3: return String postfix
         return postfix;
      }
   	
   }// end of class
2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by Grn Xtrm
0

Do you have a question or are you just showing off your code? ;)
Why the double post? This code was probably meant for your other thread, yes?

Edited by Grn Xtrm: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.