Solved

Read String part and store in array.

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 96
oracle 11g 23 84
Oracle SQL syntax check  without executing 6 56
Java: The Public Class Main 4 17
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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

809 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