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: Problem with detecting Math operation.

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

    Question Problem with detecting Math operation.

    Hello,

    I am having trouble testing to see what Mathematica operator the user has in-putted.

    Here is the function (There are others so don't worry that this wouldn't actually run)

    public static void calc()
    	{
    		Scanner calcinputS = new Scanner(System.in);
    		String calcinput, op;
    		int answer, fnum, snum;
     
    		System.out.println("-------------------------");
    		System.out.println("|     Calculator v1.0   |");
    		System.out.println("-------------------------");
    		System.out.print("  Enter an equation: ");
    		calcinput = calcinputS.nextLine();
     
    		String[] calcinputA = calcinput.split(" ");
    		fnum = Integer.parseInt(calcinputA[0]);
    		op = calcinputA[1];
    		snum = Integer.parseInt(calcinputA[2]);
     
    		if (op == "+")
    		{
    			answer = fnum + snum;
    			System.out.println(answer);
    		}
    		else if (op == "-")
    		{
    			answer = fnum - snum;
    			System.out.println(answer);
    		}
    		else if (op == "*")
    		{
    			answer = fnum * snum;
    			System.out.println(answer);
    		}
    		else if (op == "/")
    		{
    			answer = fnum / snum;
    			System.out.println(answer);
    		}
    		else
    		{
    			System.out.println("Error: Could not recognize operation: " + op);
    		}
     
    	}

    Thank you


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with detecting Math operation.

    You should use the equals() method for comparing Strings, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Problem with detecting Math operation.

    Ah, Thank you.

Similar Threads

  1. Help With Putting This Math Problem Into Code
    By Pettsa in forum Object Oriented Programming
    Replies: 23
    Last Post: May 28th, 2012, 06:48 PM
  2. Random math problem generator?
    By CamCompetes in forum Member Introductions
    Replies: 1
    Last Post: March 9th, 2012, 02:42 PM
  3. Math program problem.
    By dylanka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2011, 03:44 PM
  4. Math Problem
    By kidsforsale in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 14th, 2010, 04:09 PM
  5. [SOLVED] java me how to: input - math operation - output
    By Lifer in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 7th, 2010, 05:36 PM

Tags for this Thread