When dealing with dates it's not enough to simply convert string to numbers.
Suppose that the date entered in your text field is: 12/06/2006
You'll have to extract a string from it:
String dateStr = textField.getText();
Then you could split the string using "/" as separator:
String[] tokens = dateStr.split("/");
But then what?
Of course you should parse each string token to convert it to an integer whith Integer.parseInt().
If any of the token contains non-numeric value than an exception will be thrown.
Unfortunately this is not good enough to validate your date.
There are a lot more tests to be done with each number (day, month, year).
Those have min and max values.
Also there are relations between the max of day and the month, year inputed.
One example:
max day = 28 when month = 2 except when year is leap year then max day = 29 for month = 2.
This is a lot of work!!
The only good way to deal with date parsing is to use a date formatter/parser class.
Probably the easiest to use is java.text.SimpleDateFormat.
This class takes care of all the stuff to validate a date in a string so that you don't have to.
import java.util.*;
import java.text.*;
// Retrieve the date string from the text field.
String dateStr = textField.getText();
// Create a SimpleDateFormat object with a given pattern.
// You can choose any other pattern.
SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy");
// Parse the date string with the SimpleDateFormat object
// to return a date object.
// It throws a ParseException if the date string is invalid
Date date = sdf.parse(dateStr);
// Get the calendar used.
Calendar cal = sdf.getCalendar();
// Set the date (the one parsed) for the calendar.
cal.setTime(date);
// Get the day number.
int day = cal.get( Calendar.DAY_OF_MONTH);
// Get the month number.
int month = cal.get(Calendar.MONTH) + 1;
// Get the year number.
int year = cal.get(Calendar.YEAR);
Regards
2006-12-06 15:23:14
·
answer #1
·
answered by BoyScout 2
·
0⤊
0⤋
Using Integer.parseInt() and NumberFormatException is the best way, as described already. If you are using some sort of delimiter between year, month and date values (e.g., slashes, periods), then you may first want to call String.split() on your string and tokenize it.
For example,
String[] result = "2006/12/05".split("/");
for (int x=0; x
System.out.println(result[x]);
In your case, you would apply the parseInt() call within the loop. You might also check that result.length is 3.
Hope this helps.
2006-12-06 12:13:11
·
answer #2
·
answered by Eric C 1
·
0⤊
0⤋
Well, I was going to suggest something like Float.valueOf(), but I don't think this will handle the case of letters at the end of the string.
So, you'll have to manually check that all characters are numbers. But this is pretty easy. Loop through each character of the string, and see if they are in the range '0' through '9'. Adjust accordingly to handle whitespace and decimal points.
2006-12-06 11:22:30
·
answer #3
·
answered by arbeit 4
·
0⤊
0⤋
You could iterate through the String and check each character with Character.isDigit()
to iterate get the length of string and loop throught it and if you find any nondigit just say that amount must be numeric
a=input from textbox
looptime=size of b
for i=1:i
if (a[i].isDigit()==False){
msg="Is not a number";
}}
later on display the message
You can better adjust the syntax as I have been out of this for quite a long.
2006-12-06 11:22:57
·
answer #4
·
answered by Nomee 2
·
0⤊
0⤋
Uhm what you want to do is test the values, the date isnt strictly numeric. Parse the first to characters make sure the value isnt greater the 12, make the sure the third character is a '/' if thats the format you want to use (12/06/06).
int month = new Integer( textfield.getText().subStr(0,1));
if (month > 12)
throw error;
2006-12-06 11:29:26
·
answer #5
·
answered by sean_mccully 3
·
0⤊
0⤋
Here's an example:
String s = "14532";
String hasText = "43a43";
int i1, i2;
try {
i1 = Integer.parseInt( s );
i2 = Integer.parseInt( hasText );
}
catch (NumberFormatException e )
{
System.out.println( e.getMessage() );
}
What will happen here is that the first parseInt line will function just fine, but the 2nd line will generate the NumberFormatException, since it contains non-numeric characters (the 'a').
Instead of my System.out.println, you could generate a popup, or whatever, and return from the function (or continue, again, whatever).
Hope this helps.
2006-12-06 11:23:11
·
answer #6
·
answered by TankAnswer 4
·
0⤊
0⤋
you've declared entire contained in the for loop subsequently it truly is community to it and won't be able to be accessed out of it the perfect function is public static int truth(int x) { int entire=a million; for(int y=a million;y<=x;y++) entire*=y; go back entire; }
2016-11-24 19:49:01
·
answer #7
·
answered by sprang 4
·
0⤊
0⤋
check out http://www.pscode.com for examples...I don't do Java, but I use the site all the time
2006-12-06 11:12:19
·
answer #8
·
answered by Richard H 7
·
0⤊
0⤋