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 :

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.

Replies To: User entered values for arrays

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?

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?

Re: User entered values for arrays

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

Explanation: Frankie Gavin is an Irish fiddler known for playing ridiculously fast, to the point where people came up with all sorts of rumors about his supposed "studio trickery".

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.

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.