Solved

Read String part and store in array.

Posted on 2006-06-27
4
236 Views
Last Modified: 2010-03-31
Hi, I have in.readLine() function give me the following string:
 "143586371377.0'0'1'asdf'6'EDITOR@WEB'0502'20060310'23532'1411.92'ALL'0.06'EFTO'AS400234234''0'0.06#"

I try to read in the 143586371377.0 and store in element[0], 0 in element[1] and so forth, basically it store the element between the single quote into array.  If I have 2 single quote such as '' then i would store NULL.

My fields has many of these and they each start in a new line such as  (though with different content ofcourse) with # denote end of line, I can remove the # as well if needed.

 143586371377.0'0'1'asdf'6'EDITOR@WEB'0502'20060310'23532'1411.92'ALL'0.06'EFTO'AS400234234''0'0.06#
 143586371377.0'0'1'asdf'6'EDITOR@WEB'0502'20060310'23532'1411.92'ALL'0.06'EFTO'AS400234234''0'0.06#
 143586371377.0'0'1'asdf'6'EDITOR@WEB'0502'20060310'23532'1411.92'ALL'0.06'EFTO'AS400234234''0'0.06#
 143586371377.0'0'1'asdf'6'EDITOR@WEB'0502'20060310'23532'1411.92'ALL'0.06'EFTO'AS400234234''0'0.06#
0
Comment
Question by:fylix0000
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 150 total points
ID: 16993554
String str = "143586371377.0'0'1'asdf'6'EDITOR@WEB'0502'20060310'23532'1411.92'ALL'0.06'EFTO'AS400234234''0'0.06#";
ArrayList<String> arr = new ArrayList<String>();
for(int s = 0, e = 0; s < str.length(); ) {
      e = str.indexOf("'", s);
      if(e == -1)
            e = str.length();
      String token = str.substring(s, e);
      arr.add(token.length() == 0 ? null : token);
      s = e + 1;
}
System.out.println(arr);
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 16993647
String[] tokens = line.split("'");
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 200 total points
ID: 16993798
You may use split method to do the job.


            String[] x = line.split("'");
            for (int y = 0; y < x.length; y++) {
                  if (x[y] == null || x[y].length() == 0)
                        System.out.println("NULL");
                  else
                        System.out.println(x[y]);
            }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16996153
:-)

Actually i'm not sure it's even possible to get a null token, but you should probably eat up any slack space, so you'd probably be better doing:

 if (x[y] == null || (x[y] = x[y].trim()).length() == 0)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 177
login form jsp example 2 37
Bot application - advice 3 38
maven not picking latest jar instead picking old jar from .m2 12 22
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question