Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read String part and store in array.

Posted on 2006-06-27
4
Medium Priority
?
242 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 600 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 200 total points
ID: 16993647
String[] tokens = line.split("'");
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 800 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

916 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