Revision 10491

View differences:

trunk/libraries/libIverUtiles/src/com/iver/utiles/stringNumberUtilities/StringNumberUtilities.java
54 54
	 * @return A boolean value
55 55
	 */	
56 56
	public static boolean isNumber(String word) {
57
		return isExponentialRealNumber(word);
57
		return isRealNumberWithIntegerExponent(word);
58 58
	}
59 59
	
60 60
	/**
......
161 161
	
162 162
	
163 163
	/**
164
	 * Returns true if the word is a real number with or without the 'E' (or 'e') symbol for the exponent; else returns false <br>
165
	 * It's supposed that '.' is the symbol for separate integer from decimal part in the base. <br>
166
	 * The exponent must be an integer number
167
	 * 
168
	 * If it's a natural, integer or real number, it's a real number with integer exponent
169
	 *  
170
	 * @param word An string
171
	 * @return A boolean value
172
	 */
173
	public static boolean isRealNumberWithIntegerExponent(String word) {
174
		// Remove all spaces and tabs at beginning and end
175
		word = word.trim();
176
		
177
		int numberOfPoints = 0;
178
		
179
		// If no word
180
		if (word.length() == 0)
181
			return false;
182
	
183
		// Try to remove the sign of the number
184
		if ((word.charAt(0) == '-') || (word.charAt(0) == '+'))
185
			word = word.substring(1, word.length());
186
		
187
		// Analize the word
188
		int i = 0;
189
//		for (int i = 0; i < word.length(); i++) {
190
		while ((i < word.length()) && (Character.toUpperCase(word.charAt(i)) != 'E')) {
191
			switch (word.charAt(i)) {
192
				case '0': case '1': case '2': case '3': case '4':
193
				case '5': case '6': case '7': case '8': case '9':
194
					// do nothing (continue)
195
					break;
196
				case '.':
197
					// If there was another point -> fail
198
					if (numberOfPoints == 1)
199
						return false;
200
					else
201
						numberOfPoints ++;
202
					
203
					break;
204
				default:
205
					return false;
206
			}
207
			
208
			i++;
209
		}
210
		
211
		if (i == word.length())
212
			return true;
213
		
214
		numberOfPoints = 0;
215
		
216
		// Jump the symbol E
217
		i++;
218
		
219
		if (i == word.length())
220
			return false;
221

  
222
		// Try to remove the sign of the number
223
		if ((word.charAt(i) == '-') || (word.charAt(i) == '+')) {
224
			word = word.substring(++i, word.length());
225
			i = 0;
226
		}
227
		
228
		if (word.length() == 0)
229
			return false;
230
		
231
		while (i < word.length()) {
232
			switch (word.charAt(i)) {
233
				case '0': case '1': case '2': case '3': case '4':
234
				case '5': case '6': case '7': case '8': case '9':
235
					// do nothing (continue)
236
					break;
237
				default:
238
					return false;
239
			}
240
			i++;
241
		}
242
		
243
		return true;
244
	}
245
	
246
	
247
	/**
164 248
	 * Returns true if the word is a real number with or without the 'E' (or 'e') symbol for the exponent; else returns false
165 249
	 * It's supposed that '.' is the symbol for separate integer from decimal part, in the base and the exponent of the number
166 250
	 * 
167
	 * If it's a natural, integer or real number, it's an exponential real number
251
	 * If it's a natural, integer, real number or real number with integer exponent, it's a real number with real exponent
168 252
	 *  
169 253
	 * @param word An string
170 254
	 * @return A boolean value
171 255
	 */
172
	public static boolean isExponentialRealNumber(String word) {
256
	public static boolean isRealNumberWithRealExponent(String word) {
173 257
		// Remove all spaces and tabs at beginning and end
174 258
		word = word.trim();
175 259
		
......
219 303
			return false;
220 304

  
221 305
		// Try to remove the sign of the number
222
		if ((word.charAt(i) == '-') || (word.charAt(i) == '+'))
306
		if ((word.charAt(i) == '-') || (word.charAt(i) == '+')) {
223 307
			word = word.substring(++i, word.length());
308
			i = 0;
309
		}
224 310
		
225 311
		if (word.length() == 0)
226 312
			return false;

Also available in: Unified diff