Results 1 to 5 of 5

Thread: Builder

  1. #1
    AgentApe is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default Builder

    I've constructed a method that combines the use of a string tokeniser and string builder to translate words in a string to other words. This method though just seems to produce errors. Can someone take a look at this method and please explain what it is I'm doing wrong.

    Java Code:
     public void String replacePirate(String +pirate+, String +english+) {    (class, interface or enum expected)
        StringBuilder result = new StringBuilder(original.length());
        String delimiters = "+-*/(),. ";
        StringTokenizer st = new StringTokenizer(+pirate+, delimiters, true);
        while (st.hasMoreTokens()) {
            String w = st.nextToken();
            if (w.equals(find)) {
                result.append(+english+);
            } else {
                result.append(w);
            }
        }
        return result.toString();
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    public void String replacePirate(String +pirate+, String +english+) {
    I think you mean:

    Java Code:
    public String replacePirate(String pirate, String english) {

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by AgentApe View Post
    Java Code:
     public void String replacePirate(String [COLOR="Red"]+[/COLOR]pirate[COLOR="#ff0000"]+[/COLOR], String [COLOR="#ff0000"]+[/COLOR]english[COLOR="#ff0000"]+[/COLOR]) {    (class, interface or enum expected)
        StringBuilder result = new StringBuilder(original.length());
        String delimiters = "+-*/(),. ";
        StringTokenizer st = new StringTokenizer([COLOR="Red"]+[/COLOR]pirate[COLOR="#ff0000"]+[/COLOR], delimiters, true);
        while (st.hasMoreTokens()) {
            String w = st.nextToken();
            if (w.equals(find)) {
                result.append([COLOR="#ff0000"]+[/COLOR]english[COLOR="#ff0000"]+[/COLOR]);
            } else {
                result.append(w);
            }
        }
        return result.toString();
    }
    Why do you have "+" sign around your parameter names? That could be causing one of the problems.

    Another problem might be that the variables "original" and "find" are never declared. I would presume these are fields in your class, but just checking.
    Last edited by CodesAway; 12-02-2009 at 09:18 PM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  4. #4
    AgentApe is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    I have fixed what you have said I shoudl and still get the same error. Hmm.

  5. #5
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    OK, after actually reading your code, your method has two return types "void", and "String". Since you want to return a String, you should change the return type to String.

    As for the error, I presume the method is inside a Class, as Java requires.

    Java Code:
    import java.util.StringTokenizer;
    
    public class Test
    {
    	public String replacePirate(String pirate, String english)
    	{
    		StringBuilder result = new StringBuilder(original.length());
    		String delimiters = "+-*/(),. ";
    		StringTokenizer st = new StringTokenizer(pirate, delimiters, true);
    		while (st.hasMoreTokens()) {
    			String w = st.nextToken();
    			if (w.equals(find)) {
    				result.append(english);
    			} else {
    				result.append(w);
    			}
    		}
    		return result.toString();
    	}
    }
    For the above code, I only get two errors, which is that "original" and "find" are never declared.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Replies: 1
    Last Post: 08-12-2009, 03:34 PM
  2. Parsing XML using Document Builder Factory
    By Sangram123 in forum XML
    Replies: 1
    Last Post: 08-01-2009, 02:15 PM
  3. Formula Builder
    By rbs100 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2009, 07:57 PM
  4. Eclipse GUI Builder
    By elebal in forum Eclipse
    Replies: 1
    Last Post: 10-23-2008, 12:38 PM
  5. Help with NetBeans 5 GUI builder
    By Daniel in forum NetBeans
    Replies: 2
    Last Post: 07-04-2007, 08:08 AM

Posting Permissions

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