Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: I need a different approach - suggestions please Date: Tue, 26 Jun 2012 14:25:27 -0700 Organization: A noiseless patient Spider Lines: 65 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 26 Jun 2012 21:25:35 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="IZQsHU8CwMUPnWgvh4wwWA"; logging-data="17962"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/YftGo9ic1Wr+wfEHD6rq5Eb5+WBhfuk0=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 In-Reply-To: Cancel-Lock: sha1:c3fKCKIDyMbzvpWR1YGvJC88Z90= Xref: csiph.com comp.lang.java.programmer:15616 On 6/26/2012 1:04 PM, bilsch wrote: > Now that you mention it I see how that would work. However the actual > program has many non-numeric buttons I don't want in the string - I > better leave that alone for the present. A couple of things. First, even if true, it's better to do something like this: if( btn == "1" || btn == "2" || btn == "3" ... ) { // one single case here... } Than it is to use many different if-blocks. Same action for different inputs, you want to use one code block to implement that action. One other important point I'd like to make is that Java strings don't normally compare with ==. You have to use .equals() instead. Your code works now because all of the strings are in a single file, but as your program grows == will no longer work for you. This is the normal, and more correct, way to do it: if( btn.equals( "1" ) || btn.equals( "2" ) || ... ) { strng1 += btn; } Lastly, given your specific use case, there's a cheap quick way to cut down on verbosity. It involves knowing the API well, but String and Math (and a few others) are two APIs that you should memorize eventually to be a good Java programmer. (Other APIs it's OK to have to consult the documentation periodically.) String digits = "0123456789."; String opers = "+-/*"; String clear = "Clear"; if( digits.contains( btn ) ) { strng1 += btn; } else if( opers.contains( btn ) ) { // go do some math } else if( clear.equals( btn ) ) { strng1 = "0"; } Note the above block is untested. Caveat emptor. Later you'll be able to do the same thing with objects that aren't strings with the Set class. Set stuff1 = ... if( stuff1.contains( potentialMember ) ) { // take an action... }