I've got this bug in my progrom that I cannot figure out and it's due within the next couple of hours. I was hoping someone could help.

My problem is in my main function that reads in from standing input
but when the user types in $PART2 it is suppose to break and go onto
the next input. But whenever I type in:

C = 7
D = 6

the function will not go on to the next part because it should print
System.out.println("Enter PostFix Expression: ")

If you could help me figure out why I would greatly appreciate it.

import java.util.*;
import java.io.*;
import java.util.Scanner;
import java.util.Stack;
import java.lang.Character;

class PostEval2
static Stack<Integer> postFix = new Stack<Integer>(); // a new stack of integers 
public static final int integers=26;         // the max number of integers for the array
static int [] array = new int[integers];   // a new array 

// int addr(char ch) returns the equivalent interger address
// for the letter given in ch, 'A' returns 0, 'Z' returns 25 and all
// other letters return their corresponding position as well.

public static int addr(char ch)
  return (int)ch-(int)'A';

// void eval( char letter) will evaluate the postfix expression
// from what the user entered. If will make sure that letter is a digit
// then it will begin doing the equations for whatever operand is used.
// it will final push the result into the stack.
public static void eval(char letter)
   int result = 0;
   int code1 = 0;
          int operand1 = postFix.pop();
          int operand2 = postFix.pop();
          if (letter == '+') result = operand1 + operand2;
          else if (letter == '-') result = operand1 - operand2;
          else if (letter == '*') result = operand1 * operand2;
          else if (letter == '/') result = operand1 / operand2;

// void splitString(String postInput, char letter) will
// will convert the inputed postfix expression into individual
// characters then evaulate each character. It will error
// handle to see if there is more than one item left 
// in the stack

public static void splitString (String postInput, char letter)
         System.out.println(postFix+ "="); // the program was ended so print out the equation then figure out the answer
         int size = postInput.length();  //get the lenght of the input
         for (int i = 0; i<=size; i++)   // brake each character into into indivdual characters
           letter = postInput.charAt(i);
           eval(letter);               // evaluate the postfix expression
           if(postFix.size()>1)          // error check to see if the size is greater than 1
             while(postFix.size() > 0)   // if it is then there is more than one item in the stack and the problem is not solved
             System.out.println("The stack contains more than one item");
           else if(postFix.size()==1)  // but if there only one item in the stack then it is solved. and print it out

 static public void main(String args[])
    Scanner scanner = new Scanner(System.in);         //begin standard input
    System.out.println("Input Letter and Number it equals: "); // enter letters and numbers
    String postInput;   // the  postFix input
    char letter=0;    // the letter entered by the user
    String equals;   // the character to read in the equals sign
    int number = 0;    // the number the letter equals
    int code1 = 0;      // code will get the ASC from the addr function
             letter=(scanner.next()).charAt(0);   // get the letter from the user input
System.out.println("Test1 ");            
            // if a user enters $PART2 begin the postfix read in
System.out.println("Test% ");  
             equals = scanner.next();             // the user is suppose to enter a = symbol
System.out.println("Test2 ");  
             code1=addr(letter);                   // get the value of the operand by using the addr function
System.out.println("Test3 ");  
             array[code1]=scanner.nextInt();       // we then put the code into the array    
System.out.println("Test4 ");  
     while(scanner.hasNext())  // this is part two of the postfix evaluation
          System.out.println("Enter PostFix Expression: "); // signifys the postfix part
             postInput=scanner.nextLine();  // get a string of the postfix expression
             if(postInput.equals("$END"))break; // if the user enters $END then the program is done
             splitString(postInput, letter); // insert the string and the letter into the splitstring operation and begin the evaulation


So if they type in $PART2, you WANT it to print error postfix expression?

edit: It probably isn't working because you didn't prompt the user for input before the second loop. So it says while(scanner.hasNext()), but you never prompted the user (using a println statement), so it never entered that loop. Good luck.

This article has been dead for over six months. Start a new discussion instead.