Results 1 to 7 of 7
- 11-20-2010, 02:13 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
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.
- 11-20-2010, 02:19 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Not being mean but can you please post what you have attempted?
- 11-20-2010, 02:28 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
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
- 11-20-2010, 02:55 AM #4
have you learned about StringTokenizer yet? or perhaps the split() method of the String class.
where you might be able to
but I don't think that would satisfy the requirement to inject an empty string when there are two or more spaces.Java Code:public static String[] buildArrayFromText(String text) { String [] pieces = text.split(" "); return pieces; }
- 11-22-2010, 12:38 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
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
-
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.
- 11-22-2010, 01:38 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
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
-
[SOLVED] Reading a text file into an Array
By DonCash in forum New To JavaReplies: 13Last Post: 01-25-2011, 12:51 AM -
How to write text file into Array
By venkat.ravala in forum New To JavaReplies: 13Last Post: 11-19-2009, 04:59 PM -
Reading text file into an array
By Mahesh_ps in forum New To JavaReplies: 1Last Post: 10-09-2009, 03:04 PM -
GUI help - Text Box Defaults to Array Value
By ejs7597 in forum New To JavaReplies: 8Last Post: 03-31-2009, 05:48 AM -
Export an array to a text file
By carderne in forum New To JavaReplies: 32Last Post: 10-02-2008, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks