Case study : infix to postfix conversion, i don't really know how i could make codes, I can understand what is the meaning of infix and postfix but when it comes of making codes i really have a hard time with it.
I need to convert infix To Postfix but have a few errors.
Error msg:
PostFix: Exception in thread "main" java.util.EmptyStackException at java.util.Stack.peek(Unknown Source) at java.util.Stack.pop(Unknown Source) at assignment4.infixToPostfix.evaluatePostfix(infixTo Postfix.java:129) at assignment4.infixToPostfix.main(infixToPostfix.jav a:19)
NB: 129 >>return (double) s1.pop(); AND 19>>>double ans = evaluatePostfix(postfixStr); Deadline = less than 1 hour
So my code works perfectly when I input (a+(c-d) and i get ab+cd- for postfix and *+ab-cd for prefix. However when I input a+b+c for infix i receive abc++ postfix and +a+bc prefix when its supposed to be ab+c+ postfix and ++a b c prefix. So my issue is that any infix input with parenthesis, it converts them correctly, however without parenthesis it does not convert correctly.
import java.util.*; public class stack { public static char[] convertToPostfix(char[] infixEx) { Stack<Character> operatorStack = new Stack<Character>(); char[] postfix = new char[infixEx.length]; int index = 0;
So I am supposed to be changing infix notation to postfix notation using stacks. This is simply taking a string "3 + 5 * 6" (infix) and turning it into (3 5 6 * +" (postfix).
To do this, we are to scan the string from left to right and when we encounter a number, we just add it to the final string, but when we encounter an operand, we throw it on the stack. Then if the next operand has a higher input precedence than the stack precedence of the operator on the top of the stack, we add that operator to the stack too, otherwise we pop from the stack THEN add the new operator.
I am supposed to be utilizing a hash map but I don't see how you would go about doing this. We are supposed to store operators on the hash map but operators need their own character, input precedence, stack precedence, and rank. How do you use a hash map when you need to tie a character to 3 values instead of just 1? I just don't get it.
The following is our Operator class that we are to use. Another problem is this isn't really supposed to be modified, yet we were given two important variables (inputPrecedence and outputPrecedence) that we can't have nothing to be initialized to and no way of accessing? So that might be where a hash map comes in but I am not sure. I am not very sure on how they exactly work anyway...
public class Operator implements Comparable<Operator> { public char operator; // operator privateint inputPrecedence; // input precedence of operator in the range [0, 5] privateint stackPrecedence; // stack precedence of operator in the range [-1, 3]
[Code] ....
So my question mostly revolves around how I tie an Operator character to its required values, so I can use it in my code to test two operators precedence values.
My original thought was turn string into character array, but then I would need nested for/while loops to check if it is a number or letter, or if it is an operator and thus result in O(n^2) time
I am given the task to create a program that evaluates infix expressions using two generic stacks, one operator stack and one value stack.
This is my GenStack.java file:
import java.util.*; public class GenStack<T>{//T is the type parameter private Node top;//top of stack public class Node {//defines each node of stack T value; Node next;
[Code] ....
I'm having trouble with the eval and apply methods. The eval method doesn't appear to pickup ')' characters, like it doesn't even see them.
So I am working on a PostFix calculator that is used in command line for a class project, and I am having a little trouble on developing a memory for it. I have been told to create a hashMap and I have researched it and understand the basics of it. I have the calculating method working, but what I am having trouble trying to implement a way for the user to declare variables. For example this what the user should be able to do:
> a = 3 5 + 1 - 7 > bee = a 3 * 21 > a bee + 28
[code]....
I just need to be able to save the answers as a variable the User names, such as the example and let the user be able to use the variables later.
after i am done calculating everything from numbers stack, i pop the last number and return it... my question is how can i catch an exception if the size of my numbers stack is greater than 1;
public static String evaluate(String input) { char[] a = input.toCharArray(); if (input.isEmpty()) return "No input"; else if (input.equals(" ")) return "No input"; else if (input.equals(" "))
My question is to evaluate a Postfix notation entered from keyboard. I have no errors in my code but it prints only :
Exception in thread "main"
java.util.NoSuchElementException at ArrayStack.pop(PostFixEvaluation.java:72) at PostFixEvaluation.evaluatePostfix(PostFixEvaluatio n.java:107) at PostFixEvaluation.main(PostFixEvaluation.java:140)
I tried many values but it prints the same exception all the time.
My verify method also always returns false. So I'm given three classes to begin with. Calculator, Expression, and InfixExpression and they are listed below.
The goal is to create a class called PostfixExpression that extends Expression and can read and calculate postfix expressions.
My evaluate() method works for most calculations but when it needs to return a negative value it just returns the positive equivalent.
Also, my verify method always returns false and I can't pinpoint why.
Here's my current code. Some things are commented out for debugging purposes.
import java.util.Scanner; /** * Simple calculator that reads infix expressions and evaluates them. */ public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in);
import java.util.Scanner; import java.util.Stack; public class stack { public static Integer evaluate(String expression) { Scanner scanner = new Scanner(expression); Stack <Integer> operands = new Stack<Integer>();
[Code] ....
When I input my expression which has spaces between characters e.g.:10 2 8 * + 3 -, it worked; when I put expression which may not add space between each char e.g.: 3 14+2*7/, the error showed:
Enter a postfix expression: 3 14+2*7/ Exception in thread "main" java.util.EmptyStackException at java.util.Stack.peek(Stack.java:102) at java.util.Stack.pop(Stack.java:84) at hw9.stack.evaluate(stack.java:22) at hw9.stack.main(stack.java:45)
I'm having some trouble with a code I am writing for class. I had an 2 errors like this before this one and fixed it by changing int avgRe, avgMiles =0; to double. Now I am getting this error and am stuck on what I need to change. Here is the code:
import java.io.*; import java.util.*; import java.text.*; public class Reimbursement_3_09 { static Toolkit tools = new Toolkit(); public static void main (String [] args) throws Exception {
[Code] ....
This is my error:
[code=Java] Reimbursement_3_09.java:33: error: incompatible types: possible lossy conversion from double to int summary (outFile, totalAmount, ctrMiles, ctrMilesgt0, avgRe, avgMiles); ^ Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error
When I try to convert this value, "Testingu2120" (along with UTF coed u2120)comes as a string as part of SOAP response. I need to convert this UTF-8 characters in to a symbol, in this case it is SM (Service Mark) symbol and show it on the UI.
How can we achieve this in JAVA?
I have four different UTF-8 character set to convert.
I need to convert the LDIF data file to CSV format using Java. IS there any supporting JAR's, which we can use for the LDIF data file reading and parsing. Which is the best jar to use.
final ServerSocket serverSocketConn = new ServerSocket(9000); while (true) { try { Socket socketConn1 = serverSocketConn.accept(); new Thread(new ConnectionHandler(socketConn1)).start();
[Code] .....
I managed to convert this final DatagramSocket serverSocketConn = new DatagramSocket (9000);
Now I am stuck here
Socket socketConn1 = serverSocketConn.accept(); new Thread(new ConnectionHandler(socketConn1)).start();
Can I use this or I need to create a manual thread pooling for UDP ?
I am trying to convert the double 4270571936.0000000000d to a hex string using Double.toHexString() and the answer I'm getting is 0x1.fd17834p31, what does p stands for?
The answer I'm expecting to get is 0x41efd17834000000 so not sure why it won't give me the correct answer?
The following floating point Double to hex calculator shows the write answer right Floating Point to Hex Converter
but when I put this second line, the conversion, the program stops to work. I tried also with Integer.valueOf(timeInterval) but again I had the same problem.
I have a char[] containing ASCII characters that need to be converted into int value and double value.
The int value are always stored in 1 char size like 'j'. I extracted it succesffully by converting the char in a ascii bytearray and then used: Integer.parseInt(sb.toString().replace("0x", ""), 16);
How can I get the Value as double when i used the char[] with size 2 or 4 ?
Example : final char[] charValue = { 'u', ' ', '}','+' }; what is the associate Double value ?
Example : final char[] charValue = { 'T', ' ' }; what is the associate Double value ?
Example : final char[] charValue = { 'T', ' ' }; what is the associate int value ?
I'm working on a Weight Conversion program. The code I have for the program is:
import java.util.*; import java.awt.*; import javax.swing.*; public class Frame4a implements ActionListener { public static void main(String[] args) { JFrame f = new JFrame("Weight converter"); JPanel P = new JPanel(); // Make a JPanel;
[Code] .....
The errors I'm getting are:
Frame4a.java:33: error: class, interface, or enum expected public void actionPerformed(ActionEvent e){ ^ Frame4a.java:37: error: class, interface, or enum expected double kp= Double.parseDouble(strkilo);
why i cant get the return value to my conversion class.
import java.util.*; /* * * public class MetricTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); // declare a sentinal to exit the loop String check = "Quit";