Results 1 to 10 of 10

Thread: Java Help - Very Basic Calculator

  1. #1
    Join Date
    Oct 2012
    Location
    Porto, Portugal
    Posts
    218
    Mentioned
    0 Post(s)
    Quoted
    42 Post(s)

    Question Java Help - Very Basic Calculator

    So I have been taking some video lessons on Java programming and after watching a video on doing a very basic calculator, as I have some good knowledge of scripting for simba bots myself I though I would give it a try and try to make it to be capable of doing more operations and not just adding.

    Although I get some errors and need someone to help me out here. So if you have time I appreciate.

    My code:

    Code:
    package bucky;
    
    import java.util.Scanner;
    
    class apples{
    	public static void main(String args[]){
    		Scanner bucky = new Scanner(System.in);
    		double num1, num2, answer;
    		String add;
    		String sub;
    		int operation;
    		add = "+";
    		sub = "-";
    		
    		System.out.println("Choose your operation: + or - ");
    		if (add = bucky.nextLine("+")){
    			operation = 1;
    		}
    		if (sub = bucky.nextLine()){
    			operation = 2;
    		}
    		System.out.println("Enter first number: ");
    		num1 = bucky.nextDouble();
    		System.out.println("Enter second number: ");
    		num2 = bucky.nextDouble();
    		if (operation == 1){
    			answer = num1 + num2;
    		}
    		if (operation == 2){			
    			answer = num1 - num2;
    		}
    		System.out.println("Your answer is: " + answer);
    	}
    }


    Errors I got:

    Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	The method nextLine() in the type Scanner is not applicable for the arguments (String)
    	Type mismatch: cannot convert from String to boolean
    
    	at bucky.apples.main(apples.java:16)

    If you are willing to help me please can you tell me what I am doing wrong, and give me some extra tips on that?


    Thanks in advance!

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    If statements require a boolean result within their parameters..

    You have:

    Java Code:
    if (add = bucky.nextLine("+"))...

    This cannot be. The result of the asignment operator is still a string. To get a boolean, you need a comparison operator such as:

    ==, ||, &&, !=, ===, etc..

    So perhaps:

    Java Code:
    if ((add = bucky.nextLine("+")) != null)...

    Same for the other if statements. :S Still confused as to why ppl use the scanner in this day and age.
    Last edited by Brandon; 08-12-2013 at 08:21 PM.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Sep 2012
    Location
    Netherlands
    Posts
    2,752
    Mentioned
    193 Post(s)
    Quoted
    1468 Post(s)

    Default

    You are following the boston tutorials right?

  4. #4
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Ofcourse ^ :P

  5. #5
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    For your first error
    Code:
    	The method nextLine() in the type Scanner is not applicable for the arguments (String)
    This occurs because Scanner.nextLine() has no parameter.

    If you are attempting to compare the value of add and Bucky.nextLine():
    Code:
    add.equals(Bucky.nextLine())
    Also it may be better to use a switch block for the operation control flow.

  6. #6
    Join Date
    Oct 2012
    Location
    Porto, Portugal
    Posts
    218
    Mentioned
    0 Post(s)
    Quoted
    42 Post(s)

    Default

    Thanks for the replies Brandon and riwu

    In here:
    Code:
    if (add = bucky.nextLine("+")){
    			operation = 1;
    I don't want to assign add to bucky.nextLine("+").
    I assigned add to be the signal + , and I want to know if it finds the + symbol in the line the user will imput. if it will then operation will be 1 (the add operation).
    You see the user will input + or - according to the operation he wants and then if he wrote + then the operation he wants is the operation 1 (add).
    How could I compare the add string (+) to the symbol it finds on the user imput line?

    Sorry for my ignorance on this matters and thank you for your patience.

  7. #7
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by Nirvana View Post
    Thanks for the replies Brandon and riwu

    In here:
    Code:
    if (add = bucky.nextLine("+")){
    			operation = 1;
    I don't want to assign add to bucky.nextLine("+").
    I assigned add to be the signal + , and I want to know if it finds the + symbol in the line the user will imput. if it will then operation will be 1 (the add operation).
    You see the user will input + or - according to the operation he wants and then if he wrote + then the operation he wants is the operation 1 (add).
    How could I compare the add string (+) to the symbol it finds on the user imput line?

    Sorry for my ignorance on this matters and thank you for your patience.
    Gave u the solution in prev post:
    Code:
    add.equals(Bucky.nextLine()) //returns true if add contains the same string literal as Bucky.nextLine()
    As Brandon mentioned, '=' is for assignment in java (equivalent to ':=' in Pascal). '==' is for equality comparison in Java (equivalent to '=' in Pascal), although using the equals() function for comparing objects is a better practice as using '==' returns true only if the objects point to the same memory address.
    Eg.
    Code:
    String a = new String("a");
    String b = new String("a");
    System.out.print(a == b); //returns false even though they have the same string literal as a and b are 2 distinct objects stored in distinct locations in the memory
    System.out.print(a.equals(b)); //returns true as equals() has been overridden to compare the wrappers' string literal
    Although in cases where a String object is not created through its constructor '==' would work due to the String pool feature of Java.

  8. #8
    Join Date
    Oct 2012
    Location
    Porto, Portugal
    Posts
    218
    Mentioned
    0 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    Gave u the solution in prev post:
    Code:
    add.equals(Bucky.nextLine()) //returns true if add contains the same string literal as Bucky.nextLine()
    As Brandon mentioned, '=' is for assignment in java (equivalent to ':=' in Pascal). '==' is for equality comparison in Java (equivalent to '=' in Pascal), although using the equals() function for comparing objects is a better practice as using '==' returns true only if the objects point to the same memory address.
    Eg.
    Code:
    String a = new String("a");
    String b = new String("a");
    System.out.print(a == b); //returns false even though they have the same string literal as a and b are 2 distinct objects stored in distinct locations in the memory
    System.out.print(a.equals(b)); //returns true as equals() has been overridden to compare the wrappers' string literal
    Although in cases where a String object is not created through its constructor '==' would work due to the String pool feature of Java.
    Thank you but now I got this errors:

    Code:
    The local variable operation may not have been initialized
    	The local variable operation may not have been initialized
    	The local variable answer may not have been initialized
    Any idea what it may be causing this error?

    Again thank you for helping me


    The code:

    Code:
    package bucky;
    
    import java.util.Scanner;
    
    class apples{
    	public static void main(String args[]){
    		Scanner bucky = new Scanner(System.in);
    		double num1, num2, answer;
    		String add;
    		String sub;
    		int operation;
    		add = "+";
    		sub = "-";
    		
    		System.out.println("Choose your operation: + or - ");
    		if (add.equals(bucky.nextLine())){
    			operation = 1;
    		}
    		if (sub.equals(bucky.nextLine())){
    			operation = 2;
    		}
    		System.out.println("Enter first number: ");
    		num1 = bucky.nextDouble();
    		System.out.println("Enter second number: ");
    		num2 = bucky.nextDouble();
    		if (operation == 1){
    			answer = num1 + num2;
    		}
    		if (operation == 2){			
    			answer = num1 - num2;
    		}
    		System.out.println("Your answer is: " + answer);
    	}
    }

  9. #9
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Nirvana View Post
    Thank you but now I got this errors:

    Code:
    The local variable operation may not have been initialized
    	The local variable operation may not have been initialized
    	The local variable answer may not have been initialized
    Any idea what it may be causing this error?

    Again thank you for helping me

    Java Code:
    package bucky;

    import java.util.Scanner;

    class apples{
        public static void main(String args[]) {
            Scanner bucky = new Scanner(System.in);
            double answer = 0;
            int operation = 0;
           
            System.out.println("Choose your operation: + or - ");

                    switch(bucky.nextLine()) {
                        case "+": operation = 1; break;
                        case "-": operation = 2; break;
                        default: throw new Exception("Invalid Operation"); break;
                    }

            System.out.println("Enter first number: ");
            double num1 = bucky.nextDouble();

            System.out.println("Enter second number: ");
            double num2 = bucky.nextDouble();

                    switch(operation) {
                        case 1: answer = num1 + num2; break;
                        case 2: answer = num1 - num2; break;
                    }
            System.out.println("Your answer is: " + answer);
        }


    Initializing variables are sometimes important and usually recommended so just create them on the fly and init them with the value of w/e..

    Another thing is you have branching in your program.. If statements like yours need else statements..

    Java Code:
    if (somecondition) {
    } else if (nextcondition) {
    } else {
    }

    Don't keep doing if, if, if, if.. It ends up checking every if statement and not all compilers are going to optimize that out.. In some cases, you need if, if, if.. however, in this case you don't.


    For faster responses, wrap your code in java tags. You can do so by doing:

    Code:
    [Highlight=Java]
    //Put code here..
    [/Highlight]
    Last edited by Brandon; 08-16-2013 at 10:57 PM.
    I am Ggzz..
    Hackintosher

  10. #10
    Join Date
    Oct 2012
    Location
    Porto, Portugal
    Posts
    218
    Mentioned
    0 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    Java Code:
    package bucky;

    import java.util.Scanner;

    class apples{
        public static void main(String args[]) {
            Scanner bucky = new Scanner(System.in);
            double answer = 0;
            int operation = 0;
           
            System.out.println("Choose your operation: + or - ");

                    switch(bucky.nextLine()) {
                        case "+": operation = 1; break;
                        case "-": operation = 2; break;
                        default: throw new Exception("Invalid Operation"); break;
                    }

            System.out.println("Enter first number: ");
            double num1 = bucky.nextDouble();

            System.out.println("Enter second number: ");
            double num2 = bucky.nextDouble();

                    switch(operation) {
                        case 1: answer = num1 + num2; break;
                        case 2: answer = num1 - num2; break;
                    }
            System.out.println("Your answer is: " + answer);
        }


    Initializing variables are sometimes important and usually recommended so just create them on the fly and init them with the value of w/e..

    Another thing is you have branching in your program.. If statements like yours need else statements..

    Java Code:
    if (somecondition) {
    } else if (nextcondition) {
    } else {
    }

    Don't keep doing if, if, if, if.. It ends up checking every if statement and not all compilers are going to optimize that out.. In some cases, you need if, if, if.. however, in this case you don't.


    For faster responses, wrap your code in java tags. You can do so by doing:

    Code:
    [Highlight=Java]
    //Put code here..
    [/Highlight]
    Thanks a lot. That switch statement is very helpful. Thank you for your time

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •