Pour une affectation, je dois entrer une chaîne et l'inverser, tout en lançant une exception de caractère illégal si un caractère de la chaîne n'est pas une lettre, un chiffre ou un espace blanc. Mon professeur dit qu'il n'y a pas de clause de rejet de méthode.Pourquoi cette exception de caractères illégaux n'est-elle pas résolue en un type?
Voici mon code
import java.util.*;
import java.io.*;
public class reverse3
{
public static void reverse(String x)
{
char j;
String reversedString;
for(int i = 0; i < x.length(); i++)
{
try
{
if(!Character.isDigit(x.charAt(i)) && !Character.isLetter(x.charAt(i)) && !Character.isWhitespace(x.charAt(i)))
{
throw (new IllegalCharacterException("Illegal Character in String"));
}//end if
else if(Character.isDigit(x.charAt(i)) && Character.isLetter(x.charAt(i)) && Character.isWhitespace(x.charAt(i)))
{
j = x.charAt(i);
j = reversedString.charAt(i - 1);
}//end else if
}//end try
catch(IllegalCharacterException e)
{
System.out.println(e.getMessage());
}//end catch
}//end for loop
}//end method
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String s;
System.out.println("Please enter a string");
s = keyboard.next();
reverse(s);
}//end main
}//end class
Je continue à obtenir cette erreur à la ligne 15 et 23 Erreur: IllegalCharacterException ne peut être résolu à un type
Avez-vous défini votre propre classe d'exception 'IllegalCharacterException'? – rgettman
Vous devez probablement l'importer. – Gendarme
Dans votre capture, vous pourriez faire 'catch (Exception e) {e.printStackTrace(); } 'et cela vous dirait ce qui se passe. Catching Exception e attrape toutes les exceptions possibles – Joris