Refreshing a counter++ for every new file - java

If I have four different files that I'm passing through a loop with count++ how would I get the count++ to refresh and start at 1 for every new file?
Right now, the output for the files is:
20
37
57
76
But I want it to refresh and start at 1, so the output would be:
20
17
20
16
My code in entirety:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Grader {
public static int Score = 0;
public static void getFileInfo(String fileName)
throws FileNotFoundException {
Scanner in = new Scanner(new File(fileName));
while (in.hasNext()) {
String fileContent = in.nextLine();
String result = removeSpaces(fileContent);
double first = Double.parseDouble(fileContent.substring(0, fileContent.indexOf(" ")));
char operator = getOperator(fileContent);
double second = secondNumber(result, fileContent);
double last = Double.parseDouble(result.substring(result.indexOf("=") + 1));
double math = mathChecking(first, second, operator);
mathGrading(math , last);
}
System.out.println(Score);
}
public static String removeSpaces(String content){
String result = content.replace(" ","");
return result;
}
public static double mathGrading(double math , double last) {
if (math == last){
Score++;
}
return Score;
}
public static double secondNumber(String result, String opContent){
int checkAdd = opContent.indexOf('+');
int checkMinus = opContent.indexOf('-');
int checkMulti = opContent.indexOf('*');
int checkDivi = opContent.indexOf('/');
if (checkAdd != -1){
return Double.parseDouble(result.substring(result.indexOf('+')+1 , result.indexOf('=')));
}
else if (checkMinus != -1) {
return Double.parseDouble(result.substring(result.indexOf('-')+1 , result.indexOf('=')));
}
else if (checkMulti != -1) {
return Double.parseDouble(result.substring(result.indexOf('*')+1 , result.indexOf('=')));
}
else if (checkDivi != -1){
return Double.parseDouble(result.substring(result.indexOf('/')+1 , result.indexOf('=')));
}
return 0;
}
public static char getOperator(String fileContent){
int checkAdd = fileContent.indexOf('+');
int checkMinus = fileContent.indexOf('-');
int checkMulti = fileContent.indexOf('*');
int checkDivi = fileContent.indexOf('/');
if (checkAdd != -1){
char operator = fileContent.charAt(fileContent.indexOf('+'));
return operator;
}
else if (checkMinus != -1) {
char operator = fileContent.charAt(fileContent.indexOf('-'));
return operator;
}
else if (checkMulti != -1) {
char operator = fileContent.charAt(fileContent.indexOf('*'));
return operator;
}
else if (checkDivi != -1){
char operator = fileContent.charAt(fileContent.indexOf('/'));
return operator;
}
return ' ';
}
public static double mathChecking(double first, double second, char operator){
double math = 0;
if (operator == '+'){
return math = (first + second);
}
else if (operator == '-'){
return math = (first - second);
}
else if (operator == '*'){
return math = (first * second);
}
else if (operator == '/'){
return math = (first / second);
}
return math;
}
Here's the Starter class:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Starter {
public static void main(String[] args) throws FileNotFoundException {
Grader.getFileInfo("data\\studentSubmissionA.txt");
Grader.getFileInfo("data\\studentSubmissionA2.txt");
Grader.getFileInfo("data\\studentSubmissionB.txt");
Grader.getFileInfo("data\\studentSubmissionB2.txt");

Change the getFileInfo method to the following:
public static void getFileInfo(String fileName) throws FileNotFoundException {
Scanner in = new Scanner(new File(fileName));
Score = 0;
while (in.hasNext()) {
String fileContent = in.nextLine();
String result = removeSpaces(fileContent);
double first = Double.parseDouble(fileContent.substring(0, fileContent.indexOf(" ")));
char operator = getOperator(fileContent);
double second = secondNumber(result, fileContent);
double last = Double.parseDouble(result.substring(result.indexOf("=") + 1));
double math = mathChecking(first, second, operator);
mathGrading(math , last);
}
System.out.println(Score);
}

Related

recursive parsing calculator java

Im trying to create a recursive parsing calculator in java for addition, multiplication and factorial, but I'm struggling on the very first part of just reading through the user input to split the input into numbers and operators. While debugging I tried to see where it was going wrong, and I found that when the "+" was going through the if else statements it just skipped over it. I'm really unsure on what the issue is, I originally tried using tokens, and splitting up into substring, but it wasn't going well then either. Any help would be appreciated. Thank you
package com.company;
import java.util.Scanner;
class Main {
public static void main(String[] param) {
String input = input("Please enter an expression");
int n = input.length()-1;
String[] splitter = input.split("(?<=\\G.)");
split(input, n);
//int result = calculate(input);
//String[] splitter = input.split("(?<=\\G.)");
}
public static String split(String input, int n) {
String[] splitter = input.split("(?<=\\G.)");
System.out.println(splitter[n]);
String symbol = splitter[n];
if (symbol.equals("+")) {
evalADD(n, splitter);
}
if (symbol.equals("*")) {
evalMULT(n, splitter);
}
if (symbol.equals("!")) {
evalFACT(n, splitter);
}
else if (Integer.parseInt(splitter[n]) >= 0 && Integer.parseInt(splitter[n]) <=9)
{
if (n != 0) {
n = n - 1;
split(input, n);
}
}
if (n != 0)
n = n - 1;
split(input, n);
return input;
}
public static int evalADD(int n, String [] splitter){
int arg1;
int arg2;
int result;
arg1 = Integer.parseInt(splitter[n+1]);
arg2 = Integer.parseInt(splitter[n+2]);
result = arg1 + arg2;
return result;
}
public static int evalMULT(int n, String [] splitter){
int arg1;
int arg2;
int result;
arg1 = Integer.parseInt(splitter[n+1]);
arg2 = Integer.parseInt(splitter[n+2]);
result = arg1 * arg2;
return result;
}
public static int evalFACT(int n, String [] splitter){
int arg1;
int arg2;
int result;
arg1 = Integer.parseInt(splitter[n+1]);
arg2 = Integer.parseInt(splitter[n+2]);
result = arg1 - arg2;
return result;
}
public static String input(String message) {
Scanner scanner = new Scanner(System.in);
System.out.println(message);
return (scanner.nextLine());
}
}
I have noticed that you are using the java.util.Scanner. I wrote a script that should do the task for you by following all of your criteria:
import java.util.Scanner;
class recursiveParsingCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask user to input the expression
System.out.println("Please input the expression");
String userInput = scanner.nextLine();
System.out.println(
"And the final result is: " + recursiveCalculation(userInput, userInput.length() - 1, 0, 0, 0));
scanner.close();
System.exit(0);
}
// Identify the type of character at a specific position
public static char charOfString(String userInput, int i) {
return userInput.charAt(i);
}
/*
* Position must be userInput.length() - 1 initially. currentResults, operand1
* and operand2 are also meant to be initilized with 0.
*/
public static int recursiveCalculation(String userInput, int position, int operand1, int operand2,
int currentResults) {
// If position is zero, just output the operand.
if (position == 0) {
if (Character.isDigit(charOfString(userInput, position))) {
return charOfString(userInput, position) - '0';
} else {
System.out.println("Invalid input.");
}
}
if (position > -1) {
// Check if it is a number or an operator
if (Character.isDigit(charOfString(userInput, position))) {
operand1 = charOfString(userInput, position) - '0'; // First operand
// Check if 2nd char is a number or an operator.
if (Character.isDigit(charOfString(userInput, position - 1))) {
operand2 = charOfString(userInput, position - 1) - '0';
position = position - 1;
}
} else {
// If it is an operator, then proceed to compute the results so far
char operator = charOfString(userInput, position);
// If it is a binary situation
if (operator == '+' || operator == '*') {
currentResults = binaryOperator(operator, operand1, operand2);
operand2 = currentResults;
}
// If it is an unary situation
else if (operator == '!') {
if (currentResults == 0) {
currentResults = operand1;
}
currentResults = unaryOperator(currentResults);
operand2 = currentResults;
} else {
System.out.println("Invalid operator");
return 0; // Return zero by default
}
}
position = position - 1;
}
if (position > -1) {
return recursiveCalculation(userInput, position, operand1, operand2, currentResults);
} else {
return currentResults;
}
}
public static int binaryOperator(char operator, int operand1, int operand2) {
switch (operator) {
case '+':
return operand1 + operand2;
case '*':
return operand1 * operand2;
default:
System.out.println("Invalid binary Operator");
return 0; // Return zero by default
}
}
// Calculate the factorial
public static int unaryOperator(int operand) {
if (operand <= 1)
return 1;
else
return operand * unaryOperator(operand - 1);
}
}
Examples of usage: For a binary operator, input +21, and it will add them for you. With unary, input !3, and it will yield the factorial. Now, you can try any chain of combinations and permutations of numbers with unary and binary operators, and it will calculate the values for you recursively.
For example, consider the input !*3+12: It will add 1 and 2, then multiply it by 3 and finally, it calculates the factorial out of the whole expression, thereby resulting in 362880 as expected.
Why don't you assign the input calculation string to a character array and iterate through the array and match the characters '+', '-','*'?

