Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problems with If validation

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    Heres the 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


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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.

    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

Similar Threads

  1. the problems of psx model ripping
    By wolfgar in forum Java Theory & Questions
    Replies: 4
    Last Post: November 4th, 2009, 07:31 PM
  2. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  3. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM
  4. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM
  5. Problems in setting classpath
    By missyati in forum Java Theory & Questions
    Replies: 3
    Last Post: June 30th, 2009, 12:43 AM