Results 1 to 3 of 3
  1. #1
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default How Iterate through a String composed of multiple Strings of unknown length

    I am making modifications to a previous program to prevent the client class from having direct access to the data file. I want to send the data file to the client class in a giant String. Since the file is going to be of varying lengths, I am having trouble coming up with a strategy in order to iterate through the string like I was when I was reading directly from the file using the while(information!= null) approach. When reading the file into the giant String, I am inserting a ';' in between lines in ordrer to split the data line by line. The problem I am having is not knowing how long the string is.

    I need something to the effect of

    while(String is not at the end)
    info = information.nextToken;

    or

    for(int i =0; int < words in string; i++)
    info = information.nextToken;

    There has to be an easy way to do this but I am drawling a blank. Thanks for the help! A sample of the data file to be put into the large String is below:

    Java Code:
    F:723:sofa:Victorian:599.99:23
    F:750:chair:Victorian:799.99:23
    F:455:table:Jacobean:1899.99:37
    F:854:desk:Modern:599.99:25
    M:23:Gavigan's LLC:1024 Maple Ave:Mechanicsville:MD:20723:(443)-987-6543
    M:37:Shoffer's Furniure:23 Charles St:Baltimore:MD:20125:(410)-123-4567
    M:25:Autofab Inc:7443 Washington Blvd:Elkridge:MD:21075:(301)-555-1234
    F:7231:sofa:New Victorian:599.99:232
    F:7502:chair:New Victorian:799.99:232
    F:4553:table:Old Jacobean:1899.99:372
    F:8544:desk:Modern:599.99:25
    M:232:John's Furniture LLC:1024 Highview Ave:Glen Burnie:MD:21725:(443)-787-6543
    M:372:Sarah's Furniure:7223 Pratt St:Baltimore:MD:20125:(410)-123-4997
    M:252:BuildIt Inc:5543 Delaware Blvd:Annapolis:MD:21175:(301)-566-1234

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: How Iterate through a String composed of multiple Strings of unknown length

    You do not need to know String length. If all your Strings have been separated by a semi-colon then you can use the String.split method to split on the semi-colon. This will return an array with each line as a separate element of the array.

  3. #3
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: How Iterate through a String composed of multiple Strings of unknown length

    Thanks that's what I was looking for! got it working!

Similar Threads

  1. How to declare array of Unknown Length for below code
    By sandeep43 in forum New To Java
    Replies: 10
    Last Post: 08-24-2011, 01:31 PM
  2. Replies: 11
    Last Post: 08-15-2011, 10:12 AM
  3. Replies: 2
    Last Post: 06-10-2011, 07:15 AM
  4. String.length?
    By rizowski in forum New To Java
    Replies: 7
    Last Post: 11-20-2010, 05:33 PM
  5. string length HELP PLZ!!
    By vette427 in forum Eclipse
    Replies: 1
    Last Post: 09-23-2010, 06:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •