3 Replies - 271 Views - Last Post: 04 December 2019 - 08:17 AM Rate Topic: -----

#1 kkee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-December 19

Java basic calculator

Posted 03 December 2019 - 02:53 PM

I've been in the process of trying to create a calculator that can do the following.
--read mathematical equation from user input
--parse input when user hits enter (newline)
--if user types quit it ends the program
--will need to be able to handle parenthesis
--must follow order of operations
--should handle addition, subtraction, multiplication, division, square and squareroot (these methods have been previously created and must be implemented in the program.)

The major issue I am having with the program is how to get it to properly read and evaluate the expression while following all guidelines. I'm not really sure where to go from here as I am new to programming in Java.


import java.util.*;

public class Calculator{
	
	public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        String userInput;
		ArrayList<Double> opperands = new ArrayList<Double>();
		ArrayList<Character> operator = new ArrayList<Character>();
		
        
		System.out.println("Enter equation:");
		userInput = scnr.nextLine();
		s = s.replaceAll(" ", "");
				
		
		if (s.equalsIgnoreCase('quit')) {
			break;
		}
		else {			
			for (int i = 0; i < s.length(); i++) {
				
				if (s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '*' || s.charAt(i) == '/' || s.charAt(i) == '^' || s.charAt(i) == '|'){
					operator.add(s.charAt(i));
				}
				else if ((!Character.isDigit(s.charAt(i)) {
					opperands.add(s.charAt(i))
				}

			}
		}
 
			
		//1. when quit is entered program will stop otherwise will continue
		//2. read user input and parse at new line
		//3. must be able to read mathmatical equation 
		//4. must handle parenthesis
		//5. must have order of presidence 
		//6. once parsed use methods in calculations.java
		//7. return result
		//8. if non computable return error
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java basic calculator

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,843
  • Joined: 12-June 08

Re: Java basic calculator

Posted 03 December 2019 - 03:06 PM

Quote

The major issue I am having with the program is how to get it to properly read and evaluate the expression while following all guidelines.

So, essentially "the assignment"? :D

Take small steps first.

First figure out how to read input from a user.
Then think how you would break apart basic things like "5 + 4" or "2-1" into relevant parts. What sort of storage would be nifty for that information?

Parenthesis are a pain, but do-able after that.

Armed with this you should be able to tackle complex input!
Was This Post Helpful? 0
  • +
  • -

#3 kkee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-December 19

Re: Java basic calculator

Posted 04 December 2019 - 07:14 AM

View Postmodi123_1, on 03 December 2019 - 03:06 PM, said:

Quote

The major issue I am having with the program is how to get it to properly read and evaluate the expression while following all guidelines.

So, essentially "the assignment"? :D/>

Take small steps first.

First figure out how to read input from a user.
Then think how you would break apart basic things like "5 + 4" or "2-1" into relevant parts. What sort of storage would be nifty for that information?

Parenthesis are a pain, but do-able after that.

Armed with this you should be able to tackle complex input!


View Postmodi123_1, on 03 December 2019 - 03:06 PM, said:

Quote

The major issue I am having with the program is how to get it to properly read and evaluate the expression while following all guidelines.

So, essentially "the assignment"? :D/>

Take small steps first.

First figure out how to read input from a user.
Then think how you would break apart basic things like "5 + 4" or "2-1" into relevant parts. What sort of storage would be nifty for that information?

Parenthesis are a pain, but do-able after that.

Armed with this you should be able to tackle complex input!





I guess I should have been more clear, I don't want it done for me but moreover I have no idea where to even start. I've tried breaking it down but I am unclear of what it is exactly I need to do.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,843
  • Joined: 12-June 08

Re: Java basic calculator

Posted 04 December 2019 - 08:17 AM

That's what I outlined above. Where to start.

What you need to do is get an equation string input from a user, and break that down in to some programmatical way to properly get the result.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1