0

My teacher wants us to make a letter 'o' move around the console. The letter 'o' has been coded to appear in the center of the console screen. I have already created the movingRight and movingDown methods but I'm having difficulty creating the movingLeft and movingUp methods. Here is my code:

import java.util.Scanner;

public class Main {

static String letter = "\n\n\n\n                                                                               O";
String whenmovingup = letter.substring(0, 1);
char whenmovingleft = letter.charAt(letter.length() - 2);

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print(letter);
    input.nextLine();

    if (input.equals("left")) {
        movingLeft();
    }
    if (input.equals("right")) {
        movingRight();
    }
    if (input.equals("up")) {
        movingUp();
    }
    if (input.equals("down")) {
        movingDown();
    }

}

    public static void movingRight(){
        letter = " " + letter;
    }
    public static void movingDown(){
        letter = "\n" + letter;
    }
    public static void movingLeft(){
        letter.remove(whenmovingleft);
    }
    public static void movingUp(){
        letter.remove(whenmovingup);
    }
}

I'm having an issue with removing the whenmovingfeft and whenmovingup substrings from my original string letter. It's giving an error ('The method remove(char) is undefined for the type String'), and I'm not sure what needs to be done.

Does anyone know how this can be resolved? Thanks in advance for all responses.

7
  • 2
    "It's giving me errors" - what errors, exactly, are you getting? Please edit the error messages into your question. Commented Jul 23, 2014 at 23:45
  • Thanks for your help Greg. The pop-up tells me that this is an invalid way of using a String type. Commented Jul 23, 2014 at 23:47
  • 'The method remove(char) is undefined for the type String' Commented Jul 23, 2014 at 23:49
  • There's no remove method for Strings. If you want to delete one character from a String, and you know the position of the character you wnat to delete, you basically need to use substring, either once or twice. Commented Jul 23, 2014 at 23:49
  • There's now a new error. 'Type mismatch: cannot convert from char to String' Commented Jul 23, 2014 at 23:52

1 Answer 1

1

There is no remove method for a string. However, there is a replace method that may do what you want. Note that it does not modify the string object, but it returns a new string. So you would do:

letter = letter.replace(whenmovingup, "");

Note that there are two slightly different overloads of replace which do different things depending on whether you ask it to remove a String or char. The replace(String, String) method replaces one occurrence, while the replace(char, char) replaces all occurrences. You want just one, so declare whenmovingleft as a String and initialise it appropriately.

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

4 Comments

Whenever I try and change the whenmovingup declaration to a string, I get this error message: 'Type mismatch: cannot convert from char to String'
Yes, as I mentioned you'll have to initialise it appropriately, with a String instead of a char. You successfully initialise whenmovingup with a String, so do something similar for whenmovingleft.
Is there a .stringat function? However else I rewrite this declaration gives me the same error as before.
Just as in the line above, use letter.substring(...), which returns a String.

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.