Skip to main content
Code Review

Return to Answer

Fixed blunder pointed out by @tim
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
public static boolean isNumber(String toTest) {
 if (toTest == null || toTest.isEmpty()) {
 return false;
 }
 ifint dots = 0, digits = 0;
  for (int i = toTest.indexOfstartsWith(".-") !=? 1 : 0; i < toTest.lastIndexOflength("); i++) {
 switch (toTest."charAt(i)) {
 return false; case '0': case '1': case //'2': Morecase than'3': onecase decimal'4':
 point case '5': case '6': case '7': case '8': case '9':
 } digits++;
 break;
 for (int i = toTest.startsWith("-") ? 1case '.': 0; i < toTest.length(); i++) { dots++;
 if ("0123456789.".indexOf(toTest.charAt(i)) < 0) {break;
 default:
  return false; // Illegal character
 }
 }
 return true;dots <= 1 && digits > 0;
}
public static boolean isNumber(String toTest) {
 if (toTest == null || toTest.isEmpty()) {
 return false;
 }
 if (toTest.indexOf(".") != toTest.lastIndexOf(".")) {
 return false; // More than one decimal point
 }
 for (int i = toTest.startsWith("-") ? 1 : 0; i < toTest.length(); i++) {
 if ("0123456789.".indexOf(toTest.charAt(i)) < 0) {
 return false; // Illegal character
 }
 }
 return true;
}
public static boolean isNumber(String toTest) {
 if (toTest == null || toTest.isEmpty()) {
 return false;
 }
 int dots = 0, digits = 0;
  for (int i = toTest.startsWith("-") ? 1 : 0; i < toTest.length(); i++) {
 switch (toTest.charAt(i)) {
 case '0': case '1': case '2': case '3': case '4':
  case '5': case '6': case '7': case '8': case '9':
  digits++;
 break;
 case '.':  dots++;
 break;
 default:
  return false; // Illegal character
 }
 }
 return dots <= 1 && digits > 0;
}
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

As evidenced by the comments, we don't think that you have posted a question that clearly specifies what should and shouldn't be considered a valid number. In addition to Scientific Notation, I'd ask whether "六萬七千" or "MDXVI" counts as a number. How about a European-style "," as a decimal point? How about thousands separators? How about an explicit leading positive sign? Can there be a space between the negative sign and the digits? What about gratuitous leading/trailing whitespace? In an interview, you should engage your interviewer in the same way to seek clarification. (Poke hard enough, and you might even get your interviewer to do part of the work for you!)

I'd also like to know what is considered a parse function. new BigDecimal(toTest) seems like it should be off-limits, but can you use a regular expression matcher? That could reduce the function down to one line.

By inspection, your function is incorrect: an empty string would cause the entire for-loop to be skipped, and isNumber("") would return true.

if (!"0123456789-.".contains(new String(new char[]{toTest.charAt(i)}))) is rather monstrous. A typical way to test that is

if ("0123456789-.".indexOf(toTest.charAt(i)) < 0)

Inside the for-loop, you seem to be obsessed with testing for the negative sign and decimal point. I'd pull those special cases out of the loop.

Keeping to the spirit of a brute-force approach, I'd write something like this:

public static boolean isNumber(String toTest) {
 if (toTest == null || toTest.isEmpty()) {
 return false;
 }
 if (toTest.indexOf(".") != toTest.lastIndexOf(".")) {
 return false; // More than one decimal point
 }
 for (int i = toTest.startsWith("-") ? 1 : 0; i < toTest.length(); i++) {
 if ("0123456789.".indexOf(toTest.charAt(i)) < 0) {
 return false; // Illegal character
 }
 }
 return true;
}

It's not the most efficient solution, but it is short and easy to understand. Those are good attributes to aim for in a stressful interviewing situation.

lang-java

AltStyle によって変換されたページ (->オリジナル) /