I'm really happy that as a teacher, you are performing research before giving your pupils an assignment. For that, have a cow!
•You're using parallel lists. Instead, encapsulate parallel elements in objects and maintain a single list.
•Use Java conventions for capitalization. Instance fields should be in camel-case, starting with a lower-case letter.
•Your variable names are unreadable. Don't use names like fz, untilC, p or pf. Use descriptive names. Characters don't cost anything.
•Put spaces after commas and semi-colons, not before, put spaces around operators, and put spaces before opening braces.
•Put 'then'-clauses on a separate line from the if-condition.
•If two 'else-if' statements have the same 'then'-clause, then just perform a union over their conditions with the || operator.
•Use consistent indentation: Block statements with the same scope should be indented with the same whitespace.
•Don't modify loop variables inside the loop. Instead of forcing i to the last index, use a break statement.
•Use early returns instead of result variables: Get rid of the ok variable.
•Don't declare variables before you use them.
Aleana Cologne wrote:
Mike Simmons wrote:We also "know" that the method builds the address with a ":" rather than a "-". And yet, we're willing to speculate on what the authors meant to write. Or what the OP may have miscopied. I don't think any of this is definite at this point - for all I know, the question in the book might actually have a print statement, which the OP misremembered as a return statement. Or not.
Assuming the question was copied directly, and therefore needs revision anyway... I do think that the question would be better if the ambiguity about what is "output" (print or return value) were removed. Replacing the return with a print statement would make things clearer in that sense, if a correction is required.
Campbell Ritchie wrote:How are you supposed to know that the wrapper class throws such exceptions? Does it say anything in the book or the documentation that the method throws its own exceptions?
Gp and look through your Java® installation folder; there is a file called src.zip in there (at least there often is). Unzip that file and go to the .../java.base/java/lang/Integer.java file, and in the code for the parseInt(String, int) method, you will find all sorts of lines like this:-That confirms what CO'L told you about how such exceptions are thrown, but how would you know without looking at the source?