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 6 of 6

Thread: Sorting Array & Binary Search Help

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting Array & Binary Search Help

    I am trying to sort my array of DVDs without using a sorting algorithm. This works with 4 or so DVDs but displays error java.lang.ArrayIndexOutOfBoundsException: 4 with anymore added.

    I don't know why it is doing this.

    My other problem is implementing a binary search.

    import java.text.NumberFormat;
     
     
    public class DVD 
    {
       public String title, director;
       private int year;
       private double cost;
       private boolean bluRay;
     
     
     
       public DVD (String title, String director, int year, double cost,
          boolean bluRay)
       {
          this.title = title;
          this.director = director;
          this.year = year;
          this.cost = cost;
          this.bluRay = bluRay;
       }
     
       public String getTitle(){
           return title;
        }
     
     
     
     
       public String toString()
       {
          NumberFormat fmt = NumberFormat.getCurrencyInstance();
     
          String description;
     
          description = fmt.format(cost) + "\t" + year + "\t";
          description += title + "\t" + director;
     
          if (bluRay)
             description += "\t" + "Blu-Ray";
     
          return description;
       }
    }

    import java.text.NumberFormat;
     
     
    public class DVDCollection 
    {
       private DVD[] collection;
       private int count;
       private double totalCost;
       private double average;
     
       public DVDCollection ()
       {
          collection = new DVD[0];
          count = 0;
          totalCost = 0.0;
     
        }
     
     
       public void addDVD (String title, String director, int year, double cost, boolean blueray)
       {
     
             DVD[] temp = new DVD[collection.length + 1];
             int index = 0;
             int i = 0;
             for(;  0 < collection.length; i++) 
                  {
                 if(collection[i].getTitle().compareTo(title)>0) {
                     temp[index++] = new DVD (title, director, year, cost, blueray);
                     break;
                    }
     
                  temp[index++] = collection[i];
     
               }
     
                for(; i < collection.length; i++){
                      temp[index++] = collection[i];
                     }
           if(index != temp.length)
                 temp[index] = new DVD (title, director, year, cost, blueray);
     
             collection = temp;
           totalCost += cost;
          average=totalCost/collection.length;
        }
     
       public String toString()
       {
          NumberFormat fmt = NumberFormat.getCurrencyInstance();
     
          String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
          report += "My DVD Collection\n\n";
     
          report += "Number of DVDs: " + collection.length + "\n";
          report += "Total cost: " + fmt.format(totalCost) + "\n";
          report += "Average cost: " + average;
     
          report += "\n\nDVD List:\n\n";
     
          for (int i = 0; i < collection.length; i++)
             report += collection[i] + "\n";
     
          return report;
       }
     
     
       private void increaseSize ()
       {
          DVD[] temp = new DVD[collection.length * 2];
     
          for (int dvd = 0; dvd < collection.length; dvd++)
             temp[dvd] = collection[dvd];
     
          collection = temp;
       }
    }

     
    public class Movies
    {
       //-----------------------------------------------------------------
       //  Creates a DVDCollection object and adds some DVDs to it. Prints
       //  reports on the status of the collection.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          DVDCollection movies = new DVDCollection();
     
          movies.addDVD ("The Godfather", "Francis Ford Coppala", 1972, 24.95, true);
          movies.addDVD ("District 9", "Neill Blomkamp", 2009, 19.95, false);
          movies.addDVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
          movies.addDVD ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
          movies.addDVD ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
     
          System.out.println (movies);
     
          movies.addDVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
          movies.addDVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false);
     
          System.out.println (movies);
       }
    }

    public static Comparable searchForDVD (Comparable[] collection, Comparable target)
       {
          int min=0, max=collection.length-1, mid=0;
          boolean found = false;
     
          while (!found && min <= max)
          {
              mid = (min+max) / 2;
              if (collection[mid].compareTo(target) == 0)
                found = true;
              else
              if (target.compareTo(collection[mid]) < 0)
                max = mid-1;
              else
                min = mid+1;
           }
              if (found)
                return collection[mid];
              else
                return -1;
       }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,284
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Sorting Array & Binary Search Help

    Explain this:

            int index = 0;
             int i = 0;
             for(;  0 < collection.length; i++) 
                  {
                 if(collection[i].getTitle().compareTo(title)>0) {
                     temp[index++] = new DVD (title, director, year, cost, blueray);
                     break;
                    }
     
                  temp[index++] = collection[i];
     
               }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting Array & Binary Search Help

    I see what you're saying...how can I sort it without sorting...if I swap values it is sorting...

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting Array & Binary Search Help

    Quote Originally Posted by xJavaLover View Post
    I see what you're saying...how can I sort it without sorting...if I swap values it is sorting...
    Anyone wanna help me figure out how to sort this array of collection objects?

    I am trying to sort it using a loop. I currently recieve a null exception.

    public void addDVD (String title, String director, int year,
          double cost, boolean bluRay)
       {
           if (count == collection.length)
                increaseSize();
     
           DVD[] temp = new DVD[collection.length];
           for (int i = 0; i < collection.length - 1; i++)
           {
               for (int j = i; j < collection.length; j++)
               {
                   if (collection[i].getTitle().compareTo(collection[j].getTitle())>0)
                    temp[i] = new DVD(title, director, year, cost, bluRay);
                    temp[i] = collection[i];
                    collection[i] = collection[j];
                    collection[j] = temp[i];
               }
           }   
     
           collection[count] = new DVD (title, director, year, cost, bluRay);
           totalCost += cost;
           count++;
       }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,136
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Sorting Array & Binary Search Help

    currently recieve a null exception.
    Copy the full text of the error message and paste it here.

    Look at the line where the NPE happened, find the null variable and backtrack in the code to see why the variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting Array & Binary Search Help

    if (collection[i].getTitle().compareTo(collection[j].getTitle())>0)

    is where the null error is occuring.

Similar Threads

  1. Code for Linear & Binary Search using ArrayLists
    By JavaBeginner12 in forum Java Theory & Questions
    Replies: 2
    Last Post: December 11th, 2012, 08:22 AM
  2. Binary Search for an array of random ints - Can't find the target value
    By mju516 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2012, 10:25 AM
  3. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM