对于作业,我必须输入一个字符串并将其反转,同时如果字符串中的字符不是字母、数字或空格,则抛出非法字符异常。我的教授说不需要方法 throw 子句。
这是我的代码
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
我在第 15 行和第 23 行不断收到此错误错误:IllegalCharacterException 无法解析为类型