java - Please help me understand why my code doesn't work -
java - Please help me understand why my code doesn't work -
this question has reply here:
how compare strings in java? 23 answersthis class test see if 2 methods (the "endfinder"s) working (unfortunately not). endfinder() meant homecoming index of next non space character in provided string; endfinder2() meant homecoming endpoint of word starting index found endfinder. they're meant used pull out words string multiple words separated spaces. i've gone on few hours , there must obvious reply sense i'm going insane.
i've removed of outputs used test code in many attempts figure out wrong create cleaner.
basically, i've found endfinder() returns 0 reason , endfinder2() runs until reaches break (i assume that's happens used loop infinitely before added in).
the result want start = 3, end = 6 create output "[one]".
the result get:
[ 1 2 three] start = 0, end = 17.
here's code:
import java.io.*; import java.util.*; public class test { static string answer, container; static int start = 0, end = 1; public static int endfinder(string perspective) { int holder = start; while (perspective.substring(holder, (holder + 1)) == " ") { holder++; if (holder == perspective.length()) break; } homecoming holder; } public static int endfinder2(string perspective) { int holder = end; while (perspective.substring(holder, (holder + 1)) != " ") { holder++; if (holder == perspective.length()) break; } homecoming holder; } public static void main(string args[]) { system.out.println(); system.out.println(); container = " 1 2 three"; start = endfinder(container); end = start + 1; end = endfinder2(container); system.out.println("[" + container.substring(start, end) + "]"); system.out.println(); system.out.println("start: [" + start + "] end: [" + end + "]"); system.out.println(); } }
you want this:
static string answer, container; static int start = 0, end = 1; public static int endfinder(string perspective) { int holder = start; while (perspective.charat(holder) == ' ') { holder++; } homecoming holder; } public static int endfinder2(string perspective) { int holder = end; while (perspective.charat(holder) != ' ') { holder++; } homecoming holder; } public static void main(string args[]) { system.out.println(); system.out.println(); container = " 1 2 three"; start = endfinder(container); end = start + 1; end = endfinder2(container); system.out.println("[" + container.substring(start, end) + "]"); system.out.println(); system.out.println("start: [" + start + "] end: [" + end + "]"); system.out.println(); }
you should utilize charat compare character ' ' either != ' ' or == ' '. should modify loops utilize comparing shown above. doing work more needed. stripped out of code removed breaks loop (they end automatically when status not met). take @ code if have questions inquire away.
java
Comments
Post a Comment