Results 1 to 2 of 2
  1. #1
    arson09 is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default str.trim().split("\\s+") help

    I'm trying to split a string into individual words, but is having problems doing it.. You can skip the passage below for the only reason i'm posting it is for the sake of showing what's being printed out when i try and use it.
    Please forgive the lost passage, but this is the file i'm reading from:

    The International Space Station is proving to be very valuable for
    scientific research. It provides a platform to conduct experiments
    that require one or more of the unusual conditions present on the
    station. The primary fields of research include human studies, space
    medicine, life sciences, physical sciences, astronomy, and
    meteorology. The 2005 NASA Authorization Act designated the American
    segment of the International Space Station as a national laboratory
    with the goal of increasing the use of the International Space Station
    by other federal agencies and the private sector. For this reason, the
    agency has entered into a number of partnerships with various private
    enterprises. Research on the International Space Station improves
    knowledge about the effects of long-term space exposure on the human
    body. Subjects currently under study include muscle atrophy, bone
    loss, and fluid shift, with an emphasis on fatigue and endurance. The
    data will be used to determine whether space colonization and lengthy
    human spaceflight are feasible. As of 2006, data on bone loss and
    muscular atrophy suggest that there would be a significant risk of
    fractures and movement problems if astronauts landed on a planet after
    a lengthy interplanetary cruise (such as the six-month journey time
    required to fly to Mars). Large scale medical studies are conducted
    aboard the International Space Station via the National Space and
    Biomedical Research Institute. Prominent among these is the Advanced
    Diagnostic Ultrasound in Microgravity study in which astronauts
    (including former International Space Station Commanders Leroy Chiao
    and Gennady Padalka) perform ultrasound scans under the guidance of
    remote experts. For their work on this project, the two commanders
    were awarded NASA Service Commendations. The study considers the
    diagnosis and treatment of medical conditions in space. Usually, there
    is no physician onboard the International Space Station and diagnosis
    of medical conditions is a challenge. It is anticipated that remotely
    guided ultrasound scans, with the use of some of the equipment
    developed for the station, will have application on Earth in emergency
    and rural care situations where access to a trained physician is
    difficult. Researchers are investigating the effect of the station's
    near-weightless environment on the evolution, development, growth, and
    internal processes of plants and animals. In response to some of this
    data, NASA wants to investigate microgravity's effects on the growth of
    three-dimensional, human-like tissues, and the unusual protein crystals
    that can be formed in space. The investigation of the physics of
    fluids in microgravity will allow researchers to model the behavior of
    fluids better. Because fluids can be almost completely combined in
    microgravity, physicists are investigating fluids that do not mix well
    on Earth. In addition, an examination of reactions that are slowed by
    low gravity and temperatures will give scientists a deeper
    understanding of superconductivity. With that knowledge, improvements
    in electronics and computers are expected. The study of materials
    science is an important element of the research activity, with the
    objective of reaping economic benefits through the improvement of
    techniques used on the ground. Other areas of interest include the
    effect of the low gravity environment on combustion, through the study
    of the efficiency of burning and control of emissions and pollutants.
    These findings may improve our knowledge about energy production, and
    lead to economic and environmental benefits. Future plans are for the
    researchers aboard the International Space Station to examine aerosols,
    ozone, water vapor, and oxides in the Earth's atmosphere, as well as
    cosmic rays, cosmic dust, antimatter, and dark matter in the universe.
    With the impending retirement of the space shuttle fleet, it is very
    important that this proposed research be initiated in the next one to
    two years. For additional reading, see the NASA Web-site or visit your
    local library.


    When i use the str.trim().slit("\\s+"), this is what i'm getting:

    The
    International
    scientific
    research.
    that
    require
    station.
    The
    medicine,
    life
    meteorology.
    The
    segment
    of
    with
    the
    by
    other
    agency
    has
    enterprises.
    Research
    knowledge
    about
    body.
    Subjects
    loss,
    and
    data
    will
    human
    spaceflight
    muscular
    atrophy
    fractures
    and
    a
    lengthy
    required
    to
    aboard
    the
    Biomedical
    Research
    Diagnostic
    Ultrasound
    (including
    former
    and
    Gennady
    remote
    experts.
    were
    awarded
    diagnosis
    and
    is
    no
    of
    medical
    guided
    ultrasound
    developed
    for
    and
    rural
    difficult.
    Researchers
    near-weightless
    environment
    internal
    processes
    data,
    NASA
    three-dimensional,
    human-like
    that
    can
    fluids
    in
    fluids
    better.
    microgravity,
    physicists
    on
    Earth.
    low
    gravity
    understanding
    of
    in
    electronics
    science
    is
    objective
    of
    techniques
    used
    effect
    of
    of
    the
    These
    findings
    lead
    to
    researchers
    aboard
    ozone,
    water
    cosmic
    rays,
    With
    the
    important
    that
    two
    years.
    local
    library.

    As u can see, it's not getting everyword in each line.. it's skipping something terrible.. this is where i'm trying to do it at..

    Java Code:
    //Open the file for reading
    
          Scanner fsc = new Scanner(infile);
          FileInputStream fstream = new FileInputStream(infile);
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
    
    
        //Read the data in the file line by line
            while((passage = br.readLine()) != null)
          {
           	 System.out.println(passage);
             str[num_lines]= fsc.nextLine();
             num_lines ++;
          }
           	fsc.close();
    // Count the total number of words in passage
          for(i=0; i<num_lines; i++)
          {
           the_words = str[i].trim().split("\\s+");
    
           sum += the_words.length;
           System.out.println(the_words[0]);
           System.out.println();
          }

    The only reason i used the_words[0] was for the sake of seeing what was stored in that particular element..
    Last edited by arson09; 05-03-2010 at 06:39 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default

    Don't use a Scanner and an BufferedReader to read from one single file at the same time; change your algorithm to something like this:

    Java Code:
    BufferedReader br= ...;
    String line;
    while ((line= br.readLine()) != null)
       process(line);
    Your process( ... ) method processes the entire file line by line; you can use your split( ... ) trick in that method on each and every line.

    kind regards,

    Jos

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  2. Replies: 2
    Last Post: 10-09-2009, 12:28 AM
  3. [SOLVED] split(&quot;\\d&quot;)
    By vysh in forum Advanced Java
    Replies: 3
    Last Post: 05-21-2009, 10:06 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •