Results 1 to 5 of 5
- 09-11-2009, 06:40 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
strings stringbuilders replace and reverse
Hello, I have to write a class and a program that utilizes it.
the program prompts user for a search string, then prompts the user for a line of text to search through until the user chooses to stop (by pressing enter). The program will then output the text line with the occurances of the search string replaced by a string of astericks '***', and the reverse of the search string replaced with a string of exclamation marks '!!!'.
[eg:]
Please enter search string (all letters, no spaces): abcda
Please enter a line of text to search (enter to exit):
This is an example of a line of text where adcba and abcda are used.
This is an example of a line of text where !!!!! and ***** are used.
Please enter a line of text to search (enter to exit):
abcdabcdabcdabcda
*****bcd*****bcda
Please enter a line of text to search (enter to exit):
fghabcd afgh a b c d a jkl;asdfabcdadcbadcbadcbadcbajkl;
fghabcd afgh a b c d a jkl;asdf*****dcb!!!!!dcb!!!!!jkl;
Please enter a line of text to search (enter to exit):
adcbadcba adcbabcda
!!!!!dcba adcb*****
Please enter a line of text to search (enter to exit):
[/eg]
I havent used String Objects before let alone manipulating strings. I know I have to have a default constructor(and the program will termanate when no arguements are passed to searchString or textLine), set search string and text line string, convert search string into a stringbuilder, reverse it, and replace search string and its reverse.
But everything I have been trying is not working (the code is a mess). I have no idea what the syntax rules are when im using string references instead of entering string literals into the string buffer.
Thank you anyone who can help.
- 09-11-2009, 08:26 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Read the whole of this section of Sun's tutorial.
- 09-12-2009, 04:00 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
Thank you, Thank you!
That's better, now.. if i can get it to compile i'll be getting places.
I used the example there, to put into my class (has to be separate to the main program) but its not accepting the for statements.
14 errors all with the for sections. (probably other problems too but i cant get rid of these ones to see).Java Code:public class StringManip { private String searchString; public StringManip(String s) { searchString = s; } public void setSearch(String s) { searchString = s; } int len = searchString.length(); char[] searchArray = new char[len]; char[] reverseArray = new char[len]; //make string chars for (int i = 0; i < len; i++) // errors { searchArray[i] = searchString.charAt[i]; } // reverse it for (int j = 0; j < len; j++) //errors { reverseArray[j] = searchArray[len - 1 - j]; } String reverseSearch = new String(reverseArray); String searchString = new String(searchArray); public String getSearch() { return searchString; } public String getReverse() { return reverseSearch; } }
Thanks anyone who can help.Last edited by bicepatron; 09-12-2009 at 05:38 AM.
- 09-12-2009, 08:12 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You need to learn that error messages give you a clue as to what is wrong in your programs. You need to read them carefully to see what the clues are.
If you still don't get what they mean then post some of them here.
- 09-13-2009, 01:17 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
ok im so close to getting this.
But i cant for the life of me, initialize the searchString and textLine Strings in the class and have the separate program set keyboard input to it.
Ive tried creating String variables in the separate program, and sending those as arguements to the other classes constructor like i did with primitive data types.. (sorry example is gone now)
Ive tried sending the keyboard input to the methods of the class via stating:
Ive tried adding the prompt into the methods in the class itself eg:Java Code:public static void main(String[] args) { //new class object Duplo2 test = new Duplo2(); //prompts System.out.print("\n Please enter search string (all letters," + " no spaces): "); test.setSearch(keyboard.nextLine()); System.out.println(" Please enter a line of text to search" + " (enter to exit) : "); test.setText(keyboard.nextLine()); //rest of code is not shown.
any help would blow my mind.Java Code:import java.util.Scanner; // Needed for Keyboard input. public class Duplo2 { String searchString = null, textLine = null; public void setSearch() { Scanner keyboard = new Scanner(System.in); // Output a prompt to the display for the user System.out.println("\n Please enter search string (all letters," + " no spaces): "); searchString = keyboard.nextLine(); } public void setText() { Scanner keyboard = new Scanner(System.in); // Output a prompt to the display for the user System.out.println(" Please enter a line of text to search" + " (enter to exit) : "); textLine = keyboard.nextLine(); // rest of code is not shown
thanks in advance.
Similar Threads
-
Reverse a string not using the substring method
By kathyla18 in forum New To JavaReplies: 17Last Post: 04-08-2009, 04:08 AM -
how do i reverse this method for sorting?Again!
By PureAwesomeness in forum New To JavaReplies: 2Last Post: 03-09-2009, 12:51 AM -
Reverse and Replace a String in Linear Time
By colin.cruise in forum New To JavaReplies: 5Last Post: 07-01-2008, 09:02 PM -
How to reverse two dimensional
By masaka in forum New To JavaReplies: 4Last Post: 05-19-2008, 10:02 AM -
Reverse engineer a java code
By lenny in forum Advanced JavaReplies: 1Last Post: 07-25-2007, 11:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks