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

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2006
Posts: 5
Reputation: outbackandy is an unknown quantity at this point 
Solved Threads: 0
outbackandy outbackandy is offline Offline
Newbie Poster

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

 
0
  #1
Jul 4th, 2006
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" ;

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,464
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 266
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

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

 
0
  #2
Jul 4th, 2006
Looking at your string I assume that the different parts of the string are
seperate4d by a TAB character. If so do the following:
  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
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 5
Reputation: outbackandy is an unknown quantity at this point 
Solved Threads: 0
outbackandy outbackandy is offline Offline
Newbie Poster

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

 
0
  #3
Jul 4th, 2006
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,464
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 266
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

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

 
0
  #4
Jul 5th, 2006
No problem. And have fun.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 5
Reputation: outbackandy is an unknown quantity at this point 
Solved Threads: 0
outbackandy outbackandy is offline Offline
Newbie Poster

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

 
0
  #5
Jul 5th, 2006
This is a rough draft solution to tha problem. (if your are inferested) Thanks for your help!

  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);
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 785
Reputation: Phaelax is on a distinguished road 
Solved Threads: 39
Phaelax Phaelax is online now Online
Master Poster

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

 
0
  #6
Jul 7th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 5
Reputation: outbackandy is an unknown quantity at this point 
Solved Threads: 0
outbackandy outbackandy is offline Offline
Newbie Poster

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

 
0
  #7
Jul 7th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 785
Reputation: Phaelax is on a distinguished road 
Solved Threads: 39
Phaelax Phaelax is online now Online
Master Poster

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

 
0
  #8
Jul 9th, 2006
well split() does take a regular expression, so why not just use that?

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

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 5
Reputation: outbackandy is an unknown quantity at this point 
Solved Threads: 0
outbackandy outbackandy is offline Offline
Newbie Poster

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

 
0
  #9
Jul 9th, 2006
Thanks everyone for your help.

AB
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 18
Reputation: N[e]tt[e] is an unknown quantity at this point 
Solved Threads: 0
N[e]tt[e] N[e]tt[e] is offline Offline
Newbie Poster

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

 
0
  #10
Jul 15th, 2006
use String Tokenizer
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC