Problems with If validation
Hi Guys,
Another quick question.
I want to validate the field of inGenre to either Jazz, Blues, Soul
but I cant get the hang of doing an If statement with Strings :confused:
Heres the code
Code :
/**
* A class that maintains information on a CD.
*
* @author (Martyn Weber)
* @version (Version 1)
*/
class CD
{
// The fields.
private Artist performer;
private String album;
private String genre;
private int numberOfTracks;
private int yearReleased;
////Constructors///////////////////////////////////////////////////////////
public CD (String inalbum, String inGenre, int inNumberOfTracks,
int inYearReleased)
{
album = inalbum;
if(inGenre = Soul && inGenre = Jazz && inGenre = Blues){
genre = inGenre;
}
else{
System.out.println
("#Please make sure that the genre is set to either "Soul" Jazz" or "Blues"");
}
if(inNumberOfTracks > 1 ){
numberOfTracks = inNumberOfTracks;
}
else{
System.out.println
("#There must be 1 or more tracks");
}
if(inYearReleased > 1901 && inYearReleased < 2003){
yearReleased = inYearReleased;
}
else{
System.out.println
("#Date of Release Must Be Between 1900 - 2004");
}
}
public CD () // This is a blank *Constructor*
{ // and will take *No* *Arguments*
}
////These are Accessor Methods/////////////////////////////////////////////
public String getAlbum() // This Accessor is used to return
{ // The title of the Album
return album; //
} //
//
public String getGenre() // This Accessor is used to return
{ // The Genre of the Album
return genre; //
} //
//
public int getYear() // This Accessor is used to return
{ // The Year the Album was created
return yearReleased; //
} //
///////////////////////////////////////////////////////////////////////////
////These are Mutator Methods///////////////////////////////////////////////
public void setAlbum(String addAlbum) // This Accessor is used to return
{ // The title of the Album
album = addAlbum; //
} //
//
public void setGenre(String addGenre) // This Accessor is used to return
{ // The Genre of the Album
genre = addGenre; //
} //
//
public void setYear(int addYear) // This Accessor is used to return
{ // The Year the Album was created
yearReleased = addYear; //
} //
///////////////////////////////////////////////////////////////////////////
////This is a printing *Method* to print a ticket to the screen////////////
public void printVoucher()
{
System.out.println("|------------------------------------| ");
System.out.println("|----------Cryptic Tickets-----------| ");
System.out.println("|-----Festival & Concert Bookings----| ");
System.out.println("|-----www.cryptictickets.co.cc-------| ");
System.out.println("|--------------CD Info---------------| ");
System.out.println("|-Album------------------------------| ");
System.out.println("|.........." + album +"");
System.out.println("|-Genre------------------------------| ");
System.out.println("|.........." + genre +"");
System.out.println("|-Tracks-----------------------------| ");
System.out.println("|.........." + numberOfTracks + "");
System.out.println("|-Release-Year-----------------------| ");
System.out.println("|.........." + yearReleased +"");
System.out.println("|------------------------------------| ");
}
}
Thanks in advance
Re: Problems with If validation
Every object in Java have a method called equals. This method is meant to return true if the two objects compared are meaningfully equal. When you create your own objects in the future you can override this method to perform your own comparison.
Its important to know that the == way of comparing two objects does not work for types other than primitives, well it does work on Strings but thats in very special case but you should never count on it.
Here's some sample code.
Code :
String myString = "test";
// This should succeed
if(myString.equalsIgnoreCase("TEST")) {
System.out.println("We have a match");
}
// This should fail
if(myString.equals("TEST")) {
System.out.println("We have a match");
}
// You can even do it the other way around
if("TEST".equalsIgnoreCase(myString)) {
System.out.println("We have a match");
}
Hope that helps.
// Json