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" ;

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:

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

Looking at your string I assume that the different parts of the string are
seperate4d by a TAB character. If so do the following:

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

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

No problem. And have fun.

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

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);

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?

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

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

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

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]);

Thanks everyone for your help.

AB

use String Tokenizer

hello my dears.
here i have text and i want to tokenize and the tokenized words to be listed in the table having columns with rank of words and fequency of words i have a doubit with my code and please offer your help

So start a new thread as this is a completely different question. P.S. that advice about StringTokenizer is not worth listening to. StringTokenizer has big problems with non-ASCII character sets among other limitations and drawbacks. It is effectively deprecated.

Killing this 4 year dead zombie now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.