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: Can someone take a look at my code? last few lines not doing anything.

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Can someone take a look at my code? last few lines not doing anything.

    Hey guys,

    I am new to the forum, and also new to java.

    I wanted to make a simple 4 operation calculator, but it wouldn't work.

    The problem is that after I enter in my first number, then my operation, and then my final number, the answer is never shown.

    Can someone help?

    the code:

    import java.util.Scanner;
     
    public class TheClas {
     
    	public static void main (String[] args){
    		float first;
    		float second;
    		String operation;
    		float answer = 0;
     
    		Scanner theFirst = new Scanner(System.in);
    		Scanner theSecond = new Scanner(System.in);
    		Scanner theOperation = new Scanner(System.in);
     
    		System.out.println("this is a calculator");
    		System.out.println("Pick your first number");
    		first = theFirst.nextFloat();
    		System.out.println("Pick the operation");
    		operation = theOperation.nextLine();
    		System.out.println("Now pick the last number");
    		second = theSecond.nextFloat();
     
    		if(operation.equals('*')){
    			answer = first*second;
    			System.out.println("the answer is: "+answer);
    		}
     
    		if(operation.equals('/')){
    			answer = first/second;
    			System.out.println("the answer is: "+answer);
    		}
     
    		if(operation.equals('+')){
    			answer = first+second;
    			System.out.println("the answer is: "+answer);
    		}
     
    		if(operation.equals('-')){
    			answer = first-second;
    			System.out.println("the answer is: "+answer+"!");
    		}
     
     
     
    	}
     
    }


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

    Default Re: Can someone take a look at my code? last few lines not doing anything.

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    '*' is not the same as "*". The first is a char, the second a String. Same with your other 'if' conditions.

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

    xelaton (May 17th, 2014)

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

    Default Re: Can someone take a look at my code? last few lines not doing anything.

    Oh, ok.

    So the problem was that I was treating the String like a char?

    Thanks for the answer btw

    --- Update ---

    Ok, great, that solved it.

    Thanks a lot for the fast and great answer

Similar Threads

  1. Code to open up a txt file and display only first five lines?
    By darkr166 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 6th, 2014, 10:00 PM
  2. [SOLVED] Trying to rewrite the main method to use as few lines of code as possible
    By jc100 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 26th, 2013, 08:28 PM
  3. why do some methods have lines through them
    By bean in forum Java Theory & Questions
    Replies: 4
    Last Post: August 30th, 2013, 09:28 AM
  4. Help with three lines
    By heythisgreg in forum Java Theory & Questions
    Replies: 9
    Last Post: February 17th, 2013, 05:40 PM
  5. Replies: 9
    Last Post: August 30th, 2012, 03:25 PM