Bolor Ch
Bolor Ch

Reputation: 29

Can not compile my code

I want to print this sign /\ but on the compiler, i think everything is fine but can not compile. error appeared as

leets.java:13: error: unclosed string literal
System.out.printf ("%s /\",ch);
                     ^
 leets.java:13: error: ';' expected
 System.out.printf ("%s /\",ch);
                     ^
 2 errors

My code is below.

import java.util.Scanner;
public class switchDemo2
{
    public static void main ( String args[] )
    {
    Scanner i = new Scanner ( System.in );
    System.out.print ( "Enter a character to test: " );
    char ch;
    ch = i.next().charAt(0);
    switch ( ch )
    {
    case 'A': case 'a':
    System.out.printf ("%s /\",ch);
                       break;
    case 'B': case 'b':
        System.out.printf ("%s 13",ch);
    break;
    case 'C': case 'c':
    System.out.printf ("%s )",ch);
    break;
    case 'D': case 'd':
    System.out.printf ("%s 1)",ch);
    break;
    case 'E': case 'e':
    System.out.printf ("%s 3",ch);
    break;
    case 'F': case 'f':
    System.out.printf ("%s 1=",ch);
    break;
    default:
    System.out.printf ("%s not a lowercase vowel\n",ch);
}
}

Upvotes: 1

Views: 636

Answers (4)

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

You need to escape backslashes as .

 System.out.printf ("%c /\\",ch);

You can see more information about it here.

Few comments :

  1. You used "%s" for characters, you can use "%c for characters. See formatting tutorial.
  2. You did not close the scanner, you will get so warning. You should close it using Scanner.close() method.
  3. Last curly brace } is remaining.

Upvotes: 4

Rohit Jain
Rohit Jain

Reputation: 213261

Backslashes needs to be escaped: -

System.out.printf ("%c /\\",ch);

And you can change your: - switch ( ch ) to switch(Character.toLowerCase(ch)), to avoid having cases for both A and a. Just use case 'a':

Upvotes: 0

dinukadev
dinukadev

Reputation: 2297

This should work for you;

System.out.printf ("%s '/\\'",ch);

Regards,

Dinuka

Upvotes: 0

Amit Deshpande
Amit Deshpande

Reputation: 19185

System.out.printf ("%s /\\",ch);

Instead of System.out.printf ("%s /\",ch);

 Escape Sequence    Description
 \t     Insert a tab in the text at this point.
 \b     Insert a backspace in the text at this point.
 \n     Insert a newline in the text at this point.
 \r     Insert a carriage return in the text at this point.
 \f     Insert a formfeed in the text at this point.
 \'     Insert a single quote character in the text at this point.
 \"     Insert a double quote character in the text at this point.
 \\     Insert a backslash character in the text at this point

Here is the Tutorial on characters in java

Upvotes: 1

Related Questions