Solved

Read String part and store in array.

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now