10 Replies Latest reply: Mar 30, 2007 7:20 AM by 800282 RSS

    splitting strings

    807599
      i have a list of information e.g.

      junction blue2
      segment UniversityDrive University blue2 0.3 10
      segment BluebellRd2 blue1 blue2 0.11 20
      segment BluebellRd3 blue2 blue3 0.42 20
      busline Line25 University blue2 blue3 colman2 Unthank
      busline Line26 University blue2 blue1 colman1 roundabout Earlham

      i want to be able to take certain information from certain lines e.g.
      i need all of the first line, only the second and third words of lines two, three and four.

      i was thinking of splitting the lines into tokens, then using the system.out.println to print them.

      is this a good way, hows could i do this?
        • 1. Re: splitting strings
          807599
          You can also use StringTokenizer. Take a look at this

          http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html
          • 2. Re: splitting strings
            807599
            split method should take care of it.
            try {
            FileReader fr = new FileReader("book.txt");
                           BufferedReader br = new BufferedReader(fr);
                           String s = br.readLine();
            while(s!=null)
                           {
                           String[] str = s.split( " + "); // or s.spilt("\\s");
             s=br.readLine();
            }
            }
            catch (Exception e) {
            e.printStackTrace();
            }
            Message was edited by:
            fastmike

            Message was edited by:
            fastmike
            • 3. Re: splitting strings
              807599
              yeh but how would i take the individual elements from a line?
              • 4. Re: splitting strings
                807599
                You will iterate through the array. Can you get elements from an array?
                • 5. Re: splitting strings
                  807599
                  yeh i can get elements from an array, but how would that work?
                  • 6. Re: splitting strings
                    807599
                    Well based on what you said earlier:
                    i want to be able to take certain information from
                    certain lines e.g.
                    i need all of the first line,
                    You will keep the first line you read as is
                    only the second and
                    third words of lines two, three and four.
                    Then call StringTokinzer or splite and get those values into array and then travers the array and get what you want. You know the location of those items.
                    • 7. Re: splitting strings
                      807599
                      i have tried sumthing like this:
                       String[] a = line.split(" ");
                               System.out.println(a[2]);
                      but it is producing an ArrayIndexOutOfBoundsException, will this create a new array for each line, or just make one big array?
                      • 8. Re: splitting strings
                        800282
                        i have tried sumthing like this:

                        String[] a = line.split(" ");
                        System.out.println(a[2]);

                        but it is producing an
                        ArrayIndexOutOfBoundsException, will this create a
                        new array for each line, or just make one big array?
                        It will only split line into an array of Strings. The array a will get recreated every time split(...) is being called on line.
                        • 9. Re: splitting strings
                          807599
                          how could i print certain elements in the the array a?
                          • 10. Re: splitting strings
                            800282
                            how could i print certain elements in the the array a?
                            Iterate over you array a from 0 to a.lenght-1 and print each element in a.