Results 1 to 7 of 7
  1. #1
    marinavaries is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default please help with method to build array from text

    Hi can someone please help me im really new to java programming and im really confused on how to do this. I need to create a program where users can enter a new text and then display that text as an array and then allow them to find and replace something in that text.

    THis is what i need to do for the first method and i am unsure how to begin this::
    buildArrayFromText - This method should build and return an array of Strings where the contents should be all pieces of text separated by space characters. Note that if you find two space characters in a row in the text, then you should place an empty string at that index in the array. The same goes for the first and last entries in the array. If the first or last character is a space, an empty String should go in the array at that location.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Not being mean but can you please post what you have attempted?

  3. #3
    marinavaries is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    public static String[] buildArrayFromText(String text)
    {
    String input = keyboard.nextLine();
    String currentText = input;
    String [0] = currentText;
    return new String[0];
    }


    sorry i didnt post this because its probably horribly in correct

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    have you learned about StringTokenizer yet? or perhaps the split() method of the String class.
    where you might be able to
    Java Code:
    public static String[] buildArrayFromText(String text)
    {
      String [] pieces = text.split(" ");
      return pieces;
    }
    but I don't think that would satisfy the requirement to inject an empty string when there are two or more spaces.

  5. #5
    marinavaries is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    thanks so much for your input i have resolved it to this:

    public static String[] buildArrayFromText(String text)
    {

    String str = text;
    return str.split (" ");

    }

    however it doesnt return a empty string in the array when i enter an extra space at the end of the text--but this code does return an empty string in the array for a space before the text entered or an extra space between text

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You can use regular expressions in the split method to be able to split in a variety of ways. For instance "\\s+" will split on any whitespace. Also, there are overloads of the split method that can help you fine tune its results.

  7. #7
    marinavaries is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    thanks for replying but actually thats not what i meant, i meant that i want it to skip only one space and if theres more then on space together-one of them should show up in the array printout for this method:

    public static String rebuildTextFromArray(String[] array)
    {

    String newText = "";

    for (int index = 0; index < array.length; index++)
    {
    newText = newText + array[index] + " ";
    }
    return newText;
    }

Similar Threads

  1. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  2. How to write text file into Array
    By venkat.ravala in forum New To Java
    Replies: 13
    Last Post: 11-19-2009, 04:59 PM
  3. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:04 PM
  4. GUI help - Text Box Defaults to Array Value
    By ejs7597 in forum New To Java
    Replies: 8
    Last Post: 03-31-2009, 05:48 AM
  5. Export an array to a text file
    By carderne in forum New To Java
    Replies: 32
    Last Post: 10-02-2008, 03:52 PM

Tags for this Thread

Posting Permissions

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