hey guys.. howdy?
i am trying to compile my program however i'm having a hard time. it says there's an error. im a newbie so imma post it here the codes... please help me if you can ^^
import java.io.*; import java.util.Stack; import java.io.IOException; /** *Check if a string is a palindrome * */ public class Palindrome{ private Stack theStack; private String input; private String output = ""; public Palindrome(String in) { input = in; int stackSize = input.length(); theStack = new Stack(); } public String isPalindrome(){ boolean chk = false; int midpoint = input.length() / 2; if(input.length() % 2 == 1){ midpoint = input.length() / 2 + 1; } for(int i=0; i < input.length() / 2; i++){ char ch = input.charAt(i); theStack.push(ch); System.out.println(input.charAt(i)); } for(int j=midpoint; j < input.length(); j++){ System.out.println(input.charAt(j)); if(!theStack.empty() && input.charAt(j) == theStack.peek()){ theStack.pop(); chk = true; }else{ chk = false; } } if(chk){ output = "The String is a palindrome"; }else{ output = "The String is not a palindrome"; } //System.out.println(midpoint); return output; } public static void main(String[] args) throws Exception{ BufferedReader sr = new BufferedReader(new InputStreamReader(System.in)); String output; System.out.print("Please enter a string: "); String inputPalindrome = sr.readLine(); Palindrome theTrans = new Palindrome(inputPalindrome); output = theTrans.isPalindrome(); System.out.println(output); } }
import java.util.*; import java.io.IOException; /** *Evaluates a postfix expression * */ public class evalPostfix{ private static final String operators = "+-*/ "; private int operate(String op, int op1, int op2){ if (op.equals("+")) { return op1 + op2; } if (op.equals("*")){ return op1 * op2; } if (op.equals("-")){ return op1 - op2; } if (op.equals("/")){ return op1 / op2; } throw new IllegalArgumentException("unrecognized operator "+op); } public int evaluate(String s){ Stack stack = new Stack(); StringTokenizer tokens = new StringTokenizer(s,operators,true); while (tokens.hasMoreTokens()){ String t = tokens.nextToken(); if (operators.indexOf(t) >= 0){ if (t.equals(" ")) continue; int right = Integer.parseInt((String) stack.pop()); int left = Integer.parseInt((String) stack.pop()); stack.push(""+operate(t,left,right)); } else { stack.push(t); } } int result = Integer.parseInt((String) stack.pop()); if (stack.size() > 0){ throw new IllegalArgumentException("non empty stack on "+s); } return result; } public static void main(String[] args){ String expression = " 25 10 * 50 + 50 - 50 /"; if (args.length > 0){ expression = args[0]; } evalPostfix p = new evalPostfix(); int result = p.evaluate(expression); System.out.println("value of "+expression+" = "+result); } }
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; public class GUI2 extends JFrame implements KeyListener { JButton buttonAccept; JTextField textLetter, textFix; JList listComponentProduct; DefaultListModel listModelProduct; public GUI2() { createGUI(); } public void createGUI() { JPanel panel, panelSearch; JLabel mainLabel, labelColumnHeader, labelA, labelB, labelC, labelD, labelE, labelF, labelG, labelH; Font f = new Font("Cambria", Font.PLAIN, 18); GridBagConstraints gbc = new GridBagConstraints(); listModelProduct = new DefaultListModel(); listComponentProduct = new JList(listModelProduct); listComponentProduct.setFont(f); mainLabel = new JLabel(" M E N U "); mainLabel.setFont(f); labelA = new JLabel(""); labelB = new JLabel("A. Evaluate a postfix expression. "); labelC = new JLabel("B. Convert from infix to postfix expression. "); labelD = new JLabel("C. Check if a string is a palindrome. "); labelE = new JLabel("D. Quit "); labelF = new JLabel("Letter of your choice: "); buttonAccept = new JButton("Accept"); textLetter = new JTextField(1); textLetter.setEnabled(true); textLetter.addKeyListener(this); textLetter.setFont(f); panelSearch = createSearchPanel(); panel = new JPanel(); panel.setLayout(new GridBagLayout()); gbc.insets = new Insets(2, 2, 2, 2); gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx=0; gbc.gridy=0; panel.add(mainLabel, gbc); gbc.gridx=0; gbc.gridy=8; panel.add(textLetter, gbc); gbc.gridx=0; gbc.gridy=6; panel.add(new JSeparator(), gbc); gbc.gridx=0; gbc.gridy=1; panel.add(labelA, gbc); gbc.gridx=0; gbc.gridy=2; panel.add(labelB, gbc); gbc.gridx=0; gbc.gridy=3; panel.add(labelC, gbc); gbc.gridx=0; gbc.gridy=4; panel.add(labelD, gbc); gbc.gridx=0; gbc.gridy=5; panel.add(labelE, gbc); gbc.gridx=0; gbc.gridy=7; panel.add(labelF, gbc); gbc.gridx=0; gbc.gridy=9; panel.add(buttonAccept, gbc); setTitle("Group Exercise"); setContentPane(panel); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); setVisible(true); } public JPanel createSearchPanel() { JPanel panel; GridBagConstraints gbc = new GridBagConstraints(); Font f = new Font("Lucida Console", Font.PLAIN, 22); ButtonGroup buttonGroup; panel = new JPanel(); panel.setLayout(new GridBagLayout()); gbc.insets = new Insets(2,2,2,2); gbc.anchor = GridBagConstraints.WEST; gbc.gridx=1; gbc.gridy=10; panel.add(buttonAccept, gbc); return panel; } public void LetterB(){ BufferedReader sr = new BufferedReader(new InputStreamReader(System.in)); try{ System.out.println("Please enter an infix expression to convert to postfix. Separate the terms by a space: "); String inputInfix = sr.readLine(); String outputPostfix; InToPost infixTrans = new InToPost(inputInfix); outputPostfix = infixTrans.doTrans(); System.out.println("Postfix is " + outputPostfix + '\n'); } catch(Exception e){ e.printStackTrace(); } } public void LetterC(){ BufferedReader sr = new BufferedReader(new InputStreamReader(System.in)); try { System.out.print("Please enter a string: "); String inputPalindrome = sr.readLine(); String outputPalindrome; Palindrome chkString = new Palindrome(inputPalindrome); outputPalindrome = chkString.isPalindrome(); System.out.println(outputPalindrome); } catch(Exception e){ e.printStackTrace(); } } public void LetterD(){ System.exit(0); } public void keyPressed(KeyEvent ke) {} public void keyTyped(KeyEvent ke) {} public void keyReleased(KeyEvent ke) { } // ActionListener public static void main(String[] args) { new GUI2(); }}
package GUI4; import java.io.IOException; /** *Converts a infix expression to postfix * */ public class InToPost { private Stack theStack; private String input; private String output = ""; public InToPost(String in) { input = in; int stackSize = input.length(); theStack = new Stack(stackSize); } public String doTrans() { for (int j = 0; j < input.length(); j++) { char ch = input.charAt(j); switch (ch) { case '+': case '-': gotOper(ch, 1); break; // (precedence 1) case '*': // it's * or / case '/': gotOper(ch, 2); // go pop operators break; // (precedence 2) case '(': // it's a left paren theStack.push(ch); // push it break; case ')': // it's a right paren gotParen(ch); // go pop operators break; default: // must be an operand output = output + ch; // write it to output break; } } while (!theStack.isEmpty()) { output = output + theStack.pop(); } //System.out.println(input); return output; // return postfix } public void gotOper(char opThis, int prec1) { while (!theStack.isEmpty()) { char opTop = theStack.pop(); if (opTop == '(') { theStack.push(opTop); break; }// it's an operator else {// precedence of new op int prec2; if (opTop == '+' || opTop == '-') prec2 = 1; else prec2 = 2; if (prec2 < prec1) // if prec of new op less { // than prec of old theStack.push(opTop); // save newly-popped op break; } else // prec of new not less output = output + opTop; // than prec of old } } theStack.push(opThis); } public void gotParen(char ch){ while (!theStack.isEmpty()) { char chx = theStack.pop(); if (chx == '(') break; else output = output + chx; } } public static void main(String[] args) throws IOException { String input = "A + B"; String output; InToPost theTrans = new InToPost(input); output = theTrans.doTrans(); System.out.println("Postfix is " + output + '\n'); } class Stack { private int maxSize; private char[] stackArray; private int top; public Stack(int max) { maxSize = max; stackArray = new char[maxSize]; top = -1; } public void push(char j) { stackArray[++top] = j; } public char pop() { return stackArray[top--]; } public char peek() { return stackArray[top]; } public boolean isEmpty() { return (top == -1); } } }
everything goes well except with the code 3 and 5 im having an error "cannot access InToPost"import java.util.Stack; import java.io.*; /** *This applet is a menu-driven application program that uses the Stack data structure. *It has the following applications: *-Evaluates a postfix expression *-Converts a infix expression to postfix *-Check if a string is a palindrome * * */ public class MidGroupAct{ public static void main(String[] args ) throws Exception { BufferedReader sr = new BufferedReader(new InputStreamReader(System.in)); char input; boolean quit = false; double y, x, z; Stack operands = new Stack(); while (!quit) { System.out.println(); System.out.println(" M E N U "); System.out.println("|---------------------------------------------|"); System.out.println("|A. Evaluate a postfix expression. |"); System.out.println("|B. Convert from infix to postfix expression. |"); System.out.println("|C. Check if a string is a palindrome. |"); System.out.println("|D. Quit |"); System.out.println("|---------------------------------------------|"); System.out.println(); System.out.print("Please enter the letter corresponding to your choice: "); input = sr.readLine().charAt(0); switch (input){ case 'A': case 'a': System.out.println("Please enter a postfix expression to evaluate. Separate the terms by a space: "); String inputPostfix = sr.readLine(); if (args.length > 0){ inputPostfix = args[0]; } evalPostfix p = new evalPostfix(); double outputEvaluated = p.evaluate(inputPostfix); System.out.println("The value of the expression is " + outputEvaluated); break; case 'B': case 'b': System.out.println("Please enter an infix expression to convert to postfix. Separate the terms by a space: "); String inputInfix = sr.readLine(); String outputPostfix; InToPost infixTrans = new InToPost(inputInfix); outputPostfix = infixTrans.doTrans(); System.out.println("Postfix is " + outputPostfix + '\n'); break; case 'C': case 'c': System.out.print("Please enter a string: "); String inputPalindrome = sr.readLine(); String outputPalindrome; Palindrome chkString = new Palindrome(inputPalindrome); outputPalindrome = chkString.isPalindrome(); System.out.println(outputPalindrome); break; case 'D': case 'd': System.exit(0); default: System.out.println("Invalid character. Please enter A, B, C or D"); } } sr.close(); } }
thanks