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

Thread: Calculation looks like it should work, but doesn't.

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculation looks like it should work, but doesn't.

    I have a calculation that sorts books by rating...

    System.out.println("\r" + "\n\tSorted by Rating\n");
     
    	 for (int count = 0; count < bookL.size(); count++) {
    		    for (int in = 0; in < bookList.size() - 1; in++) 
    	            if (videoList.get(in).getRating() < bookList.get(in + 1).getRating()) {
    	                Book temp = bookList.get(in);
    	                bookList.set(in, bookList.get(in+1));
    	                bookList.set(in+1, temp);
     
    	            }
     
    		    System.out.println(bookList.get(count).getRating() + "   " + bookList.get(count).getTitle());
     
    	        }

    I was told that I could easily alter the code to sort alphabetically as well.

     for (int count = 0; count < bookList.size(); count++) {
    		    for (int in = 0; in < bookList.size() - 1; in++) 
    		    	if (bookList.get(in).getTitle().compareTo(bookList.get(in + 1).getTitle()) < 0) {
    	                Book temp = bookList.get(in);
    	                bookList.set(in, bookList.get(in+1));
    	                bookList.set(in+1, temp);
     
    	            }
     
    		    System.out.println(bookList.get(count).getRating() + "   " + bookList.get(count).getTitle()+"\t");
     
    	        }

    What am I missing here? Why doesn't this work?


  2. #2
    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: Calculation looks like it should work, but doesn't.

    System.out.println("\r" + "\n\tSorted by Rating\n");
     
    	 for (int count = 0; count < bookL.size(); count++) {
    		    for (int in = 0; in < bookList.size() - 1; in++) 
    	            if (videoList.get(in).getRating() < bookList.get(in + 1).getRating()) {
    	                Book temp = bookList.get(in);
    	                bookList.set(in, bookList.get(in+1));
    	                bookList.set(in+1, temp);
     
    	            }
     
    		    System.out.println(bookList.get(count).getRating() + "   " + bookList.get(count).getTitle());
     
    	        }

    I was told that I could easily alter the code to sort alphabetically as well.

     for (int count = 0; count < bookList.size(); count++) {
    		    for (int in = 0; in < bookList.size() - 1; in++) 
    		    	if (bookList.get(in).getTitle().compareTo(bookList.get(in + 1).getTitle()) < 0) {
    	                Book temp = bookList.get(in);
    	                bookList.set(in, bookList.get(in+1));
    	                bookList.set(in+1, temp);
     
    	            }
     
    		    System.out.println(bookList.get(count).getRating() + "   " + bookList.get(count).getTitle()+"\t");
     
    	        }

    Why doesn't this work?
    Can you post the code's output that shows what you are talking about.
    Also print the contents of the arraylist before the sort and after the sort.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculation looks like it should work, but doesn't.

    It's printing the content in the order it appears in the code.

    --- Update ---

    Do I need a compareTo bit in my Book class. Like with all this stuff?

    String title;
        int rating;
     
        public Video(String pTitle, int pRating) {
        	title = pTitle;
        	rating = pRating;}
     
        public String getTitle() {
            return title;}
     
        public int getRating() {
            return rating;}

    I've been trying to add a compareTo method in here but I don't really know how.
    Last edited by Dave Bowman; June 19th, 2014 at 07:36 PM.

  4. #4
    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: Calculation looks like it should work, but doesn't.

    Please copy what is printed out and paste it here.
    Print the contents of the arrraylist before the sort
    and after the sort.

    Also print out the contents of the arraylist inside of the if statement after each swap is done.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Code doesn't work
    By Nicken99 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2014, 04:24 AM
  2. help in how to add code to work with income tax calculation program
    By jayokayo69 in forum Object Oriented Programming
    Replies: 3
    Last Post: January 21st, 2014, 02:14 PM
  3. Program doesn't work!
    By Mrcinica in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 16th, 2013, 10:23 AM
  4. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 04:25 AM
  5. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 11:19 PM