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: Help with a calculator

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with a calculator

    I am quite new to java, I am trying to make a calculator. What i don't understand is in the last line of code says "The local variable answer may not have been initialized"? How do I initialize variable answer?


    import java.util.Scanner;
    public class Basic_Calculator {
    	public static void main (String args[]){
    		Scanner Bucky = new Scanner(System.in);
    		double fnum, snum, answer = ;
    		String symbol;
    		System.out.println("Enter first number");  
     
    		fnum = Bucky.nextDouble();
    		System.out.printf("what is you second number?");
     
    		snum = Bucky.nextDouble();
    		System.out.println("What will you do with these numbers?");
     
    		symbol = Bucky.next();
     
    		if (symbol == "*"){ 
    			answer = snum * fnum;}
     
    			 if(symbol == "/"){
    			 answer = snum / fnum;}
     
    			 if (symbol == "+"){
    			 		answer = snum + fnum;}
     
    			 		 if (symbol == "-"){
    			 				answer = snum - fnum;}			 		 
     
    			System.out.printf("Your answer is: "+ answer);
    			}
    }


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a calculator

    Declare that answer equals to 0.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a calculator

    When I run it the answer is always equals 0.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with a calculator

    Do not compare Strings with the equality operator, "==". Use the equals() method.

Similar Threads

  1. Calculator
    By Vibex in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2012, 05:23 PM
  2. Help with a calculator
    By Joshroark10 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 10th, 2012, 03:59 PM
  3. [SOLVED] Calculator
    By ikocijan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 26th, 2012, 05:54 AM
  4. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  5. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM

Tags for this Thread