Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Need Beginner Calculator Program help!

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need Beginner Calculator Program help!

    Welcome to Java Programming Forums. Read the Forum Rules.
    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.
    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.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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
    Enter your name:
    will make it more friendly for the user than waiting for input without saying anything.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Beginner Calculator Program help!

    but nothing happens after i enter it.
    Can you post the code you are having problems with so we can see it?

  7. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Beginner Calculator Program help!


  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Beginner Calculator Program help!

    Ok, let me help you:
    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;
        }
     
     
            }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  10. #10
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?

  12. #12
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Beginner Calculator Program help!

    1) it is awaiting more input. It does not exit.

    So what could that mean?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  14. #14
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  16. #16
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need Beginner Calculator Program help!

    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.

  18. #18
    Junior Member
    Join Date
    Dec 2011
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Beginner Calculator Program help!

    again... i already posted the code, look at the thing above

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

Similar Threads

  1. [SOLVED] Calculator Program
    By mwardjava92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 3rd, 2011, 11:01 AM
  2. Replies: 3
    Last Post: October 19th, 2011, 07:57 AM
  3. Beginner Java Program Help/ txt database search
    By JavaConfused in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 21st, 2011, 09:20 AM
  4. Issue with beginner program
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:55 AM
  5. My Algebraic Calculator Program.
    By crazed8s in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2010, 03:07 AM

Tags for this Thread