Results 1 to 5 of 5
  1. #1
    bicepatron is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Exclamation 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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read the whole of this section of Sun's tutorial.

  3. #3
    bicepatron is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    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.

    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; 
        }
    }
    14 errors all with the for sections. (probably other problems too but i cant get rid of these ones to see).
    Thanks anyone who can help.
    Last edited by bicepatron; 09-12-2009 at 05:38 AM.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  5. #5
    bicepatron is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    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:
    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.
    Ive tried adding the prompt into the methods in the class itself eg:
    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
    any help would blow my mind.
    thanks in advance.

Similar Threads

  1. Reverse a string not using the substring method
    By kathyla18 in forum New To Java
    Replies: 17
    Last Post: 04-08-2009, 04:08 AM
  2. how do i reverse this method for sorting?Again!
    By PureAwesomeness in forum New To Java
    Replies: 2
    Last Post: 03-09-2009, 12:51 AM
  3. Reverse and Replace a String in Linear Time
    By colin.cruise in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 09:02 PM
  4. How to reverse two dimensional
    By masaka in forum New To Java
    Replies: 4
    Last Post: 05-19-2008, 10:02 AM
  5. Reverse engineer a java code
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 07-25-2007, 11:14 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
  •