Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!204.52.135.9.MISMATCH!newsfeed.hal-mli.net!feeder1.hal-mli.net!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: I need a different approach - suggestions please Date: Tue, 26 Jun 2012 14:32:15 -0700 (PDT) Organization: http://groups.google.com Lines: 81 Message-ID: <05a0cbeb-c97e-436b-9daf-e158445b0ac7@googlegroups.com> References: NNTP-Posting-Host: 69.28.149.29 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1340746781 8447 127.0.0.1 (26 Jun 2012 21:39:41 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 26 Jun 2012 21:39:41 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.28.149.29; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Received-Bytes: 3425 Xref: csiph.com comp.lang.java.programmer:15620 markspace wrote: > 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; > } switch (btn) { case "1": case "2": doCaseOneAndTwo(); break; case "3": doCaseThree(); break; default: doAllOtherCases(); break; } > 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.) Also you should be very, very familiar with the collections classes (mostly in java.util.*). > 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... > } -- Lew