Verify a String is in the right format

String newYear = JOptionPane.showInputDialog(null, "Please enter the year for " + newTitle);

if (newYear == null)&&(newYear ==? )

I need help to make sure the user's input is a valid year. The bolded question marks are where i think i need to put something but i think i need to change the string to a int or something before i can check if it is in the proper format, but i don't know how. If you need more of my script to see whats giong on then please say so

thanx

[509 byte] By [H_Ca] at [2007-11-27 1:56:15]
# 1

Is this what you need?

if (newYear == null && ! isValidYear(newYear)) {

// error

}

........

private boolean isValidYear(String year) {

if year is invalid {

return false;

}

return true;

}

floundera at 2007-7-12 1:30:21 > top of Java-index,Java Essentials,New To Java...
# 2
yep... thanxGuess not... it didn't work. What else do i needMessage was edited by: H_CH_C
H_Ca at 2007-7-12 1:30:21 > top of Java-index,Java Essentials,New To Java...
# 3

//user click Insert New DVD on the Edit menu

if (arg == "Insert")

{

//accept new data

String newTitle = JOptionPane.showInputDialog(null, "Please enter the new movie's title");

if (newTitle == null)

{}//if the cancel button is pressed then it does nothing and does not continue to prompt for info

else//if the cancel button is not pressed then it continues like normal

{

String newStudio = JOptionPane.showInputDialog(null, "Please enter the studio for " + newTitle);

if (newStudio == null)

{}//if the cancel button is pressed then it does nothing and does not continue to prompt for info

else//if the cancel button is not pressed then it continues like normal

{

String newYear = JOptionPane.showInputDialog(null, "Please enter the year for " + newTitle);

if (newYear == null)// && (! isValidYear(newYear))

{}//if the cancel button is pressed then it does nothing and does not continue to prompt for info

else//if the cancel button is not pressed then it continues like normal

{

//enlarge arrays

title = enlargeArray(title);

studio = enlargeArray(studio);

year = enlargeArray(year);

//add new data to arrays

title[title.length-1] = newTitle;

studio[studio.length-1] = newStudio;

year[year.length-1] = newYear;

//call sort method

sort(title);

fieldCombo.setSelectedIndex(0);

}//close of newtitle else

}//close of newstudio else

}//close of newyear else

}//close of if user click Insert New DVD on the Edit menu

//checks if the year is valid in newYear

//user clicks Title on the Search submenu

if (arg == "title")

search(arg, title);

//user clicks Studio on the Search submenu

if (arg == "studio")

search(arg, studio);

//user clicks Year on the Search submenu

if (arg == "year")

search(arg, year);

}

//public boolean isValidYear(String year)

//{

//if (year = !valid)

//{

//return false;

//}

//else

//{

// return true;

// }

//}

This is the part i'm having trouble with. The last part with all the // in front of it was the suggested part but it keeps saying its invalid when using a boolean value

H_Ca at 2007-7-12 1:30:21 > top of Java-index,Java Essentials,New To Java...
# 4
i guess i have to 'bump' this
H_Ca at 2007-7-12 1:30:21 > top of Java-index,Java Essentials,New To Java...
# 5

First, when comparing Strings do not use ==, use the equals method instead.

When writing if statements, it usually is bad to have empty branches.

if(newTitle == null) {

// do nothing

} else {

something;

}

This is better written as:

if(newTitle != null) {

something;

}

No dead end.

floundera at 2007-7-12 1:30:21 > top of Java-index,Java Essentials,New To Java...
# 6
well it does what i want to do and i'm new to java so i don't want to mess with it. Also, that doesn't answer my question but thanx for the bumpStill seeking an answer...
H_Ca at 2007-7-12 1:30:21 > top of Java-index,Java Essentials,New To Java...
# 7
You can validate a typed-in Date by using DateFormat.parse().
TimRyanNZa at 2007-7-12 1:30:21 > top of Java-index,Java Essentials,New To Java...
# 8
How can this be valid syntax? Variable year is a string!if (year = !valid)
DrLaszloJamfa at 2007-7-12 1:30:21 > top of Java-index,Java Essentials,New To Java...
# 9

> How can this be valid syntax? Variable year is a

> string!

> > if (year = !valid)

>

Well they could do

if(year.equals(Boolean.toString(!valid)))

but that probably isn't what they are after.

floundera at 2007-7-12 1:30:21 > top of Java-index,Java Essentials,New To Java...