Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / stringNumberUtilities / StringNumberUtilities.java @ 40561

History | View | Annotate | Download (9.23 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.stringNumberUtilities;
25
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
26
 *
27
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
28
 *
29
 * This program is free software; you can redistribute it and/or
30
 * modify it under the terms of the GNU General Public License
31
 * as published by the Free Software Foundation; either version 2
32
 * of the License, or (at your option) any later version.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
 * GNU General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU General Public License
40
 * along with this program; if not, write to the Free Software
41
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
42
 *
43
 * For more information, contact:
44
 *
45
 *  Generalitat Valenciana
46
 *   Conselleria d'Infraestructures i Transport
47
 *   Av. Blasco Ib??ez, 50
48
 *   46010 VALENCIA
49
 *   SPAIN
50
 *
51
 *      +34 963862235
52
 *   gvsig@gva.es
53
 *      www.gvsig.gva.es
54
 *
55
 *    or
56
 *
57
 *   IVER T.I. S.A
58
 *   Salamanca 50
59
 *   46005 Valencia
60
 *   Spain
61
 *
62
 *   +34 963163400
63
 *   dac@iver.es
64
 */
65

    
66
/**
67
 * This class has methods for verify if an string is a number, and which kind of number.
68
 * 
69
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
70
 */
71
public class StringNumberUtilities {
72
        
73
        /**
74
         * Returns true if the word is a number; else returns false
75
         * 
76
         * @param word An string
77
         * @return A boolean value
78
         */        
79
        public static boolean isNumber(String word) {
80
                return isRealNumberWithRealExponent(word);
81
        }
82
        
83
        /**
84
         * Returns true if the word is a natural number; else returns false
85
         * 
86
         * @param word An string
87
         * @return A boolean value
88
         */
89
        public static boolean isNaturalNumber(String word) {
90
                // Remove all spaces and tabs at beginning and end
91
                word = word.trim();
92
                
93
                // If no word
94
                if (word.length() == 0)
95
                        return false;
96

    
97
                // If first symbol is '+'
98
                if (word.charAt(0) == '+')
99
                        word = word.substring(1, word.length());
100
                
101
                if (word.length() == 0)
102
                        return false;
103
                
104
                // Analyse the word
105
                for (int i = 0; i < word.length(); i++) {
106
                        switch (word.charAt(i)) {
107
                                case '0': case '1': case '2': case '3': case '4':
108
                                case '5': case '6': case '7': case '8': case '9':
109
                                        // do nothing (continue)
110
                                        break;
111
                                default:
112
                                        return false;
113
                        }
114
                }
115
                
116
                return true;
117
        }
118

    
119
        /**
120
         * Returns true if the word is an integer number; else returns false
121
         * 
122
         * If it's a natural number, it's an integer number
123
         * 
124
         * @param word An string
125
         * @return A boolean value
126
         */
127
        public static boolean isIntegerNumber(String word) {
128
                // Remove all spaces and tabs at beginning and end
129
                word = word.trim();
130
                
131
                // If no word
132
                if (word.length() == 0)
133
                        return false;
134

    
135
                // Try to extract the natural number
136
                if ((word.charAt(0) == '-') || (word.charAt(0) == '+'))
137
                        word = word.substring(1, word.length());
138
                
139
                return isNaturalNumber(word);
140
        }
141
        
142
        /**
143
         * Returns true if the word is a real number; else returns false
144
         * It's supposed that '.' is the symbol for separate integer from decimal part
145
         *
146
         * If it's a natural or integer, it's a real number
147
         * 
148
         * @param word An string
149
         * @return A boolean value
150
         */
151
        public static boolean isRealNumber(String word) {
152
                // Remove all spaces and tabs at beginning and end
153
                word = word.trim();
154
                
155
                int numberOfPoints = 0;
156
                
157
                // If no word
158
                if (word.length() == 0)
159
                        return false;
160
        
161
                // Try to remove the sign of the number
162
                if ((word.charAt(0) == '-') || (word.charAt(0) == '+'))
163
                        word = word.substring(1, word.length());
164
                
165
                if (word.length() == 0)
166
                        return false;
167
                
168
                // Analize the word
169
                for (int i = 0; i < word.length(); i++) {
170
                        switch (word.charAt(i)) {
171
                                case '0': case '1': case '2': case '3': case '4':
172
                                case '5': case '6': case '7': case '8': case '9':
173
                                        // do nothing (continue)
174
                                        break;
175
                                case '.':
176
                                        // If there was another point -> fail
177
                                        if (numberOfPoints == 1)
178
                                                return false;
179
                                        else
180
                                                numberOfPoints ++;
181
                                        
182
                                        break;
183
                                default:
184
                                        return false;
185
                        }
186
                }
187
                
188
                return true;
189
        }
190
        
191
        
192
        /**
193
         * Returns true if the word is a real number with or without the 'E' (or 'e') symbol for the exponent; else returns false <br>
194
         * It's supposed that '.' is the symbol for separate integer from decimal part in the base. <br>
195
         * The exponent must be an integer number
196
         * 
197
         * If it's a natural, integer or real number, it's a real number with integer exponent
198
         *  
199
         * @param word An string
200
         * @return A boolean value
201
         */
202
        public static boolean isRealNumberWithIntegerExponent(String word) {
203
                // Remove all spaces and tabs at beginning and end
204
                word = word.trim();
205
                
206
                int numberOfPoints = 0;
207
                
208
                // If no word
209
                if (word.length() == 0)
210
                        return false;
211
        
212
                // Try to remove the sign of the number
213
                if ((word.charAt(0) == '-') || (word.charAt(0) == '+'))
214
                        word = word.substring(1, word.length());
215

    
216
                if (word.length() == 0)
217
                        return false;
218
                
219
                // Analize the word
220
                int i = 0;
221
//                for (int i = 0; i < word.length(); i++) {
222
                while ((i < word.length()) && (Character.toUpperCase(word.charAt(i)) != 'E')) {
223
                        switch (word.charAt(i)) {
224
                                case '0': case '1': case '2': case '3': case '4':
225
                                case '5': case '6': case '7': case '8': case '9':
226
                                        // do nothing (continue)
227
                                        break;
228
                                case '.':
229
                                        // If there was another point -> fail
230
                                        if (numberOfPoints == 1)
231
                                                return false;
232
                                        else
233
                                                numberOfPoints ++;
234
                                        
235
                                        break;
236
                                default:
237
                                        return false;
238
                        }
239
                        
240
                        i++;
241
                }
242
                
243
                if (i == word.length())
244
                        return true;
245
                
246
                numberOfPoints = 0;
247
                
248
                // Jump the symbol E
249
                i++;
250
                
251
                if (i == word.length())
252
                        return false;
253

    
254
                // Try to remove the sign of the number
255
                if ((word.charAt(i) == '-') || (word.charAt(i) == '+')) {
256
                        word = word.substring(++i, word.length());
257
                        i = 0;
258
                }
259
                
260
                if (word.length() == 0)
261
                        return false;
262
                
263
                while (i < word.length()) {
264
                        switch (word.charAt(i)) {
265
                                case '0': case '1': case '2': case '3': case '4':
266
                                case '5': case '6': case '7': case '8': case '9':
267
                                        // do nothing (continue)
268
                                        break;
269
                                default:
270
                                        return false;
271
                        }
272
                        i++;
273
                }
274
                
275
                return true;
276
        }
277
        
278
        
279
        /**
280
         * Returns true if the word is a real number with or without the 'E' (or 'e') symbol for the exponent; else returns false
281
         * It's supposed that '.' is the symbol for separate integer from decimal part, in the base and the exponent of the number
282
         * 
283
         * If it's a natural, integer, real number or real number with integer exponent, it's a real number with real exponent
284
         *  
285
         * @param word An string
286
         * @return A boolean value
287
         */
288
        public static boolean isRealNumberWithRealExponent(String word) {
289
                // Remove all spaces and tabs at beginning and end
290
                word = word.trim();
291
                
292
                int numberOfPoints = 0;
293
                
294
                // If no word
295
                if (word.length() == 0)
296
                        return false;
297
        
298
                // Try to remove the sign of the number
299
                if ((word.charAt(0) == '-') || (word.charAt(0) == '+'))
300
                        word = word.substring(1, word.length());
301
                
302
                if (word.length() == 0)
303
                        return false;
304
                
305
                // Analize the word
306
                int i = 0;
307
//                for (int i = 0; i < word.length(); i++) {
308
                while ((i < word.length()) && (Character.toUpperCase(word.charAt(i)) != 'E')) {
309
                        switch (word.charAt(i)) {
310
                                case '0': case '1': case '2': case '3': case '4':
311
                                case '5': case '6': case '7': case '8': case '9':
312
                                        // do nothing (continue)
313
                                        break;
314
                                case '.':
315
                                        // If there was another point -> fail
316
                                        if (numberOfPoints == 1)
317
                                                return false;
318
                                        else
319
                                                numberOfPoints ++;
320
                                        
321
                                        break;
322
                                default:
323
                                        return false;
324
                        }
325
                        
326
                        i++;
327
                }
328
                
329
                if (i == word.length())
330
                        return true;
331
                
332
                numberOfPoints = 0;
333
                
334
                // Jump the symbol E
335
                i++;
336
                
337
                if (i == word.length())
338
                        return false;
339

    
340
                // Try to remove the sign of the number
341
                if ((word.charAt(i) == '-') || (word.charAt(i) == '+')) {
342
                        word = word.substring(++i, word.length());
343
                        i = 0;
344
                }
345
                
346
                if (word.length() == 0)
347
                        return false;
348
                
349
                while (i < word.length()) {
350
                        switch (word.charAt(i)) {
351
                                case '0': case '1': case '2': case '3': case '4':
352
                                case '5': case '6': case '7': case '8': case '9':
353
                                        // do nothing (continue)
354
                                        break;
355
                                case '.':
356
                                        // If there was another point -> fail
357
                                        if (numberOfPoints == 1)
358
                                                return false;
359
                                        else
360
                                                numberOfPoints ++;
361
                                        
362
                                        break;
363
                                default:
364
                                        return false;
365
                        }
366
                        i++;
367
                }
368
                
369
                return true;
370
        }
371
}