-1

Below is my code:

String str = "something";
char ch = 'e';
int noOfOcc = 1;
System.out.println("No of occurrences of '"+ch+"' in "+str+" is "+noOfOcc);

Expected output should be as follows:

No of occurrences of 'e' in "something" is 1

How can I print that something in double quotes? I need solution for this case where we are using variables in System.out.println();. and I need the value of that variable in double quotes.

2 Answers2

2

Escape it using \" so that the complier knows not to treat your " as something to demarcate the start or end of a String.

ifly6
  • 5,003
  • 2
  • 24
  • 47
0

This way:

System.out.println("No of occurrences of '"+ch+"' in \""+str+"\" is "+noOfOcc);

Backward slash acts as escape sequence to print double quotes.
Compiler don't interpret the character after slash symbol.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
Bhargav Kumar R
  • 2,190
  • 3
  • 22
  • 38