11 Replies - 475 Views - Last Post: 05 October 2013 - 01:28 PM Rate Topic: -----

#1 KarenByk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 13

User entered values for arrays

Posted 05 October 2013 - 10:42 AM

I need to create 2 classes - one for setters and getters and another to instantiate 5 objects from the first class and prompt the user for values for 3 fields - title, author, and number of pages. Then ask the user to choose which field they want to sort the data by and display the list.
set/get methods :
public class LibraryBook{
   private String title;
   private String author;
   private int pages;
   
   public LibraryBook(String title, String author, int pages)   {
      this.title = title;
      this.author = author;
      this.pages = pages;
   }
   public String getTitle()
      { return title; }
   public void setTitle(String title)
      { this.title = title; }
   public String getAuthor()
      { return author; }
   public void setAuthor(String Author)
      { this.author = author; }
   public int getPages()
      { return pages; }
   public void setPages(int pages)
      { this.pages = pages; }
} 


The second class is where the problems lie:
import java.util.*;
import javax.swing.*;
public class LibraryBookSort
{
   public static void main(String[] args)
   {  String title, author, sortChoice;
      int pages, x;     
      LibraryBook[] books = new LibraryBook[2];
      Scanner inputDevice = new Scanner(System.in);
      for(x = 0; x < books.length; ++x)
      {
         System.out.print("Enter a book title.\n");
         title = inputDevice.next();
         
         System.out.print("Enter the book's author.\n");
         author = inputDevice.next();
         
         System.out.print("Enter the number of pages.\n");
         pages = inputDevice.nextInt();  
         
         books[x] = new LibraryBook(title, author, pages);
      }
      System.out.print("Which field would you like to sort by: title, author, or pages?\n");
         sortChoice = inputDevice.next();
      
      //sort by field decided by user and print

      for(x = 0; x < books.length; ++x)
      {
           System.out.print(books[x].getTitle()+ " " + books[x].getAuthor() + " " + books[x].getPages() + "\n");
      }
   }
} 


Problem 1 is if either string (title or author) is more than one word it will apply the second word to the next field. ie: if you enter Java Programming for the title it reads it as title is Java and Programming is the author.

Problem 2 how would you recommend going about sorting by what the user chose? I don't think bubble sort is the most efficient or appropriate since I do not know which field the user will want to sort by. However, I don't think Arrays.sort() will work either.

Is This A Good Question/Topic? 0
  • +

Replies To: User entered values for arrays

#2 manofax  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 20
  • Joined: 17-May 13

Re: User entered values for arrays

Posted 05 October 2013 - 10:53 AM

Hi there,

As for your first problem, this can easily be solved by replacing your inputDevice.next() call with a inputDevice.nextLine() call...This will ensure that you obtain everything from the user, including the whitespaces. If you use inputDevice.next(), it using the whitespace as a delimiter and this is why your sentence is being broken up into multiple strings.

How are you intending to display the information once the user decides on a sorting preference?
Was This Post Helpful? 1
  • +
  • -

#3 KarenByk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 13

Re: User entered values for arrays

Posted 05 October 2013 - 11:00 AM

Probably as a grid/table
Title Author Pages
Book One Auth One 1
Two Book Author Two 2
Was This Post Helpful? 0
  • +
  • -

#4 manofax  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 20
  • Joined: 17-May 13

Re: User entered values for arrays

Posted 05 October 2013 - 11:08 AM

I would personally just do a switch statement and sort accordingly, because regardless of their choice, you would still need to choose how you would prefer to sort the other 2 items. i.e. if i chose to sort by books, how would you give the other 2 fields preference?

switch (choice)
case 1: //sort by books;
break;
case 2: //sort by author;
break;
case 3: //sort by pages
break;


I'd prefer someone else gave you advice on which sorting method you should use, as I don't really make much use of them...

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: User entered values for arrays

Posted 05 October 2013 - 11:09 AM

Quote

I need to create 2 classes - one for setters and getters and another to instantiate 5 objects from the first class and prompt the user for values for 3 fields - title, author, and number of pages.


