Infix To Prefix - Pow Only Takes Int Values Not Doubles

Feb 7, 2014

I am having a problem with infix and prefix ... Here is the code

import java.util.StringTokenizer;
import static java.lang.Math.pow;
import java.util.Stack;
 public class Calculator {
static int precedence(char op) {
switch(op) {

[Code] ....

it says in line 72
possible loss of precision
required: int
found: double
the pow only takes int values?not doubles?how do i fix it?

View Replies


ADVERTISEMENT

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

Method Call That Takes Large Of Two Absolute Values

Oct 6, 2014

Write a method called largerAbsVal that takes two integers as parameters and returns the larger of the two absolute values. A call of largerAbsVal(11, 2) would return 11, and a call of largerAbsVal(4, -5) would return 5.

I have tried this code using methods in the Math Class but I am getting an error in Practice-it that says

Line 4
Your method's return type is void, which means that it does not return a value. But your code is trying to return a value. This is not allowed.

cannot return a value from method whose result type is void
return Math.max(Math.abs(Num1), Math.abs(Num2));

Here is my code. What I am doing wrong?

public static void largerAbsVal (int Num1, int Num2)

return Math.max(Math.abs(Num1), Math.abs(Num2));
}

View Replies View Related

Lexicographic Ordering - When Input String Values / No Output Takes Place

Jan 28, 2015

I have a assignment to do some Lexigraphic ordering. I have figured how to get the majority of this done, however, when I input my string values. No output takes place? :s

import java.util.Scanner;
public class Lab03c {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner lexi = new Scanner (System.in);
String s1,s2;

[Code] ....

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

Why For Loop Prefix Not Taken Into Consideration

Dec 5, 2014

I was looking at this for loop

for ( int i = 0; i < 10; i++){/*print i*/}

It prints 0..9 - fine.

I then looked at this loop

for ( int i = 0; i < 10; ++i){/*print i*/}

It prints 0..9 - why? From what I understand ++i is a prefix, therefore by the time the for loop condition is checked, shoudln't i start with 1? So 1..9?

If I did this test in a print statement so:

int i=0;
System.out.print(++i) //this prints 1,

so why doesn't this apply in the for loop?

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

Number Sorting Having Prefix Letter

Dec 24, 2014

I had a list of some numbers having a prefix A like A12 , A55, A76 ,A111 ,A888, A88 ,A880 A111 , A11,A1

I need to sort this list so the result would be A1,A11,A12,A55,A76....

How to do this. Can I use arrays.sort method to achieve the same or any other way.

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

Swing/AWT/SWT :: Possible To Use Items In ComboBox To Prefix File Name?

Oct 8, 2014

If I have a comboBox full of id's - is it possible that when I choose said id (click it) it will then transfer over into my textField where I can use that as a prefix for my filename ...

(The file can be created just by having a name in the text field it doesn't need to already exists) ...

View Replies View Related

Evaluating Infix Expressions Using Generic Stacks

May 12, 2014

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.

View Replies View Related

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

Add Doubles In Array

Jan 20, 2014

I'm allowing the user to choose certain items to buy that is moved to an array.Now I'm trying to add those thing in the array use a different class. how I can call the array from my driver class to my checkout class that adds them together.

View Replies View Related

Scanner Isn't Accepting Doubles

Jan 25, 2014

I had to use scanner to receive input from the user for a formula and everything compiled and worked correctly except when I entered a double into the Scanner, it only accepts ints and gives me this error every time I enter a double:

java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at TestA1Q2.main(TestA1Q2.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

I know that you usually get that error when the Scanner expects something else that is different from what you type and I have looked over my code time and time again and cannot find anything. I created a test program to see if it was just that problem and not something else in the code and I still got the same error but when I tried compiling and running it here browxy.com everything worked and I got the right answer for the formula. Here's the test I used:

import java.util.Scanner;
public class TestA1Q2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a double");
double velocity = keyboard.nextDouble();
}
}

At this point I'm thinking it might be a problem with my install of DrJava and I've tried re-installing but nothing changed.

View Replies View Related

How To Add Doubles From A Text File

Apr 13, 2014

I have a text file that looks like this:

JacobAndrews8159RoseCtMill CityOR97322555212787964.00
RyanPerkins7546PrinceAveLas VegasNV87126555345198913.00
JoshuaGilbert9278MonroeDrCorvallisCA97330555283265695.00
MilesCrain4578ChesterDrCorvallisOR9733155523456781544.00
ButchCassidy5498SuttonPlGresham AZ9738054165657971798.56
PerryWinkle8185ShaverAveLas VegasNV871265553812346195.66

everything is separated by tabs.

My while loop is like this:

while(fileScan.hasNextLine()) {
String currentLine = fileScan.nextLine();
String[]dataSet = currentLine.split(" ");
String zip = dataSet[7];
String phone = dataSet[8];
String donation = dataSet[9];
int ZIP = Integer.parseInt(zip);
Double Donation = Double.parseDouble(donation);

As you can see I made the donation read as a double, my question is how do I get the donation total? I need to write the code to add up and get the total donations but cannot figure it out.

View Replies View Related

How To Work With Doubles In Arrays

Sep 13, 2014

import java.util.*;
public class OneDimenArray {
public static void main(String[] args) {
double[] decimals = new double[12];
double nums = 0.0;
double a = 1.0;
[Code] ....

These are the erroe codes I'm getting;

OneDimenArray.java:13: error: ']' expected
double[] decimals = double[Scanner.nextDouble()];
^
OneDimenArray.java:13: error: ';' expected
double[] decimals = double[Scanner.nextDouble()];
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

View Replies View Related

Floats / Doubles And Currency

Mar 8, 2014

Just done a quick test to try and figure out the difference between floats and doubles.I made a quick program which outputs the result of 3.3 * 2 as both a float and a double.

I assumed since a float is the larger and more precise of the two data types that there would be more numbers after the decimal point, however this was not the case, it was in fact the double which had more numbers after the decimal point. Result was as follows:

Float: 29.699999
Double: 29.7

BTW for the above code I simply had a few text fields and a button with the following code:

Java Code:

textBox3.setText("" + Float.parseFloat(textBox1.getText()) * Float.parseFloat(textBox2.getText()));
textBox6.setText("" + Double.parseDouble(textBox1.getText()) * Double.parseDouble(textBox2.getText())); mh_sh_highlight_all('java');

Also, as these are limited to a certain amount of numbers im thinking there must be a more precise way for currency, if so what would I use for that? I suppose what im trying to figure out is what data type to use in different scenarios? When to use integer, float, double and long.

View Replies View Related

Set And Assign Doubles - String For Gross Pay?

Dec 15, 2014

I'm working with doubles I'm trying to figure out how to set this up so it works?

I'm just stuck on how to set and Assign a double FedTaxWitholding that gets the Fed Tax Withholding = Gross Pay * Fed
Tax Withholding Rate.

Do I need a string for Gross pay?

View Replies View Related

Java Program Using If / Else - Multiple Doubles

Oct 14, 2014

I am new to Java and I am having trouble figuring out why input.nextDouble(); is not working for the multiple doubles.

import java.util.Scanner;
public class SimpleMenu
{
public static void main(String [] args)
{
int userInput;
int input = 0;
double circleArea = 0;
double triangleArea = 0;
double rectangleArea = 0;

[Code] ....

View Replies View Related

Method That Returns Largest Value Of Array Of Doubles?

Jan 26, 2015

Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.

Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.

package kickstarter9;
public class Kickstarter9 {
public static void main(String[] args){
double myList;
double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3};
// find the largest value in the list

[Code]...

View Replies View Related

Return Smallest Of 10 Doubles (User Input)

Oct 1, 2014

This code will let a user type 10 double's, and return the smallest of those 10 double's. But I don't really understand what happens in de last for-loop.

package hsleiden.webcat.exercise06_09;
import java.util.Scanner;
public class Exercise06_09
{
public static void main (String[] args){
double[] allNumbers = new double [10];
 
[Code] ....

View Replies View Related

Swing/AWT/SWT :: Using Doubles For Positioning Objects And Testing Equality

Aug 15, 2014

I've been noticing some of my programs have been a little buggy recently, and think it's down to confusion over doubles and positioning. Lets say I have a label called banner that I want to scroll across the screen. Now I need to know the label's width in order to position it, but the width depends on the amount of text, so i use this code:

double bannerWidth = banner.getWidth();

Which forces me into using a double if I want to be accurate.

But the problem is that I'm trying to use a condition that compares the label's horizontal position (currentX) to the left edge of the screen (LEFT_EDGE), minus the width of the label (bannerWidth). In other words when the label is off the screen, it should go back to its starting position.

I assume that means that any variables I use to track the label's position (in this case currentX), or constants that I use to check equality (LEFT_EDGE), have to be doubles as well?

My difficulty is that I iterate currentX. But currentX--; won't work because doubles don't iterate as I'd expect.

So casting becomes an option..... but if I cast to an integer I effectively lose width on the label. And that is magnified each loop, resulting in the label's starting position moving further and further to the left.

View Replies View Related

Arithmetic Expression That Will Display Doubles To Only One Decimal Place?

Sep 29, 2014

I am looking to find a way to display the number of decimal places without using any type of formatting string.

Only to make it xxx.x instead of xxx.xxxxxx.

View Replies View Related

Formatting Doubles To Two Decimal Places Within Return Statement

Jan 25, 2015

I'm attempting to format my doubles to two decimal places within my return statement. I have tried to use DecimalFormat but it gives me an error because my method needs to return a double and that results in a string being returned. I have also tried using the *100.00/100.00 method and that doesn't work when the number already ends in 0.

If I pass -150.00 it gives me -150.0 when I need two decimal places.

How can I go about doing this?

View Replies View Related

Eclipse Takes Lot Of System Memory?

Jan 11, 2014

when running Eclipse, roughly 250k memory is used by it?In my Task Manager, it says: javaw.exe00265,000 KJava(TM) Platform SE binary.The value of 265,000 K is of course fluctuating, but around this value. Btw, this is when Eclipse is just running in the background, without even any java programs running in it. Is this normal memory usage by Eclipse?

View Replies View Related

Program To Store Total Rainfall For Each Of 12 Months Into Array Of Doubles

Oct 26, 2014

"Create a project called RainFall and a class nameD RainFall. Write a program that stores the total rainfall for each of 12 months into an array of doubles. The program should display total rainfall for the year, the average monthly rainfall, the month with the most rain and the month with the least rain. When outputting the month with the most and least rain, output the name of the month. This is what I have so far.

Scanner keyboard = new Scanner(System.in);
double averageMonthly;
double mostRain;
double leastRain;
int months;
double monthlyRain = 0;
double totalRain = 0;

[Code] ....

View Replies View Related







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