Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

string class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello there, i am trying to make an applet out of fun. i came across a problem and need u guys to help me out.

my prob is,: take for example im making a calculator. i need to user to input 2 numbers, and a method for calculation ( add, subtract). the user then types "subtract" as method of calculation.

i wrote this:

if(mtdcal == "subtract")
{
sum = num1 - num2;
}

mtdcal is what ever that is typed into the textfield by the user.

mtdcal = txt3.getText();

but it wont work after i compile and execute it. pls tell me what to change.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Joke names and "handles" aren't acceptable here. You can change your display name here. Thanks! We take this rule very seriously.

Now, on to your question: when you have a String variable -- or any other kind of object variable -- it's actually a reference to a String object, rather than the object itself. That means, for example, that multiple String variables can all be referring to the same physical String object in the computer's memory. The "==" operator asks if two variables refer to the same physical object.

But if one of your Strings is compiled into your code, and the other one comes from a user typing, they aren't going to be the same object -- they're going to be two separate objects that just happen to contain the same characters. The equals() method checks for this condition. In other words, replace

if (mtdcal == "subtract")

with

if(mtdcal.equals("subtract"))

and things will magically start working.

For a better and more entertaining explanation of this concept, see this and this.
 
Omar Nasir
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thx lots man. sry bout the name, i changed it. i was in a hury when i signed up, did not read the naming policy. sry again. thx. ^_^
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic