Q:

How to make an exception based on the character found

I made a program that's supposed to read and store strings, and do some more stuff later on with them, here's an example string:
AAAAAAAAABBBBBBCCCC
BBBBBBBAAAAACCCCCCC

I want to make an exception in the scanner method, so that when I read it  and it finds un-wanted characters like '_' and '!' or 'f' it returns an exception and stops the rest of the program.
Basically I want it to JUST allow 3 characters (A, B and C). I have  no idea atm on how to write this, but there's probably a java method on how to do this :D
So how do I write an exception that only allows these 3?
public static void main(String[] args) throws FileNotFoundException, CharException {

  Scanner s = new Scanner(new File("C:/path/hehe.txt"));
  if (characters found are different than 'a', 'b', 'c') {
       break;

   }

I know that looks horrible, but yeah that's basically what I'm looking into writting. Thanks.

A:

Use Regex
String s = "AAAAAAAAABBBBBBCCCC";
if(s.replaceAll("[ABC]", "").length()>0){
    //incorrect
}else{
    //correct
}

It removes all A, B and C, and then checks if the length is greater than 0.
Or 
if(s.matches("[ABC]+")){
    //correct
}else{
    //incorrect
}

Note that it is case sensitive. Also, if the length of the String is 0, it would give incorrect

