| | |
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:
Solved Threads: 0
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" ;
to
String category = "BARTHROOM TOI" ;
String sku = "48157" ;
String tmp = "Y" ;
String description "DOVE BWASH....." ;
etc..
etc...
Any help would be much appreciated
Andy.
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)
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.
Looking at your string I assume that the different parts of the string are
seperate4d by a TAB character. If so do the following:
seperate4d by a TAB character. If so do the following:
Java Syntax (Toggle Plain Text)
String line = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ; String[] parts = line.split("\\t"); //parts[0] = "BATHROOM TOI"; //parts[1] = "48157"; // 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
----------------------------------------------
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
•
•
Join Date: Jul 2006
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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)
String line = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ; String[] parts = line.split("\\t"); //parts[0] = "BATHROOM TOI"; //parts[1] = "48157"; // etc. etc
Thank dude!!!
- AB 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
----------------------------------------------
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
•
•
Join Date: Jul 2006
Posts: 5
Reputation:
Solved Threads: 0
This is a rough draft solution to tha problem. (if your are inferested) Thanks for your help! 

Java Syntax (Toggle Plain Text)
String description = null ; String cat = null ; String sku = null ; String temp = null ; String qty = null ; String newLine = null ; ArrayList list = new ArrayList() ; // different combinations. String line = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ; //String line = "DEODORANTS 9609 REXONA R/ON 50ML ICE COOL 6" ; // String line = "FRAGRANCE 16696 Y CK ESCAPE EDP 30ML 2" ; // String line = "DEODORANTS 9609 REXONA R/ON 50ML ICE COOL 6 NEW" ; String[] parts = line.split(" "); for (int i=0; i < parts.length ; i++) { if (parts[i].length() != 0) { list.add(parts[i]) ; } } // remove surounding white space - if any for (int i=0; i< list.size() ; i++) { String ob = (String) list.get(i) ; ob = ob.trim() ; if (ob.charAt(0) == ' ') { int finish = ob.length() ; String newString = ob.substring(1, finish); ob = newString ; } list.remove(i); list.add(i,ob); switch (i) { case 0: cat = ob ; break ; case 1: sku = ob ; break ; case 2: { if (ob.equalsIgnoreCase("Y")){ temp = "Y" ; } else { description = ob ; } } break ; case 3: { if (description == null) { description = ob; } else { qty = ob ; } } break ; case 4: { if (qty == null) { qty = ob ; } else if (ob.equalsIgnoreCase("NEW")) { newLine = ob ; } } break ; case 5: { if (ob.equalsIgnoreCase("NEW")) { newLine = ob ; } } break ; } // end swith } // end for i System.out.println(line); System.out.println(""); System.out.println("Cat: " + cat); System.out.println("Sku:" + sku); System.out.println("Temp " + temp); System.out.println("Description:" + description); System.out.println("Qty: " + qty); System.out.println("NewLine: " + newLine);
•
•
Join Date: Mar 2004
Posts: 765
Reputation:
Solved Threads: 38
well split() does take a regular expression, so why not just use that?
It looks for 2 or more spaces to be the delimiter.
It looks for 2 or more spaces to be the delimiter.
Java Syntax (Toggle Plain Text)
String s = "BATHROOM TOI 48157 Y DOVE BWASH MCDAMIA 375ML 6 NEW" ; String[] tokens = s.split(" {2,}?"); for(int i=0; i<tokens.length; i++) System.out.println(tokens[i]);
Last edited by Phaelax; Jul 9th, 2006 at 10:43 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: i need help in this
- Next Thread: I've got another problem
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






