• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unsure if logic is correct for program  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to post again I should of not probably posted my last post - I have some logic for some code but I'm not entirely sure how or if you can actually do it so I was wondering if I post my logic would anyone mind taking a look please?

The bits I am unsure of is stopping the writing to the arraylist and then starting it up writing again.

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to "stop writing to the arraylist". BTW what does that mean? - is it that you want to stop adding items to the arraylist for a particular tag. You can do this several ways such as once you have found the tag to ignore keep calling br.readLine() until you have found the tag that marks the end of the ignore region.

BTW looking at the code I would suggest you think more about your variable names. For example 'arraylist' tells you what it is and not what it refers to ie you wouldn't call a double holding the value of PI 'doubleValue' you'd call it 'pi'. Also 'lines' holds a single line so using the plural is misleading.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things first:

The name lines is misleading. readLine() only reads one line from the input so I would rename that variable to line instead.

Instead of this: ArrayList<String> arrayList = new ArrayList<String>();
Do something like this: List<String> cleanedUpLines = new ArrayList<String>();
Starting with Java 7 you can write: List<String> cleanedUpLines = new ArrayList<>();

A name like cleanedUpLines or something like this is better than arrayList. As Tony said in his response, try to think more about your variable names.

When appropriate, the declared type should be the interface or abstract type (List), not the implementation type (ArrayList).

The variable end is never changed inside the while-loop so you'll never exit normally; you'll exit with an IOException from br.readLine(). I'm assuming you didn't include that so we could focus on your main logic instead.

=====
Now to your logic:

isFound and tagFound are redundant. They don't really serve a purpose because if you remove them and the if-statements leaving only what's inside the if-statements, the code will work the same way.

Thinking of states and transitions will help you with this kind of logic. The main state that you have is cleaning up lines: you either are or you aren't. The program will always be reading lines from the input, regardless of the "cleaning up" state.

This is how I would do it: Prepare a cleanedUpLines list and another list for uncleaned lines. Start at "not cleaning up" state. Read a line from input. Does this line have text that signals I have to start cleaning up lines? If yes, switch to "cleaning up" state. If in "cleaning up" state, add the line to unclean lines, otherwise, add line to cleanedUpLines. If in "cleaning up" state and the line has text that signals I should stop cleaning up, then clean up all the lines in the unclean lines list, then add the results to the cleanedUpLines list, then clear out the unclean lines list to get ready for the next time we switch to the "cleaning up" state; switch to "not cleaning up" state. Repeat from reading a line from input while you have more lines to read.

I'll leave it to you to try this out with code and arrange the control flow statements appropriately. This is just off the top of my head, mind you, but I think I've covered most of the necessary logic. You would probably be better off by separating the logic to "clean up all the lines in the unclean lines list" into a separate method.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys thanks for replies. I have my variables named as they are as I have other variables that do the same thing with similar names, I can assure you they are called what they are all called for good reason - I've had the other dev check it over and he is happy with it.

Junilu I appreciate your response but it sounds a bit over complicated if you don't mind me saying?I want the entire file just a chosen elements contents I don't wish to include in my arraylist.

Sorry I've tried to make sense of it but towards the end it gets a bit confusing

Also I have Java 6 not Java 7. I will take your advice and rename my arraylist to something more appropriate. Yes I did not include anything else to do with my variable end as I was trying to just include my main logic.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ask yourself this:

Can you ever get to your line 24 and have "isFound" be false:

Hint: no - not with the code you have posted.

So is there some code that is missing - either you didn't post it, or you haven't written it yet? If the answer is 'yes', ok. But if not...your if condition on 24 is 100% unnecessary and confusing.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:Junilu I appreciate your response but it sounds a bit over complicated if you don't mind me saying?I want the entire file just a chosen elements contents I don't wish to include in my arraylist.

Sorry I've tried to make sense of it but towards the end it gets a bit confusing

I understand. I tried not to spoonfeed the algorithm to you but going abstract this way tends to get wordy and I admit having that concern as I was re-reading it. I assure you that when written down in code (I just did a first pass at it), it's not that complicated. It's your call though and I respect that.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Fred - I have not included all of my code as I didn't feel that it was needed given the fact that I am trying to focus on the logic. Other than it not deleting a specific environment because I have yet to finish coding it it is 100% functional otherwise in what it should be doing. I appreciate you pointing it out though

@Junilu - I will re read it and try and break it down I think if I make some bullet points that should help. Thank you for your reply you have been very patient with me
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more shot at trying to convince you, then I'll hold my peace.

The code I wrote to implement the algorithm I described consists of 11 lines of executable code inside the main while-loop. The rest are just closing braces on lines by themselves and whitespace. That makes 18 lines in all for the main while-loop. I think that's quite a bit of lines fewer than what you have written, right? Granted, I have three calls to helper methods but extracting details to their own methods (see Extract Method refactoring) can really make your code less complicated and more expressive. Here's a sampling of what I wrote:

That's as deep as the nested if-statement goes, BTW, and it's the only that's nested that deep. I could un-nest that second one but I can live with the two levels of nesting. There are 14 lines in the elided code above so the actual code I wrote isn't much longer that what you see here.

(Edit: removed first "..." right after the while. Don't want to mislead you into thinking I wrote some code there)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!