I'm more than a little confused. What do you mean, "one for setters and getters"? Are you wanting to make one class to handle setting and getting the values, and another to handle other manipulations of the values? This sounds a little like the old joke about Frankie Gavin recording his fiddle parts in two passes - once to get the bowing, and a second go to get the fingerings.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#6 KarenByk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 13

Re: User entered values for arrays

Posted 05 October 2013 - 11:16 AM

I have no idea why. I hate set/get methods because at this point in my learnin' they are pretty useless. What I want to achieve can be done in one class. I'm assuming it is just to get in the habit for future, more complicated things.
Assignment says to create a class that contains fields to hold methods for setting and getting title, author, and page count.
Then write an application that instantiates 5 objects and have the user enter values for the data fields. Then have the user decide what to sort by.
If you want to sort by the author then the title and pages that are "with" it need to stay with it.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: User entered values for arrays

Posted 05 October 2013 - 11:30 AM

Oh, I get it. Your LibraryBook class is just a data bag. Gotcha.

So your input issue is because you're using the next() method, which gets the next token. The delimiter used by default is "any whitespace", so next just gets the next word. Try nextLine(), which gets up to the next line break character and returns the result, discarding the line break.

It's a good idea to avoid mixing next() and nextLine(), since they can interact weirdly. This includes, unfortunately, the convenience methods for parsing primtive types, such as nextInt(), which are based on a call to next() and also interact weirdly with nextLine().

So this means that this call
 pages = inputDevice.nextInt();


is going to get a little complicated. There are a few reasonable ways to deal with this. If you search this site, you'll find several threads where I've explained this problem. If you need further explanation, I can try to provide it.
Was This Post Helpful? 1
  • +
  • -

#8 KarenByk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 13

Re: User entered values for arrays

Posted 05 October 2013 - 11:35 AM

I found that out already. I decided to change it to a String as well. Keep them all the same. May help with sorting too...?
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: User entered values for arrays

Posted 05 October 2013 - 11:40 AM

Numeric and lexicographic sorting work slightly differently from each other.

Consider what you'd get from sorting [2,3,23,34,100,123] as ints and as Strings.
Was This Post Helpful? 1
  • +
  • -

#10 KarenByk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 13

Re: User entered values for arrays

Posted 05 October 2013 - 11:59 AM

Alright, so back to integer it is!
 pages = Integer.parseInt(inputDevice.nextLine());


Any advice on what type of sort to use?
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7744
  • View blog
  • Posts: 13,083
  • Joined: 19-March 11

Re: User entered values for arrays

Posted 05 October 2013 - 12:13 PM

The simplest thing to do, unless the objective is to learn sorting, is to use Arrays.sort()

To sort by different fields, you'll want to look into Comparators to define different comparison functions.
Was This Post Helpful? 0
  • +
  • -

#12 KarenByk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 28-September 13

Re: User entered values for arrays

Posted 05 October 2013 - 01:28 PM

I ended up with a bubble sort. Thanks for the help.
For anyone else looking for information on this:
LibraryBook temp;
      for(x=0; x < books.length - 1; ++x)
         for(int y=0; y < books.length -1; ++y)
            for(int z = 0; z < books.length - 1; ++z)
            {
               if(sortChoice.equalsIgnoreCase("pages"))
               {
                  if(books[z].getPages() > books[z+1].getPages())
                  {
                     temp = books[z];
                     books[z] = books[z+1];
                     books[z+1]=temp; 
                  }
                }
               else if(sortChoice.equalsIgnoreCase("author"))
               {
                  if(books[z].getAuthor().compareTo(books[z+1].getAuthor())>0)
                  {
                     temp = books[z];
                     books[z] = books[z+1];
                     books[z+1]=temp; 
                  }
               }
               else if(sortChoice.equalsIgnoreCase("title"))
               {
                  if(books[z].getTitle().compareTo(books[z+1].getTitle())>0)
                  {
                     temp = books[z];
                     books[z] = books[z+1];
                     books[z+1]=temp; 
                  }
               }
            } 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1