{"id":"code-hs-java-substring-index-check","audience":"high-school","category":"coding","track":"coding","technique":"format","title":"See why your substring answer is off by one","free":false,"task":"Your answer to a Java practice question was \"MPUT\" but the key says otherwise. Ask for exactly 3 numbered lines: line 1 lists every letter of COMPUTER with its index like 'C0 O1 M2 ...'; line 2 starts 'Takes:' and says which indexes substring(2, 5) uses; line 3 starts 'Result:' with the right answer in quotes and what you included by mistake. No code blocks, under 60 words.","materialLabel":"The practice question and your answer (attached to your prompt automatically)","material":"AP CSA practice set 2 - String methods (Ms. Brennan)\n\nQuestion 4.\nString word = \"COMPUTER\";\nString part = word.substring(2, 5);\nSystem.out.println(part);\n\nWhat is printed?\n\nMy answer: MPUT\nMy reasoning: start at 2 which is M, end at 5 which is T, so MPUT.\n\nAnswer key: ______ (it's not MPUT, I checked)\n\nFrom the Java Quick Reference sheet:\nString substring(int from, int to)\n  Returns the substring beginning at index from and ending at index to - 1\n\nOther notes:\n- indexes start at 0 in Java\n- word.length() is 8\n- substring(3) would go to the end\n- I always mix up whether the end number is included","rules":[{"type":"numberedItems","count":3},{"type":"codeBlocks","count":0},{"type":"maxProseWords","max":60}],"mistakes":[{"title":"Getting a lecture","why":"The AI explains String methods in general instead of your exact mistake.","fix":"Say: \"Exactly 3 numbered lines.\"","checks":["numberedItems","maxProseWords"]},{"title":"No index map","why":"Without seeing each letter's index, it's hard to see where you went off.","fix":"Ask: \"Line 1: every letter with its index, like C0 O1 M2.\"","checks":["r0"]},{"title":"No labels","why":"Unlabeled lines blur the rule and the answer together.","fix":"Add: \"Line 2 starts 'Takes:', line 3 starts 'Result:'.\"","checks":["r1","r2"]},{"title":"Code you didn't need","why":"The AI may write a whole Java program to demonstrate.","fix":"Add: \"No code.\"","checks":["codeBlocks"]}],"promptHints":[{"label":"Sets the number of lines","patterns":["\\b(3|three)\\b.{0,15}line","numbered"]},{"label":"Asks for an index map","patterns":["index","C0","each letter"]},{"label":"Gives the labels","patterns":["takes:","result:"]},{"label":"Asks what you got wrong","patterns":["mistake","what i (got wrong|included)","my answer"]},{"label":"Rules out code","patterns":["no code","short","\\d+\\s*words"]}],"hint":"Off-by-one mistakes are easiest to see when every index is laid out. Ask for the index map, the rule applied, and the result in fixed labels.","rubricLabels":["Numbered line 1 is a single line exactly in the form C0 O1 M2 P3 U4 T5 E6 R7 (a vertical list or table fails)","Numbered line 2 begins with the label 'Takes:' (without that label it fails) and says substring(2, 5) uses indexes 2, 3 and 4, stopping before 5","Numbered line 3 begins with the label 'Result:' (without that label it fails), gives \"MPU\" in quotes, and says the T at index 5 was included by mistake"]}