Aşağıdaki programın amacı, "/"
" "
ve ":"
ayırıcıları sayesinde kaynak dizesini sınırlandırmaktır. Beklenen çıktı, 20 03 2016 17 30
ancak son öğeyi atlayarak yalnızca 20 03 2016 17
verir. Belki bazı teker teker hata?Java dize sınırlama programında birebir hata
public static void main(String[] args) {
String source = "20/03/2016:17:30";
String sep = "/:";
String[] result = new String[5];
String str = "";
int index = 0;
for (int sourcePos = 0; sourcePos < source.length(); sourcePos++) {
int compt = 0;
for (int sepPos = 0; sepPos < sep.length(); sepPos++) {
if (source.charAt(sourcePos) == sep.charAt(sepPos)) compt++;
}
if (compt > 0) {
result[index] = str;
System.out.print(" " + result[index]);
if (index < result.length)
index++;
else
break;
str = "";
} else {
str = str + source.charAt(sourcePos);
}
}
}
off-by-one hata açıklaması için teşekkürler :) – loukios
Bu yorumu eklemek için üzgünüz, ama çıktı olarak kodunuzla başka bir tür bir-off-one hatası gibi görünüyor 03/2016: 16 "yerine" 20 03 2016 17 30 ". Bir fikrin neden? – loukios