The user must enter any expression as a formula. For example, a + b + c-d + f that the user enters into the console. If the user enters the wrong formula, that is, a + df + sdfg or + a + 5 + b + c //, then an exception should be raised that the formula has the wrong structure. The formula should consist only of letters of the alphabet and signs. Numbers should not be and should be entered only small letters.
String sIn = null; Scanner in = new Scanner(System.in); System.out.println("Введите формулу: "); sIn = in.nextLine(); boolean flag=true; for (int j = 0; j < d.length(); j++) { ch = d.charAt(i); String s = Character.toString(ch); if(s.matches("[az]")&& Character.toString(d.charAt((d.length()-1))).matches("[az]")){ // ..... } else new Throwable("Неправильная структура формулы"); here only the first and the last letter of the alphabet is checked. But if the user enters a letter without letters or large letters, then the exception does not pop up. Thank.
If you do this:
boolean bool =true; char ch,chfirst, chlast; String str="a+b-c+a"; chfirst = sIn.charAt(0); chlast = sIn.charAt(sIn.length()-1); if(Character.toString(chfirst).matches("[az]")&& Character.toString(chlast).matches("[az]")){ for (int j = 0; j < sIn.length(); j++) { ch = sIn.charAt(j); if(j%2==0 && Character.toString(ch).matches("[az]")){ bool = true; } } } else bool = false; System.out.println(bool); then the result is more desirable, but if you enter not a letter of a letter and together for example aa + bb + c-r + d, then the result also gives true. How to make it so that only letters can be entered separately, for example, d + b + ac? Thank.