PDA

View Full Version : Issue with Java method


acstache
09-30-2008, 10:04 PM
Hello All,

Was doing some homework tonight and ran into a bit of a snag. The main method has an if/else statement as such:

if (code.getIsValid())
{
System.out.println("Bar Code = "+code.getBarCode());
System.out.println("Zip Code = "+code.getZipCode());
}
else
System.out.println ("Incorrect format");

I've not yet figured out what to put into the method getIsValid(). How would I make the getIsValid method to determine true or false? it's determining whether or not the user input is 'valid' in such that it's a 5 digit number or a 'String' combination of 32 :'s or |'s. I would like to keep the code above as close to what it is currently as possible. and the method as dumbed down as possible, as advanced coding may flag the teacher into knowing I got help :D

Thanks

mjc
09-30-2008, 10:15 PM
YEAH!!!! FINALLY!!!

You, acstache, get the prize...

You probably don't know what the heck I'm going on about, but you are probably the first person to ask for homework help here, by flat out stating you are stumped, that it is homework and that you want help...not hand holding!

Unfortunately, my coding skills are so rusty that I can barely remember how to do a successful 'hello world'...heck, I had to look up how to write a batch file a couple of weeks ago...but, hang on and someone with a bit more recent coding expertise will probably drop in and comment.

acstache
10-01-2008, 12:48 AM
well...it looks like there was more wrong with my code than just that...but that's besides the point. I would still like to know how to do this ^_^

Paul Komski
10-01-2008, 02:31 AM
The glaring omission would seem to be the lack of opening/closing braces after the else statement.

if (code.getIsValid())
{
System.out.println("Bar Code = "+code.getBarCode());
System.out.println("Zip Code = "+code.getZipCode());
}
else
{
System.out.println ("Incorrect format");
}

acstache
10-03-2008, 09:40 PM
But in java you get 1 freebie line before you need the curly bracers. although, I'm usually good about putting them in due to slight OCD. I just didn't want to do it here because the teacher is just as OCD in terms of how much we change the 'main' code. I'm going to also be talking with the teacher about this issue, but input from here would be awesome as well :)

pangea33
10-03-2008, 11:04 PM
I am not a java programmer but I have done a fair amount of coding in my day, and IMHO logic transcends the specific language you're using. It seems like you've either got a zip code with digits, or you've got a bar code with a combination of ":" and "|".

If that's the case I would think that you'd actually want two validation functions so you could determine which output command to execute. IE: getIsValidZipCode() and getIsValidBarCode(). Unless there are multiple user-variable properties in that object. IE: code.zipCode and code.barCode.

It seems that there is probably some useful instantiation code that you didn't include so as not to give us information overload. Regardless of which implementation you're working with, I think you'll need a couple simple regular expressions in your validation method.

Like I said I don't do java, but here is a sample page I did using javascript. Hopefully it will help you on your way. Sorry about the lack of indentation... the text editor killed my formatting.

************************************************** *
<SCRIPT LANGUAGE="JavaScript"><!--
function isValidZip() {
var re = new RegExp("[0-9]{5}");
if (document.getElementById("userInput").value.match(re)) {
alert("Is Valid Zip");
} else {
alert("Not Valid Zip");
}
}

function isValidBarCode() {
var re = new RegExp("[:|]{32}");
if (document.getElementById("userInput").value.match(re)) {
alert("Is Valid Bar Code");
} else {
alert("Not Valid Bar Code");
}
}
// -->
</SCRIPT>
<form>
<input type="text" id="userInput">
<input type="button" value="Go" onclick="isValidZip();isValidBarCode();">
</form>
************************************************** *

zip code: [0-9]{5}
bar code: [:|]{32}
bar code example: "::::::::||||||||::::::::|||||||:"