• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add String Array into String Array or ArrayList dynamically!  RSS feed

 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have lots of lines which contain 2 dates.
example:-
aaa.
blah abc 01-01-2016 02-01-2016 xxx yyy.
blah abc 02-01-2016 02-03-2016 ooo ppp.
bbb.
...

Now somehow I fetch these lines in String[] ie String Array.
Then by sub string i manage to check words like 'Blah' & 'abc' & dates (means if in some line if date not present etc).
But i can not create separate list or array of first date & second date.
 
Checking of date following function I used:-


But I am getting exception at line 'date_list1.add(dates[0]);' & 'date_list2.add(dates[1]);' as follows:-


Question:- How to make two separate ArrayList or Array of String which contain dates?

Need Help to solve this.
 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to add initialization of date variable.
It is as below:-
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even without reading your code I started to get suspicious seeing a long row of }s. Search for arrowhead code or arrowhead antipattern and see what people say about that sort of code.

The exception you are suffering means you are trying to add something to an unmodifiable List. Read the details of the Arrays#asList() method. Work out what you are returning if you pass () as its arguments. Also please explain why you have those null tests in lines 16 and 19.
I think you are going to have to start again.
You have written all sorts of things about what you want to get rid of; now tell us what you want to keep. Please explain that regex, where you got it from, and how it is supposed to work. Are you seeking dates in dd-mm-yyyy format? I can see at least one error in that regex: what does [- /.] mean? Why not use the regex to find the parts of the String representing dates and simply keep those parts?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mandar Khire wrote:. . .
That looks like a mistake to me. Why are you using arrays in the first place? Why not go straight for a List? Why are you initialising anything to null?
 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Even without reading your code I started to get suspicious seeing a long row of }s. Search for arrowhead code or arrowhead antipattern and see what people say about that sort of code.

Sure I will remove arrowhead code or arrowhead antipattern.

Here I wrote separate java program & checked with UCdetector & Findbug:-


Campbell Ritchie wrote: Please explain that regex, where you got it from, and how it is supposed to work. Are you seeking dates in dd-mm-yyyy format? I can see at least one error in that regex: what does [- /.] mean? Why not use the regex to find the parts of the String representing dates and simply keep those parts?


I searched in Google 'How to separate 2 dates from String' & found getDate function in Stackoverflow. That i used.
Stackoverflow

Campbell Ritchie wrote: now tell us what you want to keep.

I want to create 2 separate Arrays or ArrayList of Strings (date (dd-mm-yyyy)) as below:-
Position        date_list1            date_list2
0                   01-01-2016        02-01-2016
1                   02-01-2016        03-01-2016
2                   03-01-2016        04-01-2016
3                   04-01-2016        05-01-2016

Thanks,
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I was mistaken about [- /.], but have you tried that with all the different separators between numbers? Please tell us more details about where you found that regex. In two days it will be 1-11-2017 here. Can your regex match that? Does it always require padding the numbers to a particular length with leading 0s?
I am not convinced you have got rid of all the }s.
Why don't you simply find the part which matches the regex and keep that match?
 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Maybe I was mistaken about [- /.], but have you tried that with all the different separators between numbers? Please tell us more details about where you found that regex. In two days it will be 1-11-2017 here. Can your regex match that? Does it always require padding the numbers to a particular length with leading 0s?
I am not convinced you have got rid of all the }s.
Why don't you simply find the part which matches the regex and keep that match?

Yes, You are right for 2 things.

1. function copied from Stackoverflow not match date '1-11-2017' but works fine when '01-11-2017'.
But in my case I always use 'dd-mm-yyyy' ie dd always 01,02,03...09.
I get that function(stackoverflow).
My main concern is after getting 2 values in dates[0] & dates[1], how I can add those in String Array or String ArrayList.

2. I added one For loop unnecessary. ie


Thanks,
 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,
After Rethinking about question raised by 'Campbell Ritchie', there are two serious issues.
1. Though I am confirm that I never get date without leading 0, but if I get date d-mm-yyyy, then this program not add that specific date in Array or ArrayList.
After that incident I will wake up & start finding solution. This is not good for Java programmer.
2. I can not Add values what I get in Array or ArrayList, even after 579 posts & replies in coderanch (At least 100 are in java related).
This is also not good.

I have to restudy all basics.
 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie,
I got answer my self. I forgot points given in How To Ask Questions On Java Ranch.
I change

to


Thanks,
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you have enough sleep, it is a good thing to wake up .
There are databases of regexes available on the net, along with regex tutorials. There is also a regex section in the Java™ Tutorials, which I think is good. Search for regex tutorials and databases, and you will probably find a ready‑made regex for dates . If you keep searching, you will find all sorts of useful things about regexes. Even ready‑made test harnesses where you can submit a regex and test text against it.
I know that two of the characters you have inside those [] are metacharacters. I think − doesn't count as a metacharacter when it is the first character inside [] (not certain). I am not sure whether . still counts as a metacharacter when inside [] or whether it needs to be escaped \\. But I am pretty sure you will find that from the many regex sites available.
I think you can iterate those Strings and find all the groups which match the date format and add those groups to a List. You will have to decide whether you want a List<String> or a List<List<String>> or even a List<String[]>; all are valid, but they are different from one another. I personally would try to avoid the List of arrays.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mandar Khire wrote:Thanks Campbell Ritchie
That's a pleasure
. . . I change

to


Thanks,
Why? If you are creating an empty List, what is wrong with myList = new XYZList<>();? Why did you choose a linked list rather than an array list?
 
Mandar Khire
Ranch Hand
Posts: 582
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Why? If you are creating an empty List, what is wrong with myList = new XYZList<>();? Why did you choose a linked list rather than an array list?


I tried LinkedList by reading Stackoverflow.
Then my example works, Also My actual working code also works.

Now I am searching root cause of why Linkedlist works & why Arraylist not works.

If anyone know root cause then it will helpful for understand basics better way.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very surprised you can get a linked list to work and not an array list. Please give the fully‑qualified name of the class you are using for your array list. There is a hint in that SO link about why you may be having problems.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!