Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Trying to compile a java code and it is giving me a weird error... all the code seems right any ideas?

public class Chap1Prob4

{

public static void main(String []args)

{

System.out.println(" /\_/\ ----- ");

System.out.println("( ' ' ) / Hello \ ");

System.out.println("( - ) < Junior |");

System.out.println(" | | | \ Coder!/ ");

System.out.println("(__|__) ----- ");

}

}

The error it is giving me is Chap1Prob4.java:5: error: illegal escape character

System.out.println(" /\_/\ ----- ");

^

Chap1Prob4.java:5: error: illegal escape character

System.out.println(" /\_/\ ----- ");

^

Chap1Prob4.java:6: error: illegal escape character

System.out.println("( ' ' ) / Hello \ ");

^

Chap1Prob4.java:8: error: illegal escape character

System.out.println(" | | | \ Coder!/ ");

^

4 errors

i thought it was just supposed to print whatever was in the "" and just not check it for coding errors but for some reason when i try to compile it so it shows that it sais error...

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    "Literal Strings" are interpreted internally regarding some special characters. The newline \n character is one. But the backslash \ is another. It means to treat the following character specially or literally (the opposite of its normal treatment).

    That means that inside "a quoted string" every backslash must be preceded by another backslash in order to be taken a a literal backslash.

    So, in your kitty's case, double each backslash to get one backslash.

    System.out.println (" /\\_/\\ ----- ");

    System.out.println ("( ' ' ) / Hello \\ ");

    System.out.println ("( - ) < Junior |");

    System.out.println (" | | | \\ Coder!/ ");

    System.out.println ("(__|__) ----- ");

    Added: The sequence backslash+something is sometimes called an "escape character", because you are escaping the normal bounds of what the 'something' character means.

  • 9 years ago

    First answer is correct.

    The

    \

    character 'escapes' what comes after. If you want to print that character you have to 'escape' it by

    \\

    that will print the \ char.

    Eg change

    System.out.println(" | | | \ Coder!/ ");

    to

    System.out.println(" | | | \\ Coder!/ ");

  • Anonymous
    5 years ago

    I have a good time including your efforts to lear yet you've began on the incorrect aspect..... Please bypass and look at java applications and the artwork of this methodology in the previous you commence away different sensible.........................;;;;;; import java.awt.*; import java.util.*; public classification DateProgram { public static void significant(String[]args) { //int day; //int month; //int 365 days; Date at present = new Date(); //at present.day = 06; //at present.month = a million; //at present.365 days = 2009; device.out.println(at present); } }

Still have questions? Get your answers by asking now.