Evaluate Postfix Notation Entered From Keyboard?

Oct 24, 2014

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.

Here is my code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
interface Stack<E> {
 
[Code] ....

View Replies


ADVERTISEMENT

Changing Infix To Postfix Notation Using Stacks - How To Utilize Hash Maps

Apr 7, 2014

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

View Replies View Related

Saving Date Entered By Keyboard?

May 21, 2014

How to save a date entered by the keyboard?

View Replies View Related

Read Integer From Keyboard Until Negative Number Is Entered

Apr 8, 2014

Basically i have a question ask me to write a program that read integer from keyboard until a negative number is entered, and then print out the max and min out of the number i have entered.

This is what i have done already which works, but i need to ignore the negative number when print out max and min...which i dont know how to do so. when i compile this program now it will always give me the negative number i enter at the end as minimum. which i need to ignore

Also if negative number is entered at once the program should print a error message say max and min are undefined.

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
int large = Integer.MIN_VALUE;
int small = Integer.MAX_VALUE;
while(true) {
System.out.print("Enter a integer: ");
int n = in.nextInt();

[Code] ....

View Replies View Related

Evaluate Nth Degree Polynomial For A Given Value Of X

Apr 8, 2014

To evaluate a nth degree polynomial for a given value of x using JAVA PRG.

E.g. 5x^7 + -2x^5 + 6x^3 + 1x^0 at x=2

The program must read in the number of non-zero terms in the polynomial followed by the co-efficient and power of each term. The value of x is read from the used and the program display the results of the evaluation of the polynomial. Use array list to represent the polynomial.

Test Case 1:
Input: No of terms: 4
Term n-1 to 0 (both coeffient and power )
5 7
-2 5
6 3
1 0
x = 2 (say)

Output :
625

View Replies View Related

How To Get Correct Postorder In Binary Tree And Evaluate It

Apr 23, 2015

The assignment is meant to understand and practice tree concepts, tree traversals and recursion. I am requested to use the tree to print the expressions by three ways and evaluate the expression in post-order. This is my node class,

package hw10;
public class Node {
String value;
Node left, right;
public Node(String value) {

[Code] .....

When i test my code, some trouble like this:

Enter a prefix expression: - + 10 * 2 8 3
prefix expression:
- + 10 * 2 8 3
postfix expression:
+ 10 * 2 8 3 -
infix expression:
( ( 10 + ( 2 * 8 ) ) - 3 )

[Code] .....

the error lines:

75: double left = evaluate(node.left);
107: evaluate(root);

In the Instruction, which said:

- int evaluate(Node node)

Given the root node of an expression tree, evaluates the expression using post-order traversal and returns the answer. main method Testing logic.

And I found the postfix expression was wrong, it should be: 10 2 8 * + 3 -

I think maybe the tree's order had some problems result in these cases or...

View Replies View Related

What Is Big O Notation In Java

Mar 6, 2015

What is Big O notation in java, what all things we need to remember before choosing a collection framework's class.

What each terms in big O notation means, Is there any blog or site that explain big O for java.

View Replies View Related

Determining Big O Notation

Apr 12, 2014

Compute the total number of operations for each of the following algorithm and define the value of Big Oh in its worst-case.

i.int jum = 0;
for (int x = 1; x<= n; x++) {
for (int y = 1 ; y <= 10; y++) {
for (int k = 0; k<= n*n; k++) {
jum = jum * x + y;

[Code] .....

What it actually means by compute the total number of operation ? My answer is

(i) 2 assignment , 1 addition and 1 multiplication
(ii) 4 assignment , 1 multiplication , 2 addition

The value of Big O is

(i) 10n^3 , so the answer is n^3
(ii) n^4+1 , so the answer is n^4

May i know is my answer is correct ??

View Replies View Related

Computational Complexity (Big O Notation)

Nov 5, 2014

I've been trying to learn more about Big O Notation and I've gotten stuck on a few pieces of code. What is the computational complexity for the following pieces of code?

1:

for(int i = n; i > 0; i /= 2) {
for(int j = 1; j < n; j *= 2) {
for(int k = 0; k < n; k += 2) {
// constant number of operations

[Code] .....

5 : Determine the average processing time of the recursive algorithm. (int n) spends one time unit to return a random integer value uniformly distributed in the range [0,n] whereas all other instructions spend a negligibly small time(e.g., T(0) = 0)

int myTest(int n) {
if(n <= 0) return 0;
else {
int i = random(n - 1);
return myTest(i) + myTest(n - 1 - i);

6 : Assume array a contains n values, the method randomValue takes a constant number c of computational steps to produce each output value, and that the method goodSort takes n log n computational steps to sort the array.

for(i = 0; i < n; i++) {
for(j = 0; j < n; j++)
a[j] = randomValue(i);
goodSort(a);
}

View Replies View Related

Scientific Notation In Java

Apr 14, 2014

I've to create a program to get user input in scientific notation. Now i'm confused that how can i check that how user have used the scientific notation. For example there are many ways to write a number in scientific notation. Also a user can either write a number as:

6.7E2 OR 6.7*10^2

How to handle this input and further convert it to double?

View Replies View Related

Notation Time Complexity

Aug 12, 2014

So recently I began Data Structures as core subject and the tutorials in this forum are great.

Right now, I seem to have trouble with Big Oh Notation algorithm and what is the mathematical side to it. "f(n) <= c.g(n), for n>=0.

The question I am working on is: Suppose we are maintaining a collection C of elements such that, each time we add a new element to the collection, we copy the contents of C into a new array list of just the right size. What is the running time of adding n elements to an initially empty collection C in this case?

View Replies View Related

Big-O Notation Of A Nested For And While Loop

May 20, 2014

I'm having trouble figuring out the Big-O notation for this loop. For now I only have it in pseudocode. I hope you understand it anyway

for i = 1 to n do
j = 1
while j <= n/2 do
print "*"
j = j + 1
end while
end for

View Replies View Related

Access Member-variable Without Dot Notation?

May 23, 2015

Java Code:

public class MountainBike extends Bicycle {

// the MountainBike subclass has
// one field
public int seatHeight;

// the MountainBike subclass has
// one constructor
public MountainBike(int startHeight, int startCadence,

[Code] ....

At first,
Java Code: public int seatHeight; mh_sh_highlight_all('java');
tells us that seatHeight is NOT a static field (because of the absence of static keyword).

Whereas in the constructor, the absence of dot notation (like something like this.seatHeight) in
Java Code: seatHeight = newValue; mh_sh_highlight_all('java');
shows that it IS a non-member/static variable.

How does this make sense?

View Replies View Related

Infix And Postfix Conversion

Feb 25, 2014

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.

View Replies View Related

PostFix Calculator Project?

Apr 2, 2015

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.

View Replies View Related

Postfix Evaluation Using Stack?

Apr 24, 2014

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(" "))

[code]....

View Replies View Related

How Does Prefix And Postfix Change The Answer

Mar 26, 2015

Will the postfix x++ and the prefix --y change the answer for this question?

If x has the value 10 and so does y, then what is the value of (x ++) * (-- y)?

Answer: 11*9

View Replies View Related

Getting Errors When Converting Infix To Postfix

Nov 24, 2014

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

View Replies View Related

Enterprise JavaBeans :: JPA With 3 Tables - Evaluate Query By Joining 2 Tables And Update Results In 3rd Table

Nov 8, 2013

I have 2 tables on 1 database

Table A, Table B

I need to evaluate a query by joining these 2 tables and then update the results in the 3rd table in another database. I need to use stateless session bean using JPA I believe. I need 3 Entities for each of the tables here.

View Replies View Related

Infix To Postfix And Prefix - Not Ordering Correctly

Jul 28, 2014

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;

[Code] .....

View Replies View Related

Postfix Calculator Doesn't Compute Negative Numbers

Nov 16, 2014

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);

[Code] .....

View Replies View Related

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)

View Replies View Related

Going Through All The Keys On Keyboard One At A Time

Jul 14, 2014

I am writing a code that tries to figure out the users password by going through every possible key (brute force). Although I think its going to work, it looks EXTREMELY inefficient to me, since its just a huge switch statement for each character -- 94 total, including the shift values. Is there a built in method in the JAVA API that goes through every key or something?

Here is my code for that segment:

public char[] HackingPassword(){
char[] passChars = this.password.toCharArray();//convert the password to char[]
char[] hacking = new char[passChars.length];//make the hacking variable same size as users password
int nextCharValue = 0;//this is used to cycle through the keyboard
//check each letter of hacking to match with password
for(int i = 0; i < passChars.length; i++){

[Code] .....

View Replies View Related

How To Get User Keyboard Input

Sep 4, 2014

I created a scanner object but it's not asking for any input. when I create a new scanner and then tell it to print it it just prints a bunch of weirdness when AFIK it should be asking me to type something and then it should repeat what i entered.

View Replies View Related

Reading Values From The Keyboard?

Aug 21, 2014

why we are not able to read second string in this program.

String s1,s2,s3;
String decider;
Scanner sc = new Scanner(System.in);

[Code].....

Note: If I uncomment line num 9 it works fine

View Replies View Related

Reading The Name Of The File From The Keyboard

Feb 20, 2014

Write a program that extracts words from a file. For the purposes of this program, a word is defined as a series of adjacent letters. Only print words that are at least four and no more than 12 letters long. Print each word on a different line.

The program should read the name of the file from the keyboard.

************************************************** ************************************************** **************

I need to get the filename from the user for this particular program. Usually I would have the name of the file prewritten into the source code like this....

Scanner input = new Scanner(new File("gettsy_burg.txt");

I tried different ways but I just can't seem to figure it out.... I guess what I'm really asking is how to rearrange Scanner input = new Scanner(new File("gettsy_burg.txt"); since I want the user to input the filename instead of the filename being prewritten by the programmer(me).

This is what I have so far to let the user for input....

Scanner keys = new Scanner(System.in);
System.out.println("Enter the filename: ");
String fileName = keys.nextLine();
keys = new Scanner(new File(fileName));

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved