0
public class Dene {

    private char ch;
    private Dene next;

    public Dene(char c) {
        this.ch = c;
    }

    public void add(Dene next) {
        this.next = next;
    }

    public boolean isWriteable(String s) {

        if (this.next == null) {
            if (s.contains(Character.toString(this.ch))) {
                return true;
            } else {
                return false;
            }
        } else {
            return this.next.isWriteable(s);
        }
    }
}


public static void main(String[] args) {
    Dene d = new Dene('a');
    Dene e = new Dene('e');
    Dene f = new Dene('f');
    d.add(e);
    e.add(f);
    System.out.println(d.IsWriteable("afb"));
}

IsWriteable gets a string as a parameter and sees recursively if its possible to write that string from the chars which are connected in the linked list.. but it is not working..any ideas?

7
  • what is the aim of isWritable method?? Commented Feb 4, 2014 at 20:10
  • Dene is a start node of a linked list.isWritable gets a string and checks if this string is writeable from the chars which linked list has.. Commented Feb 4, 2014 at 20:14
  • Define it is not working: does not compile, does compile but you got an exception when running it, runs but gives unexpected results? Commented Feb 4, 2014 at 20:14
  • Please edit your question and show a sample of expected results and your current result. Commented Feb 4, 2014 at 20:18
  • Watch out: I changed return this.x.IsWriteable(s); to return this.x.isWriteable(s); in my edit, because the first one didn't compile. See if it has anything to do with your problem! Commented Feb 4, 2014 at 20:18

2 Answers 2

2

At first I had Problems to understand your code. In my opinion you should create a isWriteable(char c) function, so you can check a character recursively:

public bool isWriteable(char c){
    if (this.x == null){
        return c == this.ch;
    else {
        return this.ch == c && this.x.isWriteable(c);
    }
}

To check a string you just have to check every character of your string.

Update

Added the code of character checking:

public bool isWriteable(String s){
    char[] chars = s.toCharArray();
    int i;
    char c;
    for (i = 0; i < chars.length; i++){
        if (!isWriteable(c)){
            return false;
        }
    }
    return true;
}

(i hope this is correct because i havent used Java for some time now)

Update I saw i could have made it so much easier:

public bool isWriteable(String s){
    if (this.x == null){
        return s.contains(this.ch);
    }
    else {
        return this.x.isWriteable(s) & s.contains(this.ch);
    }
}

This is recursive and serves its purpose.

Sign up to request clarification or add additional context in comments.

5 Comments

but in my opinion i rewrote the isWriteable function that recursively checks if the string can be put together from the chars of the linked list
but what i need is recursion
this is given in the first code Segment: isWriteable(char) calls isWriteable(char) in line 5. See edited code, you can use the last code Segment or the other 2, both should work well.
I have to admit that i first did not understand why you use String.contains(char) and tried to make it my own way to check every char recursively. So my solution was way different from what you expected and that made it hard to understand my first 2 segments of code.
thanks for the answer...this line really rocks :) return this.x.isWriteable(s) & s.contains(this.ch);
0

It looks like you are lacking open and closing braces to clearly define what belongs in each if/else block. As a general rule even though Java allows you not to, you should always put an open and closing brace on if statements and also loops to make your code easier to read, follow, and debug.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.