From: laredotornado on 4 Nov 2009 16:06 Hi, I'm using Java 1.5, Eclipse Galileo on Mac 10.5.6 and the code checking plug-in (PMD) is complaining about the below block ... BufferedReader reader = new BufferedReader(new InputStreamReader (fileStream)); StringBuilder stringBuf = new StringBuilder(); String line = null; ... while ((line = reader.readLine()) != null) { stringBuf.append(line + "\n"); } saying, "Avoid assignments in operands". How would I rewrite the while loop to make this error go away but achieve the same functionality? Thanks, - Dave
From: Lew on 4 Nov 2009 16:21 laredotornado wrote: > I'm using Java 1.5, Eclipse Galileo on Mac 10.5.6 and the code > checking plug-in (PMD) is complaining about the below block ... > > BufferedReader reader = new BufferedReader(new InputStreamReader > (fileStream)); Hey, lighten up on the indentation! Use a maximum of four spaces per indent level and don't use TAB characters for Usenet code posts. > StringBuilder stringBuf = new StringBuilder(); Your variable name choice is slightly misleading. > String line = null; > ... > while ((line = reader.readLine()) != null) { > stringBuf.append(line + "\n"); > } > > saying, "Avoid assignments in operands". How would I rewrite the > while loop to make this error go away but achieve the same > functionality? > It's not an error, it's a warning and not even a standard warning for Java. It's a perfectly legal construct. However, it does elevate the scope of the variable 'line' beyond where it should be. Also, the assignment of 'null' to it is superfluous. So really your "checker" is giving you good advice. You could use a 'for' loop. for ( String line = reader.readLine(); line != null; line = reader.readLine() ) { ... } Does FindBugs work on the Mac? -- Lew
From: laredotornado on 4 Nov 2009 16:52 On Nov 4, 2:21 pm, Lew <l...(a)lewscanon.com> wrote: > laredotornado wrote: > > I'm using Java 1.5, Eclipse Galileo on Mac 10.5.6 and the code > > checking plug-in (PMD) is complaining about the below block ... > > > BufferedReader reader = new BufferedReader(new InputStreamReader > > (fileStream)); > > Hey, lighten up on the indentation! > > Use a maximum of four spaces per indent level and don't use TAB > characters for Usenet code posts. > > > StringBuilder stringBuf = new StringBuilder(); > > Your variable name choice is slightly misleading. > > > String line = null; > > ... > > while ((line = reader.readLine()) != null) { > > stringBuf.append(line + "\n"); > > } > > > saying, "Avoid assignments in operands". How would I rewrite the > > while loop to make this error go away but achieve the same > > functionality? > > It's not an error, it's a warning and not even a standard warning for > Java. It's a perfectly legal construct. However, it does elevate the > scope of the variable 'line' beyond where it should be. Also, the > assignment of 'null' to it is superfluous. So really your "checker" > is giving you good advice. > > You could use a 'for' loop. > > for ( String line = reader.readLine(); line != null; line = > reader.readLine() ) > { > ... > } > > Does FindBugs work on the Mac? > > -- > Lew Sweet! Works like a dream. 5 stars. I don't know if FindBugs works on a Mac but there is a plug-in for Eclipse and since Eclipse is cross-platform, I assume so, but haven't tried FindBugs yet. Thanks, -
From: Arved Sandstrom on 4 Nov 2009 19:32 laredotornado wrote: > On Nov 4, 2:21 pm, Lew <l...(a)lewscanon.com> wrote: [ SNIP ] >> >> Does FindBugs work on the Mac? >> >> -- >> Lew > > Sweet! Works like a dream. 5 stars. > > I don't know if FindBugs works on a Mac but there is a plug-in for > Eclipse and since Eclipse is cross-platform, I assume so, but haven't > tried FindBugs yet. > > Thanks, - FindBugs Eclipse plugin and FindBugs standalone work just fine on Mac OS X 10.5/10.6. AHS
From: Roedy Green on 5 Nov 2009 00:59 On Wed, 4 Nov 2009 13:06:20 -0800 (PST), laredotornado <laredotornado(a)zipmail.com> wrote, quoted or indirectly quoted someone who said : >hile ((line = reader.readLine()) != null) { > stringBuf.append(line + "\n"); > } > >saying, "Avoid assignments in operands". How would I rewrite the >while loop to make this error go away but achieve the same >functionality? That code is fine. There is really no other way to do it. However in general it is confusing to newbies if you write code of the form: x = ( a = b ); // = assignment embedded in expression. as opposed to x = ( a == b ); -- Roedy Green Canadian Mind Products http://mindprod.com An example (complete and annotated) is worth 1000 lines of BNF.
|
Next
|
Last
Pages: 1 2 3 Prev: show JDialog every 30 seconds Next: hi, am a beginner in java programming |