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.

Page 4 of 4 FirstFirst ... 234
Results 76 to 96 of 96

Thread: Creating Random unused ID number for every library member.

  1. #76
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    That kind of code is hard to write. I'd use the Calendar class's add() method because it will have been tested and will work correctly.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #77
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by clarky2006 View Post
    d = d + 21;
         if (d > 30)
         {
             d= 1;
             m = m + 1;
         }
         if (m > 12)
         {
            m = 1; 
            y = y + 1;
         }
    this is suppose to add 21 days
    This is a second constructor, it wont compile so i had to comment out sum of the code..
        /**
         * Constructor for objects of class Loan
         */
        public Loan(int bookId, int memberId)
        {              
         int currentDay;
         int currentMonth;
         int currentYear;
         GregorianCalendar calendar = new GregorianCalendar();
         month=String.valueOf(calendar.get(GregorianCalendar.MONTH));            
         day=String.valueOf(calendar.get(GregorianCalendar.DAY_OF_MONTH));
         year=String.valueOf(calendar.get(GregorianCalendar.YEAR));
         loanDate = (day + " / " + month + " / " + year);
         //System.out.println (loanDate);
         int d = Integer.parseInt(day);
         int m = Integer.parseInt(month);
         int y = Integer.parseInt(year);
         m = m + 1; 
         loanDate = (d + " / " + m + " / " + y);
         //System.out.println ("new date - " + date);
         d = d + 21;
         if (d > 30)
         {
             d= 1;
             m = m + 1;
            }
         if (m > 12)
         {
            m = 1; 
            y = y + 1;
            }
         currentMonth = m;
         currentYear = y;
         currentDay = d;
         loanReturnDate = (currentDay + "/" + currentMonth + "/" + currentYear);
     
         this.bookId = bookId;// The book ID is initialised here.
         this.memberId = memberId;// The member's unique ID is initialised here.
         //loanDate = new GregorianCalendar();// When the loan item was taken out.
         //loanDate = new GregorianCalendar();// End of loan date.
         //loanReturnDate.add(Calendar.DATE, 21);// This adds three weeks(21 days) to the loanDate.
         //System.out.println (dateLoanDueBack);
        }

  3. #78
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Sorry, I don't understand your last post.

    You should remove your code that adds 21 and use the add() method.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #79
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Sorry, I don't understand your last post.

    You should remove your code that adds 21 and use the add() method.
    The last post was a second constructor. I dont know how to use the add method..

    --- Update ---

    The second constructor keeps highlighting new GregorianCalendar as an incompatible type..

  5. #80
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    know how to use the add method..
    Look at how you use the get() method and the set() method.

    highlighting new GregorianCalendar as an incompatible type..
    I can't see the highlighting. Can you compile the code with javac and get the error message?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #81
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Look at how you use the get() method and the set() method.


    I can't see the highlighting. Can you compile the code with javac and get the error message?
    Just got my results yesterday mate...Got an A 80% all thanks to you...Cheers for all the help and support.....

  7. #82
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #83
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Glad you got it working.

    Morning Norm..Not sure wot time it is across the pond..Hey got more issues with java methods
    Have a look at this code please. I created this for a car dealership. here im trying to remove a car by
    registration which is a "string". im not sure about using the "==" comparison. should i use ".contains" instead..?

    kind regards


        public void removeCarByRegistration(String carRegistration)
        {
            Iterator<Car> it = cars.iterator();
            while(it.hasNext())
            {
                Car car = it.next();
                if(carRegistration == (car.getRegNumber()))
                {
                    it.remove();
                }
            }     
        }

  9. #84
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    If you want to test for equality use the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #85
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    If you want to test for equality use the equals() method.
    But can I use the equals method when the return type is string?

  11. #86
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    not sure about using the "==" comparison
    Use the equals() method when comparing the contents of objects for equality.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #87
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by clarky2006 View Post
    But can I use the equals method when the return type is string?
    Here is the alternative method im thinkinking of using but not sure...

    public void removeCarByRegistration(String searchString)
        {
            Iterator<Car> it = cars.iterator();
            while(it.hasNext())
            {
                Car car = it.next();
                if(car.getRegNumber().contains (searchString))
                {
                    it.remove();
                }
            }     
        }

  13. #88
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Depends on the programs requirements.
    The String "ASDF" contains the String "AS"
    If you don't understand my answer, don't ignore it, ask a question.

  14. #89
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    They want to be able to remove a car that is sold from the Stock Class by just entering the registration number...

  15. #90
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Does the logic work?
    "AS" is contained in "ASDF"
    "AS" does not equal "ASDF"
    If you don't understand my answer, don't ignore it, ask a question.

  16. #91
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Does the logic work?
    "AS" is contained in "ASDF"
    "AS" does not equal "ASDF"
    I cant really understand that last reply..What is "AS" and "ASDF"? as you know I'm new to java...

    --- Update ---

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    /**
     * Write a description of class Stock here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Stock
    {
     
        // An Arraylist for storing car details.
        private ArrayList<Car> cars;
        private int carId;
     
        /**
         * Constructor for objects of class Stock
         */
        public Stock()
        {
           cars = new ArrayList<Car>();
           carId = 100;
        }
     
        /**
         * Add a new car to the Stock.
         * @param The Car to be added.
         */
        public void addCar(Car car)
        {
           cars.add(car); 
        }
     
     
        /**
         * 
         */
        public void addNewCar(String make, String model, String regNumber, double pricePaid)
        {
            carId++;// The car Id gets incremented by one in sequence when a new object is created.
            cars.add(new Car(make,  model, regNumber, carId, pricePaid));
        }
     
        public void printCarByRegistration(String searchstring)
        {
            for(Car car : cars)
            {
                if(car.getRegNumber().contains (searchstring))
                {
                    car.printDetails();
                }
            }
        }
     
         public void printCarBymake(String carMake)
        {
            for(Car car : cars)
            {
                if(car.getMake() == carMake)
                {
                    car.printDetails();
                }
            }
        }
     
        public void printCarByModel(String carModel)
        {
            for(Car car : cars)
            {
                if(car.getModel() == carModel)
                {
                    car.printDetails();
                }
            }
        }
     
        public void removeCarByRegistration(String searchString)
        {
            Iterator<Car> it = cars.iterator();
            while(it.hasNext())
            {
                Car car = it.next();
                if(car.getRegNumber().contains (searchString))
                {
                    it.remove();
                }
            }     
        }
     
     
     
    }

    Here is the whole code for the Stock Class...Not added the main yet...

  17. #92
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    "AS" and "ASDF" are two Strings I used to show the difference between contains and equals.
    That comparison has nothing to do with java. It was an attempt to discuss the logic of what it means for one String to contain another and for one String to be equal to another.
    The String "ASDF" contains these Strings: "AS" and "SD" and "DF" and "A" and "ASD" etc

    If the String returned by getRegNumber() was "ASDF"
    and searchString was "AS" or "SD" etc then contains would be true.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #93
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    ok..I will use the equals() method then so they have to enter the full registration for the right car to be removed instead of searchstring as searchstring would throw up the wrong cars...Thank you for the lecture this morning..You ve just taught me some proper logic...I love been educated by you..Thanks for sharing your knowledge...I would never get those if logics wrong again..Thank you once again..cheers.

  19. #94
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Cheers
    If you don't understand my answer, don't ignore it, ask a question.

  20. #95
    Member clarky2006's Avatar
    Join Date
    Nov 2012
    Posts
    84
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Creating Random unused ID number for every library member.

    Quote Originally Posted by Norm View Post
    Cheers
    Hi Norm..Just seen my tutor and hes saying that my comparison is wrong on the print carByRegistration method.
    He says I should use the compareto method instead. Never used compare to before.
    can you help please.

    regards

    /**
         * Print a car's details by its registration 
         * @param The registration of the car to be printed.
         */
        public void printCarByRegistration(String registration)
        {
            for(Car car : cars)
            {
                if(car.getRegNumber()== (registration))
                {
                    car.printDetails();
                }
            }
        }

  21. #96
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Random unused ID number for every library member.

    Have you read the API doc for the compareTo method to see what it does?
    Also I suggest that you write a small testing program that uses the compareTo() method with some short Strings and prints out what is returned by the method so you can see what it does.
    System.out.println("D".compareTo("C"));
    and change the letters for different tests.
    If you don't understand my answer, don't ignore it, ask a question.

Page 4 of 4 FirstFirst ... 234

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  4. creating a shared library
    By navinbecse in forum Threads
    Replies: 1
    Last Post: April 9th, 2010, 07:54 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM