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

Thread: Very Simple Beginning Java Help Needed

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Very Simple Beginning Java Help Needed

    I am currently teaching myself Java to prepare for college. I would like to have some prior experience with the language before entering the class headfirst and becoming completely lost.

    I'm making a program to perform very simplistic calculations. The problem I'm having is that my program should ask for input from the user three times. Twice for numbers and once for the operator. After the two numbers are entered, though, my program outputs "answer is 0.0" (the default value of double answer.)

    import java.util.Scanner;
    public class Calculator
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
     
    		System.out.print("Enter first number: ");
    		double number1 = input.nextDouble();
     
    		System.out.print("Enter second number: ");
    		double number2 = input.nextDouble();
     
    		System.out.print("Enter operator: ");
    		String operator = input.nextLine();
    		double answer = 0;
     
    		if(operator == "+")
    		{
    			answer = number1 + number2;
    		}
    		else if(operator == "-")
    		{
    			answer = number1 - number2;
    		}
    		else if(operator == "*")
    		{
    			answer = number1 * number2;
    		}
    		else if(operator == "/")
    		{
    			answer = number1 / number2;
    		}
     
    		System.out.println("answer is " + answer);
    	}
     
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Very Simple Beginning Java Help Needed

    Read the following to understand the differences between == operator and the .equals() method:
    http://www.javaprogrammingforums.com...-mistakes.html

    For the operator variable, to stop it skipping, you may either change it to input.next() or
    call input.nextLine() by itself before you prompt for operator.
    Calling input.nextLine() by itself essentially clears previous inputs for you.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    lgore93 (August 11th, 2011)

  4. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Very Simple Beginning Java Help Needed

    Thanks! Your link was very informative, too. I just started a tutorial on Java today and still feel very much a noob. ha.

Similar Threads

  1. Hangman game for JAVA Beginning class
    By shahravi88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 12th, 2011, 03:15 PM
  2. Restarting at the beginning of an array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2010, 10:58 AM
  3. simple java help needed
    By miss confused in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 27th, 2010, 12:29 PM
  4. Error in printing method as always it prints the last value
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 1st, 2009, 10:37 AM
  5. Beginning java programmer!!!!!!!!!!!!!!!!!! need help
    By raidcomputer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 15th, 2009, 08:52 PM