java - Is it necessary to resolve the errors shown in the output window of Netbeans IDE even if my code is working the way I want? -
say want 'of' typed in input user
i making app, not finishing off in dumb output screen (this example)
import java.util.*; scanner input = new scanner(system.in); string text = input.nextline(); int check = 0; for(int = 0; < text.length(); i++;){ if(text.substring(i, + 2) .equals("of")){ check = 0; } }
if user enters abofd
, surely recognises of @ position @ 2-4. when i
value 4 checks position 4-6, position 6 not present not present gives error. know thinking me set i < text.length() - 1
@ line 5, original code needs run until end!
it good idea validate input coming user, overcomplicating things! worse, wrote down outright wrong code:
string text = input.nextline(); int check = 0; for(int = 0; < text.length(); i++;){ if(text.substring(i, + 2) .equals("of")){
the above can't work! see, i iterates 0 text length. using i+2 substring text. (so: hello first arrayindexoutofboundsexception).
instead, can things like:
if (text.contains("of")) {
or
if (text.indexof("of") >= 0) {
to find out if string contains "of".
and answer question in title: absolutely yes. programming being craftsman large degree. craftsman keeps tools , materials in order. doesn't allow mess, waste, ...
so, long story short: day one, eternity: when writing code, strive zero tolerance policy: no compiler errors, no warnings, nothing in code doesn't belong there!
Comments
Post a Comment