This is my code
import java.util.Scanner;
import java.io.*;
class Main {
public static void main (String[] args)throws IOException {
Scanner scan = new Scanner(System.in);
System.out.println ("Enter message board post:");
String username = scan.nextLine();
String check = username.substring (username.indexOf(" ")+3 , username.length()).toLowerCase()+" ";
int a = check.indexOf(" ^^ ");
int b = check.indexOf (" qey ");
int c = check.indexOf (" $ ");
String first = username.substring (0 , username.indexOf(" "));
int x = 0;
int y =0;
int z = 0;
while (a != -1) {
x++;
check = check.substring(a+1, check.length());
a = check.indexOf(" ^^ "); }
while (b != -1) {
y++;
check = check.substring(b+1, check.length());
b = check.indexOf(" qey "); }
while (c != -1) {
z++;
check = check.substring(c+1, check.length());
c = check.indexOf (" $ "); }
if ( x > 0 || y > 0 || z > 0){
System.out.println ("");
System.out.println ("BAD");
System.out.println (first);
System.out.println ("^^: " +x);
System.out.println ("qey: " +y);
System.out.println ("$: " +z);
}else {
System.out.println ("");
System.out.println ("CLEAN");
}
}
}
In the end the result is:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -18
at java.lang.String.substring(String.java:1967)
at Main.main(Main.java:23)
Which parts do I have to change to get rid of the thread? I input B0rg - resistance is futile QeY hahahaha $$$ ^^
and it should show up as
BAD
B0rg
^^: 1
qey: 1 $: 0
But it showed up as exception... Please help me...