• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Different results with println and add to list?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I dont really understand whats going on here...

I am accessing User permissions from the server, and looping through the subcategories...
when I println the variable I get the exact result Im looking for, but when I add each instance of the variable to a list; the list doesn't give the same result.

also I am using a break; to jump out of the loop because it was printing the last line twice and I didn't know why?

here is a bit of the code...




I have attached some screenshots of the console output as well.


I have a few more questions regarding this same project
(but I'll pace myself...this is my first real java project, and I am running in to a lot of (at least seemingly) anomalous results)
any help would be greatly appreciated.
Thanks.
in-order-println.JPG
[Thumbnail for in-order-println.JPG]
This is how I want the permissions to be output 12345
looped.JPG
[Thumbnail for looped.JPG]
if I print the list inside the loop I get this,where they are numbered 11111 I understand that the output is repeated...its printing the first then first & second and so on...but there is a straggling bit of code at the end?
q.JPG
[Thumbnail for q.JPG]
If I print the list outside of the loop, all I get is the straggler?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to go back to pencil and paper and write down what you need. That code looks like what you get if you dive in and write lots of code before you work out what it does.

Why have you got everything marked static?
You have some strangely‑named variables, eg Three which has nothing to do with one more than two. If you have that sort of names, it is very easy to forget what the variables mean. All that commented‑out code and unnecessary blank lines make the code even harder to understand. You have all sorts of loops, and I am not convinced that they all run. A few print statements inside some of the loops will confirm that you are actually running them. You can delay the execution with something like this, if it scrolls thousands of lines too quickly
 
Jonathan Caldwell
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response,

I guess my main question is...

how is it possible to get different results from add to list and println?

or

is it possible to output to a string from system.out.print?
That option seems silly.
I might be doing all of this the hard way?

I have sort of "dived in" to programming in general.

does the end justify the means or vis a vis?

I guess formatting is very important.
(since this is a work in progress...maybe its bad form to clean up my toys after playing?)
but I am interested in functionality.
why its doing what its doing...etc

how could add to list have a different result that print?
especially if they are being executed on the same line?

I tried to clean up my code here...sorry for posting messy stuff before. the extra blank lines help me find parts im still working on. or parts that need attention.



as far as the loops...
there is another one that they are all nested inside of also.
the outer loop (that is not shown in this code) finds the next subcategory of the category...

the loops are really just a for loop nested inside of another for loop...
one takes out the stuff I dont need...then one seperates the stuff I do need.

I am pretty sure they are all doing their jobs?



any insights on the output?
do you think my loops could be causing the problem?


Thanks

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jonathan Caldwell wrote:
how is it possible to get different results from add to list and println?


based on that one statement, I would say that you would get different results because "Add to a list" would add an item to a list, and println would print something to the screen.

That's like saying "why do I get different results between going to the store and cooking dinner?" They do different things, so I would expect different results.
 
Jonathan Caldwell
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ahh Semantics...

:?(

I mean....
I am printing the same String that I am adding to the list.
but they are showing up differently.

why? or how?

can you give me a helpful analogy for how these process the variable differently?

and

even more helpfully a way to get a similar result to my printed result in List form?

Thanks







 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jonathan Caldwell wrote:ahh Semantics...
. . .

Maybe, but you have got a machine which takes you literally.
You need to read about how a List works, which you will probably find here. You also should work out how a List is displayed when you send it to println. How do you manage to get those numbers into the List? How are you filling the List?

Those deeply‑nested loops look awkward, and complicated. There must be a better way of filling those Lists. Back to the drawing board, and write down on paper exactly what you want to do. Divide it into bits, and get each bit worked out first. Your method looks long, and should maybe be divided into several methods.

Why have you marked everything static?

Yes, it looks better with the new formatting, but I had to split that long comment into several lines. Yes, you do have to format the code, if only because nobody will help you about code they cannot read!
 
Jonathan Caldwell
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

I have whats marked static as static because eclipse suggested it.

I have no better reason.

I feel discouraged that you say I have to go back to the drawing board...because I have the result I need.

why is that your immediate answer?


I am adding to the list inside the loop....adding every user permission for every user



where SUserLoop is the variable that holds each line of permissions as a string.

If I put println(SUserLoop) in the same place I am putting add(SUserLoop)

the output is exactly as I want it.


the loops are complicated.

I might be over complicating but I am taking a huge messy source data (that's how it comes to me)
and I am parsing it down to get specific user info.



The process is something like

1 Get all User info

2 separate into list of user names

3 and list of info per user

get rid of everything else

4 number each element in the user info list so it will correspond to the list of username list index

then when a user is selected their permissions can be compared to another selected user.


...


I am almost to the middle of the project.


you think I should start over?



 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jonathan Caldwell wrote:Thanks

You’re welcome

I have whats marked static as static because eclipse suggested it.

I have no better reason.

In that case you were mistaken to believe the suggestion to make it static. A static member belongs to the class, not the object. I suspect you want all those Lists to belong to an object, in which case they can’t be static. And the methods which use those Lists ought not to be static either. Only make things static when you have a good reason.



I feel discouraged that you say I have to go back to the drawing board...because I have the result I need.

why is that your immediate answer?

I am sorry about that. But that is what the code looks like. You might be able to improve it by giving the Lists decent names. Eclipse will do that nicely. It is something to do with highlighting and ctrl-2 R. It will find all the instances of that name and you can give it a sensible name.

I am adding to the list inside the loop....adding every user permission for every user



where SUserLoop is the variable that holds each line of permissions as a string.

If I put println(SUserLoop) in the same place I am putting add(SUserLoop)

the output is exactly as I want it.


the loops are complicated.

I might be over complicating but I am taking a huge messy source data (that's how it comes to me)
and I am parsing it down to get specific user info.



The process is something like

1 Get all User info

2 separate into list of user names

3 and list of info per user

get rid of everything else

4 number each element in the user info list so it will correspond to the list of username list index

then when a user is selected their permissions can be compared to another selected user.


...


I am almost to the middle of the project.


you think I should start over?

Depends how much time you have got and how many marks you want.

If you are sure you are filling the Lists correctly, there is a possibility which can alter the display without altering the input. I can think of two versions of it, one of which will follow.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suggestion 1:

Create your own class whcih extends ArrayList<E> and which adds the number you want as another field. Override the toString() method to print the number. Consider whether to override the equals() and hashCode() methods, too.

Suggestion 2:
I had thought to leave this for another post, but no. Both suggestions in one message.

Create a Wrapper class around your ArrayList<E>. The way it works is like this:-
  • 1: You have an ArrayList as a field.
  • 2: You have a number as a field, which you can initialise via the constructor, or via a counter field. This probably ought to be static. You can find an example of something similar here. But that example needs a lot of improvement.
  • 3: Give the wrapper class a sensible name and make it implement the same interfaces that ArrayList does, which are listed in its documentation. (You can miss out Collection and Iterable.)
  • 4: Copy every method of the ArrayList class, as shown below, except toString()
  • 5: Find out how to use a StringBuilder to create a String, or alter one. Use that in the toString() method.
  • 6: Consider whether to override equals() and hashCode(), or simply to use the implementation in the List.


  • Now you use your classes in place of the ArrayList. Since both suggestions implement List, you can simply slip them in instead of the ArrayList. That is the nice thing about declaring the lists as List, not ArrayList, which you have done correctly
     
    Jonathan Caldwell
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks,
    I am working on this again now. looking into your suggestions.
    I have so many questions its hard to get any definite answers.
    I see what you mean about wrappers, because the other code might be interfering with the string creation.

    I'll get back soon with results/more questions.

     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Good luck with it
     
    This will take every ounce of my mental strength! All for a tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic