I have no idea how to check if char[]
contains only one letter (a or b) on the first position and only one int (0-8) on the second position. for example a2
, b2
I have some this, but I do not know, what should be instead of digital +=1
;
private boolean isStringValidFormat(String s) { boolean ret = false; if (s == null) return false; int digitCounter = 0; char first = s.charAt(0); char second = s.charAt(1); if (first == 'a' || first == 'b') { if (second >= 0 && second <= '8') { digitCounter +=1; } } ret = digitCounter == 2; //only two position return ret; } ` public char[] readFormat() { char[] ret = null; while (ret == null) { String s = this.readString(); if (isStringValidFormat(s)) { ret = s.toCharArray(); }else { System.out.println("Incorrect. Values must be between 'a0 - a8' and 'b0 - b8'"); } } return new char[0]; }`
Advertisement
Answer
First, I would test for null
and that there are two characters in the String
. Then you can use a simple boolean
check to test if first
is a
or b
and the second
is between 0
and 8
inclusive. Like,
private boolean isStringValidFormat(String s) { if (s == null || s.length() != 2) { return false; } char first = s.charAt(0); char second = s.charAt(1); return (first == 'a' || first == 'b') && (second >= '0' && second <= '8'); }