Scan Postfix Expression From Left To Right Using Operand Stack
Apr 13, 2015
package hw9;
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)
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(" "))
I've been working on this problem for a while now and continue to get an error when I try to subtract one user inputted integer from another. It seems to compile fine for adding, dividing, and multiplying. Why it might be making that error and how to resolve it? (As an aside, I have no idea if I did the whole program right but am just trying to figure out why a declared int would come back with an error it's a string.)
import java.util.Scanner; public class Calculate2 { public static void main(String[] args) { int firstInt; int secondInt; int choice;
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.
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.
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
what weapon the user wants to use and set the element of the Player but I can't use defineWeapon() inside of the constructor so what can I do?here's the player class (where I need to set the element which is the second string in the constructor
package netHackDessart; import java.util.Scanner; public class Player extends Monster { public Player() { super("player", "null", 18, 6, 150, 4, 1, 6, 1, 8, 1, true, false);
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.
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;
I was trying to do a short program that scans a text for a given word and then tells you how many times that word was repeated in the text. The result was this:
var text = prompt("Write the text to be searched through"); var word = prompt("Write the word to be looked for. Beware of capitals!"); var hits = []; for (var i = 0; i < text.length; i++) { if (text[i] === word[0]) {
[Code] ....
However, this doesn't scan the text properly. I think the problem lies in one of the lines from 4 to 8, but, even after thinking quite a lot, I couldn't understand what was it. I thought that by saying that the letter of the text in position [i + k] shuold be equal to the letter of the word in position [k] I could make it work, but it doesn't.
I want to find a certain element in array I have to scan through the array index starting from 0 until I find the number I am looking for. Even in data structures which use hashing like HashMap and Hashtable we will have to scan through the keys until we find the key we are looking for. So what is the use of hashing over index based searching? I mean how is hashing an advantage over an array?
I have task for last years in campus , how do you scan document with scanner peripheral in java program? I have browsing on internet, and I have an API like TWAIN.
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);
I'm scanning my text and I want to add them into array list, but it is not easy. I want each word store from arrayList of a to z which is arrayList size is 26. Example:
arrayList a store string array of: an, and, apple, ...
arrayList b store string array of: be, become, became..... . arrayList z store string array of: zero, zone...
In my class, I create:
private static ArrayList<String[]> words = new ArrayList<String[]>(26); // to store all words
In main, I do while loop to get each words and store in words array,
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.LinkedList; import java.util.Scanner; public class BinaryTree {
I need to create a program that uses ArrayList to store integers the user inputs, then scan the array for the largest number. I would also like the user to be able to exit this loop if the number 0 is entered.
As you can see below, I'm not sure how to correctly exit the do-while loop. I found this on another forum, but it does not work.
Java Code:
import java.util.*; public class array { public static void main(String [] args){ ArrayList<Integer> list = new ArrayList<Integer>();
I am thinking of a project for my university the teachers liked it but I am not sure if its even possible.I am trying to make an andriod app. What I want to do is take a picture of a hand drawn logic circuit (having the AND, OR, NOT ... gates) recognize the gates, and make a circuit in the moblie and run it on all possible inputs.
For this I will have to make a simulator on mobile, that I dont think is the hard part. The problem is how could recognize the gates from a picture. Example of logical circuit is attached ( assume its hand drawn )..I found out that theres a edge detection plugin in java but still I dont think its enought to recognize the gates. Please share any algorithm or any technique or tools that I can use to make this thing.
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
My assignment is to create a hangman game. One of my first difficulties with it is how to scan the string for an input supplied by the user.
Here is my method:
//Guesses that character c is in the secret word. //Updates all instance variables accordingly, //and returns true if c is present in the secret word. public boolean guessCharacter(char c){ }
I'm trying to replicate the rol(rotate left) instruction in assembly though can only get as far as shifting the bits with '<<' or doing Long.rotateLeft(var, 5). Both of these method don't wrap around the bits as the rol instruction does.
I can't find any resource on the net about a simple Java code just to move a gif image left or right. I've already accomplished the up, down, and center and they're working fine, thus, I'm still struggling with moving the image left or right. Here's the code.
public class MoveIt extends Applet implements ActionListener { private Image cup; private Panel keypad; public int top = 10; public int left = 10;
[Code] ....
I remember in Visual BASIC it's easily achieved by NameOfImage.left = NameOfImage.left - 10 to move left and NameOfImage.left = NameOfImage.left + 10.
I am having issues with drawing shapes from bottom right to top left.
Issue:
- g.drawRect() will show like I am calling g.fillRect() - other shapes will not even show the shape in that area
Needs:
- g.drawSHAPE needs to show and not be filled unless I have my fill checkbox selected
The Program:
- Create a JFrame with a draw panel and a component panel - have a combobox with shapes that, when selected, will draw that shape in the draw panel - have a button that, when clicked, will launch JColorChooser to change the color of the drawn shape (draw panel is set to black) - have a checkbox that, when checked, fills the shape - have mouse listeners to adjust X and Y and will instantly update the shapes size to where you drag/click/press/release
Code for my drawRect():
Java Code:
// if statement to check if mouse drag X is less than starting X if(x2 <= x){ if(emptyORfill.isSelected()) // emptyORfill is my JCheckBox g.fillRect(x2, y, x-x2, y2-y); // x-x2 is the same as Math.abs(x2-x) else g.drawRect(x2, y, x-x2, y2-y);
[Code] .....
This is just for my Rectangle. This will show a filled rectangle when both mouse drag X and Y are less then the starting X and Y. If I take this fully functional code and adapt it to drawRoundRect(), the round rectangle wont even show the shape when mouse drag X and Y are less than the starting X and Y but will be fine if one or the other is less than the starting X or Y. NOTE: This same exact code worked on my classmates laptop in her program, but in my program on her laptop it did not. She took out the "else" in the else if's and just made them if statements all the way down and it worked on her laptop in my program, but the same "fix" did not work on my pc.
My mouse listener just sets X and Y values in my Shape class that updates my shape methods. I have an item listener for my comboBox that sets default values when a new selection is made and enables/disables editable on my fill checkbox for certain shapes. My action listener looks for the button click and the checkBox click.