root / trunk / libraries / libIverUtiles / src / com / iver / utiles / stringNumberUtilities / StringNumberUtilities.java @ 9942
History | View | Annotate | Download (5.64 KB)
1 |
package com.iver.utiles.stringNumberUtilities; |
---|---|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* Generalitat Valenciana
|
23 |
* Conselleria d'Infraestructures i Transport
|
24 |
* Av. Blasco Ib??ez, 50
|
25 |
* 46010 VALENCIA
|
26 |
* SPAIN
|
27 |
*
|
28 |
* +34 963862235
|
29 |
* gvsig@gva.es
|
30 |
* www.gvsig.gva.es
|
31 |
*
|
32 |
* or
|
33 |
*
|
34 |
* IVER T.I. S.A
|
35 |
* Salamanca 50
|
36 |
* 46005 Valencia
|
37 |
* Spain
|
38 |
*
|
39 |
* +34 963163400
|
40 |
* dac@iver.es
|
41 |
*/
|
42 |
|
43 |
/**
|
44 |
* This class has methos for verify if an string is a number
|
45 |
*
|
46 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
47 |
*/
|
48 |
public class StringNumberUtilities { |
49 |
|
50 |
/**
|
51 |
* Returns true if the word is a number; else returns false
|
52 |
*
|
53 |
* @param word An string
|
54 |
* @return A boolean value
|
55 |
*/
|
56 |
public static boolean isNumber(String word) { |
57 |
return (isNaturalNumber(word) || isIntegerNumber(word) || isRealNumber(word));
|
58 |
} |
59 |
|
60 |
/**
|
61 |
* Returns true if the word is a natural number; else returns false
|
62 |
*
|
63 |
* @param word An string
|
64 |
* @return A boolean value
|
65 |
*/
|
66 |
public static boolean isNaturalNumber(String word) { |
67 |
// Remove all spaces and tabs at beginning and end
|
68 |
word = word.trim(); |
69 |
|
70 |
// If no word
|
71 |
if (word.length() == 0) |
72 |
return false; |
73 |
|
74 |
// If first symbol is '+'
|
75 |
if (word.charAt(0) == '+') |
76 |
word = word.substring(1, word.length());
|
77 |
|
78 |
// Analize the word
|
79 |
for (int i = 0; i < word.length(); i++) { |
80 |
switch (word.charAt(i)) {
|
81 |
case '0': case '1': case '2': case '3': case '4': |
82 |
case '5': case '6': case '7': case '8': case '9': |
83 |
// do nothing (continue)
|
84 |
break;
|
85 |
default:
|
86 |
return false; |
87 |
} |
88 |
} |
89 |
|
90 |
return true; |
91 |
} |
92 |
|
93 |
/**
|
94 |
* Returns true if the word is an integer number; else returns false
|
95 |
*
|
96 |
* @param word An string
|
97 |
* @return A boolean value
|
98 |
*/
|
99 |
public static boolean isIntegerNumber(String word) { |
100 |
// Remove all spaces and tabs at beginning and end
|
101 |
word = word.trim(); |
102 |
|
103 |
// If no word
|
104 |
if (word.length() == 0) |
105 |
return false; |
106 |
|
107 |
// Try to extract the natural number
|
108 |
if ((word.charAt(0) == '-') || (word.charAt(0) == '+')) |
109 |
word = word.substring(1, word.length());
|
110 |
|
111 |
return isNaturalNumber(word);
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Returns true if the word is a real number; else returns false
|
116 |
* It's supposed that '.' is the symbol for separate integer from decimal part
|
117 |
*
|
118 |
* @param word An string
|
119 |
* @return A boolean value
|
120 |
*/
|
121 |
public static boolean isRealNumber(String word) { |
122 |
// Remove all spaces and tabs at beginning and end
|
123 |
word = word.trim(); |
124 |
|
125 |
int numberOfPoints = 0; |
126 |
|
127 |
// If no word
|
128 |
if (word.length() == 0) |
129 |
return false; |
130 |
|
131 |
// Try to remove the sign of the number
|
132 |
if ((word.charAt(0) == '-') || (word.charAt(0) == '+')) |
133 |
word = word.substring(1, word.length());
|
134 |
|
135 |
// Analize the word
|
136 |
for (int i = 0; i < word.length(); i++) { |
137 |
switch (word.charAt(i)) {
|
138 |
case '0': case '1': case '2': case '3': case '4': |
139 |
case '5': case '6': case '7': case '8': case '9': |
140 |
// do nothing (continue)
|
141 |
break;
|
142 |
case '.': |
143 |
// If there was another point -> fail
|
144 |
if (numberOfPoints == 1) |
145 |
return false; |
146 |
else
|
147 |
numberOfPoints ++; |
148 |
|
149 |
break;
|
150 |
default:
|
151 |
return false; |
152 |
} |
153 |
} |
154 |
|
155 |
return true; |
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 |
} |
232 |
} |