This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to solve particular string problem  RSS feed

 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to have each time in an ArrayList<String>, right now I have this



How can I place times into a ArrayList so that each time is in its own array ?

I tried this:


but I get Illegal/unsupported escape sequence in Android Studio.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you need to do is split on the space. Since you already have just 1 string, you will need to split by what separates the data in the String, which is the space.

String[] s = a.split(" ");
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I can't do that because temp[3] = "05:51\n06:04"
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use a Scanner which defaults to splitting on spaces:-You cannot put things into a List so each is in its own array. Not unless you write something likeAnd I am sure that isn't what you want. You do not have times; you have Strings with four digits and a : which is something different. You can probably use those Strings for the constructor of a time class, but they are not times at present.
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I use regular expressions and split() to solve this problem?
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to use regular expressions and split()? A Scanner already uses regular expressions behind the scenes. Work out what regular expression it uses (hint: it has a method which shows the regular expression in use at present).
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try something like this:

\s is a regex for whitespace.
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is the output if dlimiter =\\s ; temp[4] is = " "

 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, use \\s+
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks it seems that it works.

Why is this +, does it means that it can be more than one space?
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have ? which means 1 instance or none.
You can have * which means any number of instances including 0.
You can have + which means any number of instances greater than 0.
 
John Losty
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have + which means any number of instances greater than 0.
but I don't have more than one space together anywhere in my a string ?
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not just spaces; it's tabs and newlines and any whitespace.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!