0

I must write a constructor that takes in a 27-letter sequence and a char that "rotates" the char sequence until the first char in the sequence matches the given char. The resulting string is then saved in the class as a field.

For example: If I were to use #GNUAHOVBIPWCJQXDKRYELSZFMT and U, I expect the constructor to give me UAHOVBIPWCJQXDKRYELSZFMT#GN.

My constructor can't find the rotate method I wrote. I had a similar problem with one of my other methods, the peek() method, so I deleted the method and used the code inside the constructor, but I want to practice good OOP principles so I don't want to do the same here.

public class Rotor{
    private String rotor;

    public Rotor(String rotor, char top){
        this.rotor = rotor;
        while(this.rotor.charAt(0)!=top){
            rotor.rotate();
        }
    }
    //rotates rotor one click clockwise
    public void rotate(){
        this.rotor = this.rotor.substring(this.rotor.length()-1, this.rotor.length()) + this.rotor.substring(0, this.rotor.length()-1);
    }

    public char peek(){
        return rotor.charAt(0);
    }
    public String toString(){
        return rotor;
    }
    public static void main(String[] args){
        Rotor rotor = new Rotor("#GNUAHOVBIPWCJQXDKRYELSZFMT", 'U');
        System.out.println(rotor.toString());
        
    }
}

    Rotor.java:7: error: cannot find symbol
                rotor.rotate();
                     ^
      symbol:   method rotate()
      location: variable rotor of type String
    1 error
peteru
  • 130
  • 1
  • 11
  • 1
    That's because in the context of line 7, rotor is a String, not a Rotor. String does not have a rotate method. The error says "variable rotor of type String" does not have a symbol "rotate". And I don't see how this question has anything to do with "enigma2". Looks like it should have the tag "java". I think you might get further if it becomes this.rotate() instead of rotor.rotate(). – Jeff Holt Oct 24 '21 at 23:06
  • Jeff, you're awesome! I was working on debugging this and you're right. I was trying to rotate the rotor but assuming the string inside the rotor was the rotor. I tried this.rotate() and works like magic. – Brigitta Szepesi Oct 24 '21 at 23:10
  • Does this answer your question? [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – Mark Rotteveel Apr 29 '22 at 10:04

0 Answers0