Revision 9942
trunk/libraries/libIverUtiles/src/com/iver/utiles/stringNumberUtilities/StringNumberUtilities.java | ||
---|---|---|
110 | 110 |
|
111 | 111 |
return isNaturalNumber(word); |
112 | 112 |
} |
113 |
|
|
114 | 113 |
|
115 | 114 |
/** |
116 | 115 |
* Returns true if the word is a real number; else returns false |
... | ... | |
155 | 154 |
|
156 | 155 |
return true; |
157 | 156 |
} |
157 |
|
|
158 |
|
|
159 |
/** |
|
160 |
* Returns true if the word is a real number; else returns false |
|
161 |
* It's supposed that '.' is the symbol for separate integer from decimal part |
|
162 |
* |
|
163 |
* @param word An string |
|
164 |
* @return A boolean value |
|
165 |
*/ |
|
166 |
public static boolean isExponentialRealNumber(String word) { |
|
167 |
// Remove all spaces and tabs at beginning and end |
|
168 |
word = word.trim(); |
|
169 |
|
|
170 |
int numberOfPoints = 0; |
|
171 |
|
|
172 |
// If no word |
|
173 |
if (word.length() == 0) |
|
174 |
return false; |
|
175 |
|
|
176 |
// Try to remove the sign of the number |
|
177 |
if ((word.charAt(0) == '-') || (word.charAt(0) == '+')) |
|
178 |
word = word.substring(1, word.length()); |
|
179 |
|
|
180 |
// Analize the word |
|
181 |
int i = 0; |
|
182 |
// for (int i = 0; i < word.length(); i++) { |
|
183 |
while ((Character.toUpperCase(word.charAt(i)) != 'E') && (i < word.length())) { |
|
184 |
switch (word.charAt(i)) { |
|
185 |
case '0': case '1': case '2': case '3': case '4': |
|
186 |
case '5': case '6': case '7': case '8': case '9': |
|
187 |
// do nothing (continue) |
|
188 |
break; |
|
189 |
case '.': |
|
190 |
// If there was another point -> fail |
|
191 |
if (numberOfPoints == 1) |
|
192 |
return false; |
|
193 |
else |
|
194 |
numberOfPoints ++; |
|
195 |
|
|
196 |
break; |
|
197 |
default: |
|
198 |
return false; |
|
199 |
} |
|
200 |
|
|
201 |
i++; |
|
202 |
} |
|
203 |
|
|
204 |
numberOfPoints = 0; |
|
205 |
|
|
206 |
// Try to remove the sign of the number |
|
207 |
if ((word.charAt(i) == '-') || (word.charAt(i) == '+')) |
|
208 |
word = word.substring(i, word.length()); |
|
209 |
|
|
210 |
while (i < word.length()) { |
|
211 |
switch (word.charAt(i)) { |
|
212 |
case '0': case '1': case '2': case '3': case '4': |
|
213 |
case '5': case '6': case '7': case '8': case '9': |
|
214 |
// do nothing (continue) |
|
215 |
break; |
|
216 |
case '.': |
|
217 |
// If there was another point -> fail |
|
218 |
if (numberOfPoints == 1) |
|
219 |
return false; |
|
220 |
else |
|
221 |
numberOfPoints ++; |
|
222 |
|
|
223 |
break; |
|
224 |
default: |
|
225 |
return false; |
|
226 |
} |
|
227 |
i++; |
|
228 |
} |
|
229 |
|
|
230 |
return true; |
|
231 |
} |
|
158 | 232 |
} |
trunk/libraries/libIverUtiles/src-test/com/iver/utiles/stringNumberUtilities/TestStringNumberUtilities.java | ||
---|---|---|
455 | 455 |
} |
456 | 456 |
|
457 | 457 |
/** |
458 |
* A test |
|
459 |
*/ |
|
460 |
public void test25() { |
|
461 |
word = "+.6"; |
|
462 |
ShowText.showIsExponentialRealText(word); |
|
463 |
|
|
464 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
465 |
ShowText.showYes(); |
|
466 |
} |
|
467 |
else { |
|
468 |
ShowText.showNo(); |
|
469 |
fail(); |
|
470 |
} |
|
471 |
} |
|
472 |
|
|
473 |
|
|
474 |
/** |
|
475 |
* A test |
|
476 |
*/ |
|
477 |
public void test26() { |
|
478 |
word = ".6"; |
|
479 |
ShowText.showIsExponentialRealText(word); |
|
480 |
|
|
481 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
482 |
ShowText.showYes(); |
|
483 |
} |
|
484 |
else { |
|
485 |
ShowText.showNo(); |
|
486 |
fail(); |
|
487 |
} |
|
488 |
} |
|
489 |
|
|
490 |
|
|
491 |
/** |
|
492 |
* A test |
|
493 |
*/ |
|
494 |
public void test27() { |
|
495 |
word = "-.6"; |
|
496 |
ShowText.showIsExponentialRealText(word); |
|
497 |
|
|
498 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
499 |
ShowText.showYes(); |
|
500 |
} |
|
501 |
else { |
|
502 |
ShowText.showNo(); |
|
503 |
fail(); |
|
504 |
} |
|
505 |
} |
|
506 |
|
|
507 |
|
|
508 |
/** |
|
509 |
* A test |
|
510 |
*/ |
|
511 |
public void test28() { |
|
512 |
word = "+6"; |
|
513 |
ShowText.showIsExponentialRealText(word); |
|
514 |
|
|
515 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
516 |
ShowText.showYes(); |
|
517 |
} |
|
518 |
else { |
|
519 |
ShowText.showNo(); |
|
520 |
fail(); |
|
521 |
} |
|
522 |
} |
|
523 |
|
|
524 |
|
|
525 |
/** |
|
526 |
* A test |
|
527 |
*/ |
|
528 |
public void test29() { |
|
529 |
word = "-6"; |
|
530 |
ShowText.showIsExponentialRealText(word); |
|
531 |
|
|
532 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
533 |
ShowText.showYes(); |
|
534 |
} |
|
535 |
else { |
|
536 |
ShowText.showNo(); |
|
537 |
fail(); |
|
538 |
} |
|
539 |
} |
|
540 |
|
|
541 |
|
|
542 |
/** |
|
543 |
* A test |
|
544 |
*/ |
|
545 |
public void test30() { |
|
546 |
word = "6"; |
|
547 |
ShowText.showIsExponentialRealText(word); |
|
548 |
|
|
549 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
550 |
ShowText.showYes(); |
|
551 |
} |
|
552 |
else { |
|
553 |
ShowText.showNo(); |
|
554 |
fail(); |
|
555 |
} |
|
556 |
} |
|
557 |
|
|
558 |
/** |
|
559 |
* A test |
|
560 |
*/ |
|
561 |
public void test31() { |
|
562 |
word = "136.13"; |
|
563 |
ShowText.showIsExponentialRealText(word); |
|
564 |
|
|
565 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
566 |
ShowText.showYes(); |
|
567 |
} |
|
568 |
else { |
|
569 |
ShowText.showNo(); |
|
570 |
fail(); |
|
571 |
} |
|
572 |
} |
|
573 |
|
|
574 |
|
|
575 |
/** |
|
576 |
* A test |
|
577 |
*/ |
|
578 |
public void test32() { |
|
579 |
word = "+13.42"; |
|
580 |
ShowText.showIsExponentialRealText(word); |
|
581 |
|
|
582 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
583 |
ShowText.showYes(); |
|
584 |
} |
|
585 |
else { |
|
586 |
ShowText.showNo(); |
|
587 |
fail(); |
|
588 |
} |
|
589 |
} |
|
590 |
|
|
591 |
/** |
|
592 |
* A test |
|
593 |
*/ |
|
594 |
public void test33() { |
|
595 |
word = "-246.24"; |
|
596 |
ShowText.showIsExponentialRealText(word); |
|
597 |
|
|
598 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
599 |
ShowText.showYes(); |
|
600 |
} |
|
601 |
else { |
|
602 |
ShowText.showNo(); |
|
603 |
fail(); |
|
604 |
} |
|
605 |
} |
|
606 |
|
|
607 |
/** |
|
608 |
* A test |
|
609 |
*/ |
|
610 |
public void test34() { |
|
611 |
word = "146.134E+13"; |
|
612 |
ShowText.showIsExponentialRealText(word); |
|
613 |
|
|
614 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
615 |
ShowText.showYes(); |
|
616 |
} |
|
617 |
else { |
|
618 |
ShowText.showNo(); |
|
619 |
fail(); |
|
620 |
} |
|
621 |
} |
|
622 |
|
|
623 |
/** |
|
624 |
* A test |
|
625 |
*/ |
|
626 |
public void test35() { |
|
627 |
word = "614.425E-67"; |
|
628 |
ShowText.showIsExponentialRealText(word); |
|
629 |
|
|
630 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
631 |
ShowText.showYes(); |
|
632 |
} |
|
633 |
else { |
|
634 |
ShowText.showNo(); |
|
635 |
fail(); |
|
636 |
} |
|
637 |
} |
|
638 |
|
|
639 |
/** |
|
640 |
* A test |
|
641 |
*/ |
|
642 |
public void test36() { |
|
643 |
word = "-8945.1201e48.141"; |
|
644 |
ShowText.showIsExponentialRealText(word); |
|
645 |
|
|
646 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
647 |
ShowText.showYes(); |
|
648 |
} |
|
649 |
else { |
|
650 |
ShowText.showNo(); |
|
651 |
fail(); |
|
652 |
} |
|
653 |
} |
|
654 |
|
|
655 |
/** |
|
656 |
* A test (Incorrect) |
|
657 |
*/ |
|
658 |
public void test37() { |
|
659 |
word = "-8945.1201e48.141."; |
|
660 |
ShowText.showIsExponentialRealText(word); |
|
661 |
|
|
662 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
663 |
ShowText.showYes(); |
|
664 |
} |
|
665 |
else { |
|
666 |
ShowText.showNo(); |
|
667 |
fail(); |
|
668 |
} |
|
669 |
} |
|
670 |
|
|
671 |
/** |
|
672 |
* A test (Incorrect) |
|
673 |
*/ |
|
674 |
public void test38() { |
|
675 |
word = "8945.1201ee48.141"; |
|
676 |
ShowText.showIsExponentialRealText(word); |
|
677 |
|
|
678 |
if (StringNumberUtilities.isExponentialRealNumber(word)) { |
|
679 |
ShowText.showYes(); |
|
680 |
} |
|
681 |
else { |
|
682 |
ShowText.showNo(); |
|
683 |
fail(); |
|
684 |
} |
|
685 |
} |
|
686 |
|
|
687 |
/** |
|
458 | 688 |
* Shows a text sentence |
459 | 689 |
* |
460 | 690 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com) |
... | ... | |
497 | 727 |
} |
498 | 728 |
|
499 | 729 |
/** |
730 |
* Shows the text: Is exponential rational number? |
|
731 |
* |
|
732 |
* @param word An String |
|
733 |
*/ |
|
734 |
public static void showIsExponentialRealText(String word) { |
|
735 |
System.out.print("? Es n?mero real exponencial \'" + word + "\' ? "); |
|
736 |
} |
|
737 |
|
|
738 |
/** |
|
500 | 739 |
* Shows the text: Yes |
501 | 740 |
* |
502 | 741 |
* @param word An String |
Also available in: Unified diff