IndexOutOfBound error with string manipulation

this is the code for a pig-latin translator in JAVA, it works with one word but never with a sentence. It seems that the code at line 30 is messing everything up, and I'm not sure how it is doing that or how I can fix it. IndexOutOfBoundError on line 8 and line 30. I'm not sure how to fix this, help.
public class Practice
{
public static void main(String[] args)
{
String a = "hello Stranger";
System.out.println(translate(a)); //8
}
private static String translate(String a)
{
String XD = a;
boolean repeat = true;
int first = 1;
int second = 0;
do
{
second = XD.indexOf(" ");
if (second == -1)
{
repeat = false;
XD = vowelFinder(a);
break;
}
else
{
XD = XD + vowelFinder(a.substring(first, second)); //30
}
first = second +1;
}while(repeat == true);
return XD;
}
private static boolean isVowel (char c)
{
if (c == 'a'|| c== 'e'|| c== 'i' || c == 'o' || c== 'u')
{
return true;
}
return false;
}
private static String vowelFinder(String s)
{
String nope = s;
for(int i = 0; i <= s.length(); i++)
{
if(isVowel(s.charAt(i)) == true)
{
nope = nope.substring(i) + "-"+nope.substring(0, i);`
return nope;
}
}
return nope;
}
}
Try this;
import java.util.Scanner;
public class PigLatin {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
String yourSentence="";
do {
String[] words;
System.out.print("Enter your words here: ");
yourSentence = input.nextLine();
words = yourSentence.split(" ");
for (String word : words) {
if (word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u"))
System.out.print(word + "way ");
else if (word.startsWith("sh") || word.startsWith("ch") || word.startsWith("th"))
System.out.print(word.substring(2)+word.substring(0,2)+"ay ");
else
System.out.print(word.substring(1)+word.substring(0,1)+"ay ");
}
System.out.println();
} while(!yourSentence.equals("quit"));
}
}

Autocomplete byReverseWeightOrder comparator issue

I have been working on this problem for several hours now and I just cannot figure out what I am doing wrong here. Could anyone help point me in the right direction?
I was asked to write an Autocomplete program and I've completed everything except for this one method I cannot get working. Each term has: 1. String query and 2. long weight.
Here is the method:
public static Comparator<Term> byReverseWeightOrder() {
return new Comparator<Term>() { // LINE CAUSING PROBLEM
public int compare(Term t1, Term t2) {
if (t1.weight > t2.weight) { // LINE CAUSING PROBLEM
return -1;
} else if (t1.weight == t2.weight) {
return 0;
} else {
return 1;
}
}
};
}
My problem is that no matter how I mess with the method I always result in a NullPointerException(). Which, it points to this method (byReverseWeightOrder) as well as these two statements.
Arrays.sort(matches, Term.byReverseWeightOrder());
Term[] results = autocomplete.allMatches(prefix);
Here is the rest of the code if it can be found helpful:
Term
import java.util.Comparator;
public class Term implements Comparable<Term> {
public String query;
public long weight;
public Term(String query, long weight) {
if (query == null) {
throw new java.lang.NullPointerException("Query cannot be null");
}
if (weight < 0) {
throw new java.lang.IllegalArgumentException("Weight cannot be negative");
}
this.query = query;
this.weight = weight;
}
public static Comparator<Term> byReverseWeightOrder() {
return new Comparator<Term>() {
public int compare(Term t1, Term t2) {
if (t1.weight > t2.weight) {
return -1;
} else if (t1.weight == t2.weight) {
return 0;
} else {
return 1;
}
}
};
}
public static Comparator<Term> byPrefixOrder(int r) {
if (r < 0) {
throw new java.lang.IllegalArgumentException("Cannot order with negative number of characters");
}
final int ref = r;
return
new Comparator<Term>() {
public int compare(Term t1, Term t2) {
String q1 = t1.query;
String q2 = t2.query;
int min;
if (q1.length() < q2.length()) {
min = q1.length();
}
else {
min = q2.length();
}
if (min >= ref) {
return q1.substring(0, ref).compareTo(q2.substring(0, ref));
}
else if (q1.substring(0, min).compareTo(q2.substring(0, min)) == 0) {
if (q1.length() == min) {
return -1;
}
else {
return 1;
}
}
else {
return q1.substring(0, min).compareTo(q2.substring(0, min));
}
}
};
}
public int compareTo(Term that) {
String q1 = this.query;
String q2 = that.query;
return q1.compareTo(q2);
}
public long getWeight() {
return this.weight;
}
public String toString() {
return this.weight + "\t" + this.query;
}
}
BinarySearchDeluxe
import java.lang.*;
import java.util.*;
import java.util.Comparator;
public class BinarySearchDeluxe {
public static <Key> int firstIndexOf(Key[] a, Key key, Comparator<Key> comparator) {
if (a == null || key == null || comparator == null) {
throw new java.lang.NullPointerException();
}
if (a.length == 0) {
return -1;
}
int left = 0;
int right = a.length - 1;
while (left + 1 < right) {
int middle = left + (right - left)/2;
if (comparator.compare(key, a[middle]) <= 0) {
right = middle;
} else {
left = middle;
}
}
if (comparator.compare(key, a[left]) == 0) {
return left;
}
if (comparator.compare(key, a[right]) == 0) {
return right;
}
return -1;
}
public static <Key> int lastIndexOf(Key[] a, Key key, Comparator<Key> comparator) {
if (a == null || key == null || comparator == null) {
throw new java.lang.NullPointerException();
}
if (a == null || a.length == 0) {
return -1;
}
int left = 0;
int right = a.length - 1;
while (left + 1 < right) {
int middle = left + (right - left)/2;
if (comparator.compare(key, a[middle]) < 0) {
right = middle;
} else {
left = middle;
}
}
if (comparator.compare(key, a[right]) == 0) {
return right;
}
if (comparator.compare(key, a[left]) == 0) {
return left;
}
return -1;
}
}
AutoComplete
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
public class Autocomplete {
public Term[] terms;
public Autocomplete(Term[] terms) {
if (terms == null) {
throw new java.lang.NullPointerException();
}
this.terms = terms.clone();
Arrays.sort(this.terms);
}
public Term[] allMatches(String prefix) {
if (prefix == null) {
throw new java.lang.NullPointerException();
}
Term theTerm = new Term(prefix, 0);
int start = BinarySearchDeluxe.firstIndexOf(terms, theTerm, Term.byPrefixOrder(prefix.length()));
int end = BinarySearchDeluxe.lastIndexOf(terms, theTerm, Term.byPrefixOrder(prefix.length()));
int count = start;
System.out.println("Start: " + start + " End: " + end);
if (start == -1 || end == -1) {
// System.out.println("PREFIX: " + prefix);
throw new java.lang.NullPointerException();
} // Needed?
Term[] matches = new Term[end - start + 1];
//matches = Arrays.copyOfRange(terms, start, end);
for (int i = 0; i < end - start; i++) {
matches[i] = this.terms[count];
count++;
}
Arrays.sort(matches, Term.byReverseWeightOrder());
System.out.println("Finished allmatches");
return matches;
}
public int numberOfMatches(String prefix) {
if (prefix == null) {
throw new java.lang.NullPointerException();
}
Term theTerm = new Term(prefix, 0);
int start = BinarySearchDeluxe.firstIndexOf(terms, theTerm, Term.byPrefixOrder(prefix.length()));
int end = BinarySearchDeluxe.lastIndexOf(terms, theTerm, Term.byPrefixOrder(prefix.length()));
System.out.println("Finished numberMatches");
return end - start + 1; // +1 needed?
}
public static void main(String[] args) throws IOException {
// Read the terms from the file
Scanner in = new Scanner(new File("wiktionary.txt"));
int N = in.nextInt(); // Number of terms in file
Term[] terms = new Term[N];
for (int i = 0; i < N; i++) {
long weight = in.nextLong(); // read the next weight
String query = in.nextLine(); // read the next query
terms[i] = new Term(query.replaceFirst("\t",""), weight); // construct the term
}
Scanner ip = new Scanner(System.in);
// TO DO: Data Validation Here
int k;
do {
System.out.println("Enter how many matching terms do you want to see:");
k = ip.nextInt();
} while (k < 1 || k > N);
Autocomplete autocomplete = new Autocomplete(terms);
// TO DO: Keep asking the user to enter the prefix and show results till user quits
boolean cont = true;
do {
// Read in queries from standard input and print out the top k matching terms
System.out.println("Enter the term you are searching for. Enter * to exit");
String prefix = ip.next();
if (prefix.equals("*")) {
cont = false;
break;
}
Term[] results = autocomplete.allMatches(prefix);
System.out.println(results.length);
for(int i = 0; i < Math.min(k,results.length); i++)
System.out.println(results[i].toString());
} while(cont);
System.out.println("Done!");
}
}
I apologize for the sloppy code, I have been pulling my hair out for awhile now and keep forgetting to clean it up.
Two examples:
Example 1:
int k = 2;
String prefix = "auto";
Enter how many matching terms do you want to see:
2
Enter the term you are searching for. Enter * to exit
auto
619695 automobile
424997 automatic
Example 2:
int k = 5;
String prefix = "the";
Enter how many matching terms do you want to see:
5
Enter the term you are searching for. Enter * to exit
the
5627187200 the
334039800 they
282026500 their
250991700 them
196120000 there

NullPointerException using Scanner

I am new to programming and I'm starting to create a simple calculator in Java, but I keep getting an error on the line
for (int i = 0; i < user_input.length(); i++)
The error says:
Exception in thread "main" java.lang.NullPointerException
How can I fix this problem?
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
public class stringCalculator
{
public static ArrayList<String> input = new ArrayList<String>();
public static ArrayList<String> calcOperators = new ArrayList<String>();
public static ArrayList<Integer> calcOperands = new ArrayList<Integer>();
public static String user_input;
public static String first;
public static int int1;
public static String char1;
public static int int2;
public static String next;
}
public static void input()
{
Scanner user_input = new Scanner(System.in);
int1 = user_input.nextInt();
char1 = user_input.next();
int2 = user_input.nextInt();
next = user_input.nextLine();
}
public void calcOperators()
{
for (int i = 0; i < user_input.length(); i++)
{
if (char1 == "+")
{
calcOperators.add(char1);
}
else if (char1 == "-")
{
calcOperators.add(char1);
}
else if (char1 == "char1")
{
calcOperators.add(char1);
}
else if (char1 == "/")
{
calcOperators.add(char1);
}
}
public void calcOperands()
{
for (int i = 0; i < user_input.length(); i++)
{
if (int1 == 1 || int2 == 1)
{
calcOperands.add(1);
}
else if (int1 == 2 || int2 == 2)
{
calcOperands.add(2);
}
else if (int1 == 3 || int2 == 3)
{
calcOperands.add(3);
}
else if (int1 == 4 || int2 == 4)
{
calcOperands.add(4);
}
else if (int1 == 5 || int2 == 5)
{
calcOperands.add(5);
}
else if (int1 == 6 || int2 == 6)
{
calcOperands.add(6);
}
else if (int1 == 7 || int2 == 7)
{
calcOperands.add(7);
}
else if (int1 == 8 || int2 == 8)
{
calcOperands.add(8);
}
else if (int1 == 9 || int2 == 9)
{
calcOperands.add(9);
}
else if (int1 == 0 || int2 == 0)
{
calcOperands.add(0);
}
}
}
}
public class Main
{
public static void main(String[] args)
{
stringCalculator c = new stringCalculator();
c.input();
c.calcOperators();
c.calcOperands();
}
}
I am kind of confused in here why you have
public static String user_input
and then
Scanner user_input = new Scanner(System.in);
int1 = user_input.nextInt();
char1 = user_input.next();
int2 = user_input.nextInt();
next = user_input.nextLine();
And finally you are using the same var name for strings and ints in loops:
for (int i = 0; i < user_input.length(); i++)
for (int i = 0; i < user_input.length(); i++)
I would highly recommend refactoring this code.
.....
private ArrayList<String> input = new ArrayList<String>();
private ArrayList<String> calcOperators = new ArrayList<String>();
private ArrayList<Integer> calcOperands = new ArrayList<Integer>();
private String user_input;
private String first;
private String next;
private String char1;
private int integer2;
private int integer1;
}
public static void input()
{
Scanner input = new Scanner(System.in);
int int1 = input.nextInt();
int int2 = input.nextInt();
string str = input.next();
string nxt = input.next();
setInteger1(int1);
setInteger2(int2);
setStringFirst(str);
setStringNext(...);
....
// And so on
}
private void setInteger1(int int1) {
this.integer1 = int1;
}
private Integer getInteger1() {
return this.integer1;
}
private void setInteger2(int int2) {
this.integer2 = int2;
}
private Integer getInteger2() {
return this.integer2;
}
private void setStringFirst(String fst) {
this.first = fst;
}
private String getStringFirst() {
return this.first;
}
// And so on. Create all get and set methods for each global variable and for future
// reference do not use variable names that are the same as method, names
// and try to use more meaningful variable names. In fact, if you look at
// Java naming conventions it would do you good.
Could you also tell us perhaps what these loops are meant to do? As I don't really understand what is this? Do you want to iterate over an array of "things" and match each operation to given array element? Or do you just want a single element to match one of the operations?
Side note: Class names should be capitalized:
public class StringCalculator
==================================================================================
OK, I have made a simple program that allows you to add a string into an array and then display it. It is based on what you were doing although you will see it is structured differently. This should give you a head start and allow you to implement this further and finish whatever you are doing.
public class Thing {
private String operator;
private void getUserInput() {
Scanner input = new Scanner(System.in);
if(input.hasNextInt()) {
System.out.println("I have entered an integer: " + input.nextInt());
}
else {
setOperator(input.nextLine());
addOperators();
System.out.println("I have entered a string: " + getOperator());
}
displayThing();
}
private ArrayList<String> addOperators() {
ArrayList<String> operatorsList = new ArrayList<String>();
if(getOperator().equals("+")) {
operatorsList.add(operator);
}
if(getOperator().equals("-")) {
operatorsList.add(operator);
}
else {
operatorsList.add(getOperator());
}
return operatorsList;
}
private void displayThing() {
System.out.println(addOperators());
}
public static void main(String[] args) {
Thing program = new Thing();
program.getUserInput();
}
// Setters and getters
private void setOperator(String operator) {
this.operator = operator;
}
private String getOperator() {
return this.operator;
}
}
You have declared the public static String user_input; which is used in a for loop and not initialised, so thus the exception. Initialise it in your input method, like this.user_input=user_input.nextLine().
You have a static String named user_input defined as:
public static String user_input; // This is null and what the for loop is reading
Set a value to that string when you're using your Scanner... which you shouldn't have also named user_input.

How to do precedence with multiple exponentials, ^, in an arithmetic equation

I am working on a program that solves arithmetic equations. I have encountered a problem that occurs when there are multiple exponential statements in a row the program does not correctly solve them. An example would be: 2^3^2, the correct answer is 512 but the program outputs 64. This is because the program does 2^3 and then 8^2, instead of doing 3^2 and then 2^9. Let me know if you have any ideas on how to modify my current code or have something to add.
import java.text.DecimalFormat;
import java.util.EmptyStackException;
import myUtil.*;
public class PostFixEvaluator extends Asg6
{
public static class SyntaxErrorException extends Exception
{
SyntaxErrorException(String message)
{
super(message);
}
}
private static final String operators = "+-*/^()";
private AStack<Double> operandStack;
private double evaluateOP(char op) throws Exception
{
double rightside = operandStack.pop();
double leftside = operandStack.pop();
double result = 0;
if(op == '+')
{
result = leftside + rightside;
}
else if(op == '-')
{
result = leftside - rightside;
}
else if(op == '*')
{
result = leftside * rightside;
}
else if(op == '/')
{
if(rightside == 0)
{
throw new Exception("Can not divide by 0, the equation is undefined");
}
else
{
result = leftside / rightside;
}
}
else if(op == '^')
{
result = Math.pow(leftside, rightside);
}
return result;
}
private boolean isOperator(char ch)
{
return operators.indexOf(ch) != -1;
}
public double evaluate(String exp) throws Exception
{
operandStack = new AStack<Double>();
String[] tokens = exp.split("\\s+");
try
{
for(String nextToken : tokens)
{
char firstChar = nextToken.charAt(0);
if(Character.isDigit(firstChar))
{
double value = Double.parseDouble(nextToken);
operandStack.push(value);
}
else if (isOperator(firstChar))
{
double result = evaluateOP(firstChar);
operandStack.push(result);
}
else
{
throw new Exception("Invalid character: " + firstChar);
}
}
double answer = operandStack.pop();
if(operandStack.empty())
{
return answer;
}
else
{
throw new Exception("Syntax Error: Stack should be empty");
}
}
catch(EmptyStackException ex)
{
throw new Exception("Syntax Error: The stack is empty");
}
}
}
You're trying to use an LL(1) grammar (which is what a recursive descent parser can parse) to model a right-associative operator (^). A right-associative operator requires left recursion, which doesn't work so easily with LL(1) grammars. You'll want to look at left factoring: http://en.wikipedia.org/wiki/LL_parser#Left_Factoring
I would solve this with operator Priorities as you might want to have them anyway.
For testing i changed the class a bit, so i could test it and its sure not most efficient or readable but you should get
the idea how it works.
import java.text.DecimalFormat;
import java.util.EmptyStackException;
import java.util.*;
public class PostFixEvaluator
{
public static class SyntaxErrorException extends Exception
{
SyntaxErrorException(String message)
{
super(message);
}
}
private static final String operators = "+-*/^()";
private static int[] operatorPriority = {1,1,2,2,3,10,10};
private Stack<Double> operandStack;
private Stack<Character> operatorStack;
private double evaluateOP(char op) throws Exception
{
double rightside = operandStack.pop();
double leftside = operandStack.pop();
double result = 0;
if(op == '+')
{
result = leftside + rightside;
}
else if(op == '-')
{
result = leftside - rightside;
}
else if(op == '*')
{
result = leftside * rightside;
}
else if(op == '/')
{
if(rightside == 0)
{
throw new Exception("Can not divide by 0, the equation is undefined");
}
else
{
result = leftside / rightside;
}
}
else if(op == '^')
{
result = Math.pow(leftside, rightside);
}
return result;
}
private boolean isOperator(char ch)
{
return operators.indexOf(ch) != -1;
}
public double evaluate(String exp) throws Exception
{
operandStack = new Stack<Double>();
operatorStack = new Stack<Character>();
String[] tokens = exp.split("\\s+");
try
{
for(String nextToken : tokens)
{
char firstChar = nextToken.charAt(0);
if(Character.isDigit(firstChar))
{
double value = Double.parseDouble(nextToken);
operandStack.push(value);
}
else if (isOperator(firstChar))
{
// Try to evaluate the operators on the stack
while (!operatorStack.isEmpty())
{
char tmpOperator = operatorStack.pop();
// If Operator has higher Priority than the one before,
// Calculate it first if equal first calculate the second
// operator to get the ^ problem fixed
if (operatorPriority[operators.indexOf(firstChar)] >= operatorPriority[operators.indexOf(tmpOperator)])
{
operatorStack.push(tmpOperator);
// Operand has to be fetched first
break;
}
else
{
double result = evaluateOP(tmpOperator);
operandStack.push(result);
}
}
operatorStack.push(firstChar);
}
else
{
throw new Exception("Invalid character: " + firstChar);
}
}
// Here we need to calculate the operators left on the stack
while (!operatorStack.isEmpty())
{
char tmpOperator = operatorStack.pop();
// Operator Priority has to be descending,
// or the code before is wrong.
double result = evaluateOP(tmpOperator);
operandStack.push(result);
}
double answer = operandStack.pop();
if(operandStack.empty())
{
return answer;
}
else
{
throw new Exception("Syntax Error: Stack should be empty");
}
}
catch(EmptyStackException ex)
{
throw new Exception("Syntax Error: The stack is empty");
}
}
// For testing Only
public static void main(String[] args) throws Exception
{
PostFixEvaluator e = new PostFixEvaluator();
System.out.println(e.evaluate("2 ^ 3 ^ 2"));
}
}

Categories

Resources