Solved

Read String part and store in array.

Posted on 2006-06-27
4
234 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the latest versions eclipse neon 2 139
Requested array size exceeds VM limit 3 84
Java Loop 6 48
servlet filter example 37 34
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now