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 14 of 14

Thread: Basic Calculator

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Basic Calculator

    Hey, I'm having some problems a basic calculator that I'm messing about with in Java. Would be appreciated if some of you guys could give me some pointers as to where I'm going wrong...

     import java.util.Scanner;
     
    public class Calculator {
     
        Scanner kybd;
        kybd  = new Scanner(System.in);
     
        public void main(String[] args) {
     
            String calcname = "Calculator";
            System.out.println("Welcome to " + calcname + "please enter your name");
            String name = kybd.readString();
            System.out.println("Welcome. Press 1 to multiply and 2 to divide");
            int choice = kybd.readInt();
            choice = kybd.nextInt();
     
            if (choice == 1) {
                System.out.println("Enter your first number");
                int num1 = kybd.readInt();
                System.out.println("Enter your second number");
                int num2 = kybd.readInt();
                System.out.println(num1 + " times " + num2 + "equals" + num1 * num2 + ".");
            } else if (choice == 2) {
                System.out.println("Enter your first number");
                int Dnum1 = kybd.readInt();
                System.out.println("Enter your second number");
                int Dnum2 = kybd.readInt();
                System.out.println(Dnum1 + " divided by " + Dnum2 + "equals" + Dnum1 / Dnum2 + ".");
            } else {
                System.out.println("Please check your input");
            }
        }
    }

    Cheers!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Basic Calculator

    You're trying to perform statements outside of a method. This is only allowed for same-line initialization.
    Also, by default Java declares variables inside classes as instance fields, not static so you can't access them directly from a static method like main.

    Quick fix:

    move these two lines inside the main method (first two lines)
        Scanner kybd;
        kybd  = new Scanner(System.in);

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Yes. (June 1st, 2010)

  4. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Basic Calculator

    Quote Originally Posted by helloworld922 View Post
    You're trying to perform statements outside of a method. This is only allowed for same-line initialization.
    Also, by default Java declares variables inside classes as instance fields, not static so you can't access them directly from a static method like main.

    Quick fix:

    move these two lines inside the main method (first two lines)
        Scanner kybd;
        kybd  = new Scanner(System.in);


    Hey cheers for that!

    The one other problem I have is I'm getting the red line under all of the readString and readInt's. Netbeans say's it can't find the symbol. Which to me just doesn't make sense...

  5. #4
    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: Basic Calculator

    What class are the readString() and readInt() methods members of?
    What is the class of the object used with them?

    Read the API doc to find the answers.
    At the top of the API docs page there is an "Index" link. Click that, chose "R" and search for readInt() to see what classes have it.

  6. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Basic Calculator

    Quote Originally Posted by Norm View Post
    What class are the readString() and readInt() methods members of?
    What is the class of the object used with them?

    Read the API doc to find the answers.
    At the top of the API docs page there is an "Index" link. Click that, chose "R" and search for readInt() to see what classes have it.
    I think I've got things a bit confused

    The message that I get when I drag he cursor over the error is: cannot find symbol. symbol: method readString() location: class java util.Scanner. To me that just doesn't make sense becauase the scanner is at the top of the class- where it is usually and never causes problems like this

    I am using the only one class-Calculator. I'm not using objects and such like- the whole program run's from that one class- well that's what I want it to do haha.

    I'll give the API doc a look- thanks.

  7. #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: Basic Calculator

    I'm not using objects
    Think you are. Only if you code with primitives such as int and char can you NOT use objects.
    Many of the variables in your code refer to objects of class such as String and Scanner.

  8. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Basic Calculator

    Quote Originally Posted by Norm View Post
    Think you are. Only if you code with primitives such as int and char can you NOT use objects.
    Many of the variables in your code refer to objects of class such as String and Scanner.
    Ok then, I don't want to be using objects. I want the program to be just one class. But, making edits to the code creates more errors and more problems it seems.

    I fully understand that my ramblings about the code probably seem pretty confused - but... I'm new to this haha!

  9. #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: Basic Calculator

    Sure, it takes time to learn the new vocabulary and terms.
    I don't want to be using objects
    I'm afraid you'll have to use objects if you use the Scanner or String classes.

  10. #9
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Basic Calculator

    As far as I'm aware the Scanner (Java Platform SE 6) class does not have any read methods, only next and nextXxx methods.

    // Json

  11. #10
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Basic Calculator

    Ok then cheers guys.

    Would any of you be able to give me a few pointers on how to create the calculator just using the one class- It's not like I have NO code- but it seems that I have things very muddleded up haha :p

  12. #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: Basic Calculator

    Put all your code in the main() method. Then you only define the one class that main() is in.

  13. #12
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Basic Calculator

    Quote Originally Posted by Norm View Post
    Put all your code in the main() method. Then you only define the one class that main() is in.
    Please tell me you're not serious.

    db

  14. #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: Basic Calculator

    Read the OPs requirements. I answered his question without any comments.

    You should explain to the OP about OOP and why using the main() for simple programs is a bad idea.

  15. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Basic Calculator

    Quote Originally Posted by Yes. View Post
    Hey, I'm having some problems a basic calculator that I'm messing about with in Java. Would be appreciated if some of you guys could give me some pointers as to where I'm going wrong...

     import java.util.Scanner;
     
    public class Calculator {
     
        Scanner kybd;
        kybd  = new Scanner(System.in);
     
        public void main(String[] args) {
     
            String calcname = "Calculator";
            System.out.println("Welcome to " + calcname + "please enter your name");
            String name = kybd.readString();
            System.out.println("Welcome. Press 1 to multiply and 2 to divide");
            int choice = kybd.readInt();
            choice = kybd.nextInt();
     
            if (choice == 1) {
                System.out.println("Enter your first number");
                int num1 = kybd.readInt();
                System.out.println("Enter your second number");
                int num2 = kybd.readInt();
                System.out.println(num1 + " times " + num2 + "equals" + num1 * num2 + ".");
            } else if (choice == 2) {
                System.out.println("Enter your first number");
                int Dnum1 = kybd.readInt();
                System.out.println("Enter your second number");
                int Dnum2 = kybd.readInt();
                System.out.println(Dnum1 + " divided by " + Dnum2 + "equals" + Dnum1 / Dnum2 + ".");
            } else {
                System.out.println("Please check your input");
            }
        }
    }

    Cheers!
    Did you try static Scanner kbrd = new Scanner(System.in); ?

    Also, did you have try and catch block in case it divides by zero?

    Like
    boolean isOk = false;
     
    while (isOk == false)
    {
    try
    {
    num1 = kbrd.nextInt();
    num2 = kbrd.nextInt();
     
    num3 = num1/num2;
    isOk = true;
    }
     
     
     catch (InputMismatchException imeRef )
                         { // beginning of catch
                            String str ;
                            str = console.next();
                            System.out.println(   "Enter a value"
                               +" that is an integer, i.e.  not a decimal "  + 
                               "\n" + imeRef.toString());
     
                         } // end of catch
     
     
    } // end of while
    Last edited by helloworld922; June 4th, 2010 at 07:09 PM. Reason: please use [code] tags

Similar Threads

  1. calculator GUI needs to compute
    By javanovice in forum AWT / Java Swing
    Replies: 3
    Last Post: May 4th, 2010, 02:16 PM
  2. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM
  3. Help codiing calculator
    By FM010 in forum Exceptions
    Replies: 7
    Last Post: June 12th, 2009, 02:25 PM
  4. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 11:29 AM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM