Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To show what you can do when you split up the function in multiple parts I've created some code. Note that this not constitute a clear (class) design.</p> <p>This code has the advantages that you can actually <em>do</em> something with the values ones you parsed them. It is also useful to give good error messages. If you split up the <code>main</code> method even more, you can create a parser that validates values in real time as well (useful if you want to validate values within a GUI, for instance). </p> <pre><code>public class Numbers { private static final String[] s = new String[]{ "0",".",".0",".01","1.","1.0","1.01", "1 ","1 1","1/2","1/","1/0", "1 1/1", "1 0/0","1 /","1 /0", "1 0/"}; private static final Pattern FRACTION_PATTERN = Pattern.compile("(?:(\\d+)\\s+)?(\\d+)/(\\d+)"); public static void main(String[] args) { int counter = 0; for (String value : s) { counter++; String trimmedValue = value.trim(); if (isNumber(trimmedValue)) { System.out.printf("%d) %s ---- Natural number%n", counter, trimmedValue); continue; } boolean parseDecimal = isProbablyDecimal(trimmedValue); boolean parseFraction = isProbablyFraction(trimmedValue); if (parseDecimal &amp;&amp; parseFraction) { System.out.printf("%d) %s ---- Error (both decimal &amp; fraction)%n", counter, trimmedValue); continue; } if (parseDecimal) { if (!trimmedValue.matches("\\d*[.]\\d+")) { System.out.printf("%d) %s ---- Error (invalid decimal)%n", counter, trimmedValue); continue; } System.out.printf("%d) %s ---- Decimal value%n", counter, trimmedValue); continue; } if (parseFraction) { Matcher m = FRACTION_PATTERN.matcher(trimmedValue); if (!m.matches()) { System.out.printf("%d) %s ---- Error (invalid faction)%n", counter, trimmedValue); continue; } if (Integer.valueOf(m.group(3)) == 0) { System.out.printf("%d) %s ---- Error (division by zero in fraction)%n", counter, trimmedValue); continue; } if (m.group(1) != null) { System.out.printf("%d) %s ---- Mixed fraction%n", counter, trimmedValue); continue; } System.out.printf("%d) %s ---- Fraction%n", counter, trimmedValue); continue; } System.out.printf("%d) %s ---- Error (Cannot make bread out of it)%n", counter, trimmedValue); continue; } } public static boolean isNumber(String value) { return value.matches("\\d+"); } public static boolean isProbablyDecimal(String value) { // note, some countries use comma instead of dot return value.contains("."); } public static boolean isProbablyFraction(String value) { return value.contains("/"); } } </code></pre> <p>The output:</p> <pre class="lang-none prettyprint-override"><code>1) 0 ---- Natural number 2) . ---- Error (invalid decimal) 3) .0 ---- Decimal value 4) .01 ---- Decimal value 5) 1. ---- Error (invalid decimal) 6) 1.0 ---- Decimal value 7) 1.01 ---- Decimal value 8) 1 ---- Natural number 9) 1 1 ---- Error (Cannot make bread out of it) 10) 1/2 ---- Fraction 11) 1/ ---- Error (invalid faction) 12) 1/0 ---- Error (division by zero in fraction) 13) 1 1/1 ---- Mixed fraction 14) 1 0/0 ---- Error (division by zero in fraction) 15) 1 / ---- Error (invalid faction) 16) 1 /0 ---- Error (invalid faction) 17) 1 0/ ---- Error (invalid faction) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload