wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Can't figure out the logic in this method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't figure out the logic in this method

Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
Been a few days for me. I'm working bit by bit on my program to arrange my CD collection, and I'm trying out a while loop. I have these classes so far:

which just set the values for the variables

and this class, CDInputTestDrive, which contains the method to input the information:



For completeness, I'll also include the input code and the class with the main method:









The process is simple -- a user enters the information for each field of the CD which is added as an entry to the cdRecord Array List, after the setter methods in CD.java assign the values to the individual variables for the field. The InputCD method works fine when a user answers eithrer q or s for a CD; the fields are assigned values based on that answer and the field being entered. When a user enters information for all the fields, though, it goes screwy; Here's an example (copied from my Terminal Application screen on my mac):


Enter information for each CD field.
To skip a field, press RETURN.
To skip the current and remaining fields and go to the next record, enter S (case insensitive)
To skip the current and remaining fields and quit entering CD's, enter Q (case insensitive)

Artist: dire straits
Album: communique
Record Label: s

Artist: eric johnson
Album: ah via musicom
Record Label:
Year Of Release: s

Artist: crowded house
Album: crowded house
Record Label: sony
Year Of Release: 1986

Artist: allison krauss
Album: new favorite
Record Label: q


Size of Array List: 4

Record 1:
Artist: dire straits
Album Title: communique
Record Label:
Year of Release:

Record 2:
Artist: eric johnson
Album Title: ah via musicom
Record Label:
Year of Release:

Record 3:
Artist: allison krauss
Album Title: new favorite
Record Label:
Year of Release:

Record 4:
Artist: allison krauss
Album Title: new favorite
Record Label:

The entry for crowded house is gone and is replaced with allison krauss. As long as you choose q or s, the program works fine, but as soon as you enter all four fields with any other text (including "") the next entry that contains a q replaces that and all the following entries.

I think I have some sense of what's wrong -- it has to do with the fact that there's no break when all four fields are entered, so there's it's replacing the current entry with the new info, or something like that. How is it possible that it adds a record to the cdRecord Array list if there's no new CD object being created as evidenced by the same information for more than one entry?

If I place the add() method after the second if test in each of the field entries and then break, everything works out. But I'm looking for a way to code as leanly as possible, and something tells me putting the method in after every if test for each field is cumbersome (and it can get really cumbersome if I want to add more fields in the future).

But I can't quite figure it out. Can someone help me, and maybe give me a suggestion as to how I can fix this so no matter what is entered, each entry is added to the cdRecord?

I've been struggling with this for days, changing the code, but each time I get some other problem, either with output or with compiling. If I place the CD Entry after the second while test, I get this compiling message, for example:

CDInputTestDrive.java:114: cannot find symbol
symbol : variable Entry
location: class CDInputTestDrive
cdRecord.add(Entry);



Am I misunderstanding something about while loops? or booleans? Is my code too complex (which may very well be the problem)? Any help is welcome.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The code is difficult to read due to formatting and cyclomatic complexity issues.

One thing I don't understand is why you add the last entry twice. The loop logic is a bit convoluted, too.
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
Can you offer me some tips as regarding the legibility of the code? I haven't done anything different from any other posts. (What do you mean by cyclomatic?)

I'm not sure what's going on regarding the logic; that's why I'm posting here. I've been staring at this for so long, I'm probably missing something obvious. and I was asking if there was someone who could help me figure out why I'm getting the results I am. The entry is only supposed to be added one time; I thought that the loop(s) only added an entry one time -- once if the entry for any of the fields was or s -- in which case it breaks out of the inner while loop, adds the entry to the array, and then creates a new Entry (the only time it breaks out of the outer loop -- or is supposed to -- is when q is entered; if it's 's' then the entry is added to the list, but needs create another new entry and set the values for each of the fields.) Does that make things clearer?
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

the code is legit but will be a handfull to maintain and extend.

maybe you can separate your control input (s,q) from your data input?

and break your menu down to smaller, easier to maintain portions?

What if you start off with a menu screen like:

1 - list all CDs
2 - add a new CD
3 - edit an existing CD

4 - exit program

and all you read in is a number from 1-4.

When the user chooses 2, you display another screen:

1 - Artist:
2 - Album:
3 - Record Label:
4 - Year of Release:

5 - back to main menu

On this screen then for option 1-4 you prompt the user
accordingly.

Hope that helps
Matt


Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

Christopher,

What (I think) David means is that reading the code is not very helpful because there's too much going on to follow.

Here's what I would do from here:
1. Do some println statements to tell where in the loops/ifs you are... so you can see if something is happening twice or not at all or when it's supposed to.
2. You can try to work this out on paper, but I think #1 may be faster and more effective for you.
EDIT 3. Check this out...


Hope that helps!


When you do things right, people won't be sure you've done anything at all.
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

One of major mistake is scope of the "Entry" object in "InputCD" class. At line no 49
You are using the same object until user enters "q" or "s". At line no 52.
Below are my inputs and results using the same code:
Case One:

Enter information for each CD field.
To skip a field, press RETURN.
To skip the current and remaining fields and go to the next record, enter S (case insensitive)
To skip the current and remaining fields and quit entering CD's, enter Q (case insensitive)

Artist: a
Album: a
Record Label: a
Year Of Release: a

Artist: b
Album: b
Record Label: b
Year Of Release: b

Artist: c
Album: c
Record Label: c
Year Of Release: c

Artist: q


Size of Array List: 3

Record 1:
Artist: c
Album Title: c
Record Label: c
Year of Release: c

Record 2:
Artist: c
Album Title: c
Record Label: c
Year of Release: c

Record 3:
Artist: c
Album Title: c
Record Label: c
Year of Release: c

Case Two:

Enter information for each CD field.
To skip a field, press RETURN.
To skip the current and remaining fields and go to the next record, enter S (case insensitive)
To skip the current and remaining fields and quit entering CD's, enter Q (case insensitive)

Artist: a
Album: s

Artist: b
Album: s

Artist: c
Album: q


Size of Array List: 3

Record 1:
Artist: a
Album Title:
Record Label:
Year of Release:

Record 2:
Artist: b
Album Title:
Record Label:
Year of Release:

Record 3:
Artist: c
Album Title:
Record Label:
Year of Release:





A.A.Anbarasu
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
Janiece, I'm sorry, but I don't quite follow what you mean. Could you show me where, maybe?

This code is by no means the final version. I don't have separate screens yet -- I'm just doing this from the command line right now. I'd eventually like to use GUI's and things like that, but I'm not there yet. I'm still trying to figure out why the replacement is going on.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

Christopher Laurenzano wrote:Janiece, I'm sorry, but I don't quite follow what you mean. Could you show me where, maybe?


I'll give you a sample.... to be thorough you might put it inside each if/while/for loop


Inside the for loops you might just have a ("variable name equals " + variable) statement. You'll also put one on the outside of the whiles ("outside second while")...

This makes it easy to see what's happening or not happening.

Another (better) way to do this is with assertions, but that may be a new concept you might not want to take on right now.
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
I was thinking about this this morning and I think I've figured out what's going on.

As long as any one of the entries is q or s, things work out fine, because in the inner while loop, the if condition is met, the values for the fields are set and the loop is broken and the newly created Entry object is added to the array list; if the entry is q, then the boolean test is met and the printout of the array is exectued; if it's s then the next Entry object is created, but in either case a new object is created and added to the array list.

The problem occurs when none of the entries is either q or s, which means that the newly created Entry object is added to the array list at the bottom of the while loop; it then goes through the loop again BUT...

No new Entry object is created after the field values are set; it's simply added to the array list and is used as some kind of a 'template' for the next iteration of the loop, which means that the field values for each iteration, from the one with no entry of 'q' or 's' are changed to those of the current iteration, will have the same values; the most recent entry, though, is added to the loop, increasing the size of the array by one.

Once 'q' or 's' is entered, then the loop is broken, the current Entry is added and another Entry object is created (back to the outer while loop).

If the entry is 'q' then the boolean test is met, printout is executed, displaying the duplicated field values for each entry; if it's 's', then it adds the entry object to the array list, and goes back to the outer while loop, but all the entries from the one with no 'q' or 's' to the most recent one will all have the same field values.


It then creates the next object. The newly created object will then retain whatever values are entered for it as long as one of the entries are 'q' or 's', in which case the above process will be repeated.

So now I have to figure out a way to prevent this from happening.


I've tried various solutions over the past few days, not really knowing what what was going on, but they haven't worked.

I know this code isn't written very well, but part of the fun is figuring things out, right? Only way to get a good grasp of the concepts.

Oh well, back to the drawing board....

There is one thing I just thought about-- if the artist is 'q' and the entry is removed, how then can it be added to the array list (the second cdRecord.add(Entry) line?)

Please feel free, anyone, to check my logic and see if I've diagnosed the problem correctly and/or if I've missed anything, which is quite possible, since I've been going over this so many times.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can't figure out the logic in this method
 
Similar Threads
explanation of getter/setter methods -- what's the difference exactly?
cannot find symbol error -- can't see what I'm missing
Null pointer exception when trying to enter a blank for a response -- how to fix?
Array or Array List or something else for entering a theoretically limitless amount of objects/info
ArrayList objects