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

Thread: Calculator

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculator

    I've just started learning Java (only my second day) but can anyone explain to me why this code doesn't work. Its code for a basic calculator every time i try to compile it says found Int but expected boolean. Thanks in advance.

    import java.util.Scanner;
    public class Calculator2{

    public static void main (String[] args){
    Scanner keyboard=new Scanner(System.in);
    int x = keyboard.nextInt();
    if(x = 1){
    System.out.print("1st number: ");
    Scanner scan = new Scanner(System.in);
    int num1 = scan.nextInt();
    System.out.print("\n2nd number: ");
    int num2 = scan.nextInt();
    int num3 = num1 + num2;
    System.out.print("\nThe answer is: " + num3);
    }
    else if(x = 2){
    System.out.print("1st number: ");
    Scanner scan = new Scanner(System.in);
    int num4 = scan.nextInt();
    System.out.print("\n2nd number: ");
    int num5 = scan.nextInt();
    int num6 = num4 - num5;
    System.out.print("\nThe answer is: " + num3);
    }
    else if(x = 3){
    System.out.print("1st number: ");
    Scanner scan = new Scanner(System.in);
    int num7 = scan.nextInt();
    System.out.print("\n2nd number: ");
    int num8 = scan.nextInt();
    int num9 = num7 * num8;
    System.out.print("\nThe answer is: " + num3);
    }
    else{
    System.out.print("1st number: ");
    Scanner scan = new Scanner(System.in);
    int num10 = scan.nextInt();
    System.out.print("\n2nd number: ");
    int nu11 = scan.nextInt();
    int num12 = num10 / num11;
    System.out.print("\nThe answer is: " + num3);
    }

    }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calculator

    The error message that you've received from the compiler will come with line numbers, and to best help us out, you should indicate for us which line is causing the compilation error. I suggest you show the actual compiler error message in its entirety, and also indicate in your code with a comment which line is causing the problem.

    edit: ah I see. Your if and else blocks will need the equality operator, ==, not the assignment operator, =

    So instead of
    else if(x = 2){

    do,
    else if(x == 2){

    this:
    x = 2
    will assign the value 2 into the x variable which is not what you're trying to do.

    whereas this:
    x == 2
    will check if x holds the value 2, a big difference.
    Last edited by curmudgeon; September 14th, 2012 at 05:18 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculator

    Sorry it's

    if(x = 1){

    And i'm assuming it will also occur at
    else if(x = 2){
    else if(x = 3){

    Edit: Thank you
    Last edited by Vibex; September 14th, 2012 at 05:20 PM.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Calculator

    You're welcome. Good luck with your assignment and your Java programming education!

Similar Threads

  1. [SOLVED] Calculator
    By ikocijan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 26th, 2012, 05:54 AM
  2. Calculator Using AWT
    By Allicat in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 17th, 2011, 06:49 AM
  3. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  4. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  5. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM

Tags for this Thread