• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort the arraylist after the name at the begening and then merge strings  RSS feed

 
Alex Ardoin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to merge strings in my arraylsit buffer when the same name was found. With the surrent state I am getting just the folowing output:

Current output:
Ravensbusch 11:38 18:38
12:16 18:46
markt haus 11:46
Rathausmarkt 11:41 18:41


How can I get all the time togather with my approach and how can I get the same order of the name as they were added.

I appreciate any help.

In this oreder:
Ravensbusch 04:41 .......... 18:38
Rathausmarkt 04:43 ......... 18:41
markt haus 04:48 ............ 18:46

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line...

...doesn't compile. Are you sure you posted the same code you ran?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post a full working example.
This example is missing the definition of your pattern which is quite important to understand what is happening.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My approach would be to go for a data structure that more closely matches reality
You are using Map<String, String>
The key appears to be station name
The value appears to be a string of all the stop times?
We have multiple times for each station, and they end up lumped all into one string which is difficult to work with..

I would suggest something like
Map<Station, List<Time>>
I guess you could do it as Map<String, List<String>>, but do you see how the first communicates the concepts better?


 
Alex Ardoin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I forgot yesterday to post this pattern line yesterday



Output:



The other qestion why is the order of the string not like in the arraylist also Ravensbusch , Rathausmarkt, markt haus ?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another question.

Why are you using Strings to keep your timetable information? Why do you not have a Bus class with times for each stop? Or a Stop class with times for each bus? Or both? As you will see in our FAQ, a String is very unsuitable for recording such information.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan has already hinted that by suggesting you use a List<Time>
 
Alex Ardoin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I use the following I am getting:



I am getting this error:



I am just parsing pdf document to get the timetable.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alex friday wrote:

The error message is giving you a hint at the problem. Can you see that the second parameter required is a List but you are passing a String? What happens if you instead pass in a List<T> of some sort? I'd recommend a List<T> over a List, firstly because List still remains valid only to support legacy code and secondly because with List<T> you restrict what the list can store so guarantee that each element will be of type T.

alex friday wrote:I am just parsing pdf document to get the timetable.

you should be able to extend your parser to return a List<T>, which would help with the above.
 
Alex Ardoin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have created Stop class:





With this line:



I am getting this error:

 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not applicable for the arguments (String, String)


Does this part of the error give you any further direction as to what else you need to change?
 
Alex Ardoin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but only nothing springs to my mind Please can you tell me what else I have to change?
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is saying that you are passing a String as the value parameter when in actual fact the put(K, V) method is expecting a List<Stop> as the value parameter. You will need to modify or extend, depending on how it is structured, the parser to provide you with a list of elements. Have a look at the String methods, since that is what your parser currently seems to be outputting; you should be able to come up with a way to split the string into a list using a common delimiter (it looks like " " might be a good choice )

Campbell Ritchie wrote:Stefan has already hinted that by suggesting you use a List<Time>

You might find your code makes a bit more sense if you have:

That way you do not need to create any new classes, the name of the stop is the key in the map and the times that busses stop there are contained in the list associated with that key. It's neater than creating a Stop class, which currently looks like a superflous man in the middle.

Edit:
Alternatively, stick with the Stop class and use List<Stop> to store all of the information. However, doing so will require more overhead and programming logic to make sure you only have one instance of Stop for each bus stop, and you'd have to check each Stop in the List for it's name to return the correct stop, i.e.:

Which is a lot more effort than using a Map with the name as the key, list of times as the value:
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought you wanted to merge a complete set of times. If so, you'll need to change your Regex:

Ouput is now:

 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My recommendation: get rid of the regular expression all together.
You don't need to use regular expressions to parse this structure.
It is a simple space separated list, with the first item being the station/stop and subsequent items being times - well almost anyway.
"Markt haus" stop has two words in it so its not QUITE as simple as a line.split. You need a bit more logic to work out where the stop name stops and the times start But close.

Here is the concept in any case - (note that it assumes that the first item is the stop name which is an incorrect assumption as I mentioned)



>So I have created Stop class:

Awesome. That looks like a handy little object.

>Map<String, List<Stop>> stops = new HashMap<>();
>The method put(String, List<Stop>) in the type Map<String,List<Stop>> is not applicable for the arguments (String, String)

This would need a tweak now.
In effect we want to map a stop name to a list of times it is stopped at.
Seeing as that is now encapsulated in your stop object, then we just want to map the name directly to that:
In effect: Map<String, Stop> where the String is the name of the stop.

And so your code would become

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!