Friday, 15 February 2013

Java: How to search for a char variable in a string? -



Java: How to search for a char variable in a string? -

i need find position of char values derived user's input. indexof method not seem work when don't provide exact characters for. since char values derived user's input, cannot set in exact values. way utilize indexof way need to?

public class pg2a { public static void main(string[] args) { scanner keyboard = new scanner(system.in); string host; system.out .println("please come in sequence (any length, characters):"); host = keyboard.nextline(); system.out.println("now come in 3 character sequence:"); string candidate; candidate = keyboard.nextline(); int length = candidate.length(); char = candidate.charat(0); char b = candidate.charat(1); char c = candidate.charat(2); int = candidate.indexof(a); int j = candidate.indexof(b); int k = candidate.indexof(c); if (length == 3) { if (i == -1) system.out .println("the 3 character sequence entered not subsequence of sequence."); else system.out.println("let's go!"); } else { system.out .println("the sequence entered not 3 character sequence."); } } }

`

the code have should trivially set 0, j 1, , c 2 (assuming they're different characters). "give me index of first character", sec , third. believe meant host.indexof(a), etc.

however, there more trivial way of doing want, utilize host.indexof(candidate), should homecoming > -1 if candidate in host.

java char indexof

No comments:

Post a Comment