Need Beginner Calculator Program help!
I am working on a small java program for my first class and my last project is a calculator. This is the code I have so far
and this is the actual project details given by my teacher
http://cullenprogramming.homelinux.c...oms114proj.pdf
My main difficulty is figuring out where i enter the code to prompt the user for input. Should it be in the main method? or do i need to create another method just for that? And if someone could break down and explain some of the code that would be great.Thanks, any help is appreciated.
Re: Need Beginner Calculator Program help!
Welcome to Java Programming Forums. Read the Forum Rules.
Quote:
My main difficulty is figuring out where i enter the code to prompt the user for input. Should it be in the main method? or do i need to create another method just for that?
It depends upon you, you can get input anywhere you want, if not specifically mentioned.
Quote:
And if someone could break down and explain some of the code that would be great
Why don't you try on your own and if stuck, ask us here.
Re: Need Beginner Calculator Program help!
Ok i guess i dont understand a lot of the methods because my teacher started us off with them and just told us to finish it by friday pretty much.
So my main method calls the x,process and then that executes a method that checks the user input but no user input has been entered yet. so my question is where do I put that in? after i declare the scanner sc should i put in a println to ask for input and then assign the next line entered to be s?
Re: Need Beginner Calculator Program help!
It all depends upon you again, if you want user to show that (s)he must enter input, write in println and then get input.
Well, the best practice is to make your program user friendly so YES, printing like
will make it more friendly for the user than waiting for input without saying anything.
Re: Need Beginner Calculator Program help!
ok now my program atually puts out a messge to the user: Enter an expression. but nothing happens after i enter it.
Here is my code now, take a look [Java] import java.util.*; public class Calc { - Pastebin.com
thanks
Re: Need Beginner Calculator Program help!
Quote:
but nothing happens after i enter it.
Can you post the code you are having problems with so we can see it?
Re: Need Beginner Calculator Program help!
Re: Need Beginner Calculator Program help!
Ok, let me help you:
Code :
import java.util.*;
public class Calc {
int rc;
static int src;
double result;
double evalexpr;
double m;
String s;
String token;
String operator;
String expr;
String peektoken;
int cursor = 0;
public static void main(String[] args)
{
Calc x = new Calc();
src = x.process();
return;
}
private int process()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter an expression");
s = sc.nextLine();
boolean keepgoing = true;
while(keepgoing == true)
{
String j = sc.nextLine();
j = j.trim();
if (j== null)
{
keepgoing = false;
System.out.println("The input is" + token);
result = evalexpr();
continue;
}
}
return 0;
}
double evalterm()
{
double a = evalfactor();
token = peektoken();
if((token.equals("*")) || (token.equals("/")))
{
operator = token;
cursor++;
double b = evalfactor();
if ("*".equals(operator))
{
a=a*b;
}else{
a=a/b;
}
}
return a;
}
double evalexpr()
{
double x = evalterm();
token = peektoken();
double z;
if((token.equals("+")) || (token.equals("-")))
{
operator = token;
cursor++;
double y = evalterm();
if (operator == "+")
z = x + y;
else
z = x - y;
}
return x;
}
double evalfactor(){
token = peektoken();
if(token.equals("("))
{
cursor++;
double K = evalexpr();
}
else{
cursor++;
double r = Double.parseDouble(token);
}
return x;
}
private String peektoken() {
{
String j = "";
while(cursor<expr.length())
{
j = expr.substring(cursor,cursor+1);
if(j.matches("[0-9]"))
{
return j;
}
if(j.equals(" "))
{
cursor++;
continue;
}
if(j.matches("[+-*/]"))
{
return j;
}
System.out.println("Error");
return j;
}
return null;
}
}
String token(){
double evalterm();
return main;
}
}
Re: Need Beginner Calculator Program help!
Can you explain what "nothing happens" means?
Is the program waiting for more input?
Does the program quickly execute and then exit?
Add some println statements that shows where the code is executing if it exits without any output.
Re: Need Beginner Calculator Program help!
ok i just entered a bunch of println statements to see where my program was going and i got nothing in return. this is the changed code with the print ln statements [Java] import java.util.*; public class Calc { - Pastebin.com
if u run it on your computer you will see that it doesnt print any of the statements, just "Enter an expression"
so im guessing that means none of my methods are being reached right?
Re: Need Beginner Calculator Program help!
1)Is the program waiting for more input?
2)Does the program quickly execute and then exit?
Looking at what your code printed out was it 1) or 2) that is happening?
Re: Need Beginner Calculator Program help!
1) it is awaiting more input. It does not exit.
So what could that mean?
Re: Need Beginner Calculator Program help!
The user needs to type in some input.
Your should print out a message before EVERY read/nextLine statement so the user knows he is supposed to enter something.
Re: Need Beginner Calculator Program help!
what are you talking about? i am making a calculator program. I prompt the user to enter an expression and i expect the answer to be sent back. I want an answer coming back when i enter an expression and nothing comes back, it just awaits more input until i close it
Re: Need Beginner Calculator Program help!
You do not have to ask the user continually for more input as long as the user knows what to expect.
Have you tried entering something when the program is waiting for input?
Where is the program waiting for input? It should be the next statement after your last print out if you were careful to put a lot of printlns in the code so that you would know exactly where the program is executing.
Re: Need Beginner Calculator Program help!
try my new code in netbeans or w/e you use
[Java] import java.util.*; public class Calc { - Pastebin.com
now i can get to my while loop becuase my println tells me. but then it doesnt execute the string i enter in. why is that?
Re: Need Beginner Calculator Program help!
Quote:
it doesnt execute the string i enter in. why is that?
What does the code do to "execute the string" you entered?
Post your code here on the forum if you want help with it.
Re: Need Beginner Calculator Program help!
again... i already posted the code, look at the thing above
Re: Need Beginner Calculator Program help!
Sorry, I don't see your code. I'd rather not have to chase some link to find it.