944,010 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 9306
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 4th, 2006
0

breaking up a String... (a bit more advance)

Expand Post »
Hello everyone!

Any help would be much appreciated!

My problem:

I want to break up a string into separate objects.

Scenario:

I am reading a txt file into my program and I want to
separate the String in to separate categories.

String line = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ;

Java Syntax (Toggle Plain Text)
  1. String line = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ;

to

String category = "BARTHROOM TOI" ;
String sku = "48157" ;
String tmp = "Y" ;
String description "DOVE BWASH....." ;
etc..
etc...

Any help would be much appreciated

Andy.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
outbackandy is offline Offline
5 posts
since Jul 2006
Jul 4th, 2006
0

Re: breaking up a String... (a bit more advance)

Looking at your string I assume that the different parts of the string are
seperate4d by a TAB character. If so do the following:
Java Syntax (Toggle Plain Text)
  1. String line = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ;
  2. String[] parts = line.split("\\t");
  3. //parts[0] = "BATHROOM TOI";
  4. //parts[1] = "48157";
  5. // etc. etc
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jul 4th, 2006
0

Re: breaking up a String... (a bit more advance)

Quote originally posted by masijade ...
Looking at your string I assume that the different parts of the string are
seperate4d by a TAB character. If so do the following:
Java Syntax (Toggle Plain Text)
  1. String line = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ;
  2. String[] parts = line.split("\\t");
  3. //parts[0] = "BATHROOM TOI";
  4. //parts[1] = "48157";
  5. // etc. etc
Gee thanks mate, that's pretty close. For the moment I'm now using two separated spaces i.e. line.split(" ") instead of your tab idea - which by the way has given me a GREAT starting point. The data is unfortunately not separated by tabs.
Thank dude!!! - AB
Reputation Points: 10
Solved Threads: 0
Newbie Poster
outbackandy is offline Offline
5 posts
since Jul 2006
Jul 5th, 2006
0

Re: breaking up a String... (a bit more advance)

No problem. And have fun.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jul 5th, 2006
0

Re: breaking up a String... (a bit more advance)

This is a rough draft solution to tha problem. (if your are inferested) Thanks for your help!

Java Syntax (Toggle Plain Text)
  1. String description = null ;
  2. String cat = null ;
  3. String sku = null ;
  4. String temp = null ;
  5. String qty = null ;
  6. String newLine = null ;
  7.  
  8.  
  9. ArrayList list = new ArrayList() ;
  10. // different combinations.
  11. String line = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ;
  12. //String line = "DEODORANTS 9609 REXONA R/ON 50ML ICE COOL 6" ;
  13. // String line = "FRAGRANCE 16696 Y CK ESCAPE EDP 30ML 2" ;
  14. // String line = "DEODORANTS 9609 REXONA R/ON 50ML ICE COOL 6 NEW" ;
  15. String[] parts = line.split(" ");
  16. for (int i=0; i < parts.length ; i++) {
  17. if (parts[i].length() != 0) {
  18. list.add(parts[i]) ;
  19. }
  20. }
  21.  
  22. // remove surounding white space - if any
  23. for (int i=0; i< list.size() ; i++) {
  24. String ob = (String) list.get(i) ;
  25. ob = ob.trim() ;
  26. if (ob.charAt(0) == ' ') {
  27. int finish = ob.length() ;
  28. String newString = ob.substring(1, finish);
  29. ob = newString ;
  30. }
  31. list.remove(i);
  32. list.add(i,ob);
  33.  
  34. switch (i) {
  35. case 0: cat = ob ; break ;
  36. case 1: sku = ob ; break ;
  37. case 2: {
  38. if (ob.equalsIgnoreCase("Y")){
  39. temp = "Y" ;
  40. }
  41. else {
  42. description = ob ;
  43. }
  44.  
  45. } break ;
  46.  
  47. case 3: {
  48. if (description == null) {
  49. description = ob;
  50. }
  51. else {
  52. qty = ob ;
  53. }
  54. } break ;
  55. case 4: {
  56. if (qty == null) {
  57. qty = ob ;
  58. }
  59. else if (ob.equalsIgnoreCase("NEW")) {
  60. newLine = ob ;
  61. }
  62. } break ;
  63. case 5: {
  64. if (ob.equalsIgnoreCase("NEW")) {
  65. newLine = ob ;
  66. }
  67. } break ;
  68.  
  69.  
  70. } // end swith
  71.  
  72. } // end for i
  73. System.out.println(line);
  74. System.out.println("");
  75. System.out.println("Cat: " + cat);
  76. System.out.println("Sku:" + sku);
  77. System.out.println("Temp " + temp);
  78. System.out.println("Description:" + description);
  79. System.out.println("Qty: " + qty);
  80. System.out.println("NewLine: " + newLine);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
outbackandy is offline Offline
5 posts
since Jul 2006
Jul 7th, 2006
0

Re: breaking up a String... (a bit more advance)

Is each data chunk given a certain amount of space to be written in and just filled in with blank spaces to pad a smaller value?
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jul 7th, 2006
0

Re: breaking up a String... (a bit more advance)

no it's really all random information, but the space between the data is at least two blank spaces between them.

The information is being read from a txt file that was cut and pasted from an email.

AB
Reputation Points: 10
Solved Threads: 0
Newbie Poster
outbackandy is offline Offline
5 posts
since Jul 2006
Jul 9th, 2006
0

Re: breaking up a String... (a bit more advance)

well split() does take a regular expression, so why not just use that?

It looks for 2 or more spaces to be the delimiter.

Java Syntax (Toggle Plain Text)
  1. String s = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ;
  2. String[] tokens = s.split(" {2,}?");
  3.  
  4. for(int i=0; i<tokens.length; i++)
  5. System.out.println(tokens[i]);
Last edited by Phaelax; Jul 9th, 2006 at 10:43 pm.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jul 9th, 2006
0

Re: breaking up a String... (a bit more advance)

Thanks everyone for your help.

AB
Reputation Points: 10
Solved Threads: 0
Newbie Poster
outbackandy is offline Offline
5 posts
since Jul 2006
Jul 15th, 2006
0

Re: breaking up a String... (a bit more advance)

use String Tokenizer
Reputation Points: 10
Solved Threads: 0
Newbie Poster
N[e]tt[e] is offline Offline
18 posts
since Feb 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: new/help
Next Thread in Java Forum Timeline: Java Interpreter: LET Statement





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC