Hi, I want the input to accept only: digits, operators and space. How could I do?
I mean:
When I type: 8 – 9 a b c ! ?
It should throw exception.
When I type: 8 – 9 + 20 * 1 / 5
It should end in success.
I tried but could not
Code:package assignments; import java.util.Scanner; public class RegexTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.next(); // try to input: 8 - 9 + 20 * 1 / 5 try { if(s.matches("[^ \\d \\+ \\- \\* \\/]+")) throw new Exception(); } catch (Exception e) { System.err.println("Recycle bin: \"I\'m here\""); } } }





Reply With Quote