0
import java.util.Scanner;
public class InputTest {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter your first name : ")
        String Answer = input.next();
        System.out.println(Answer + " is your first name.");
    }

}

if the user inputs John,

the output will say : John is your first name.

My question : How can I change the output to "John" instead of John.

Crypto
  • 60
  • 1
  • 10
  • 3
    Possible duplicate of [How can I make Java print quotes, like "Hello"?](http://stackoverflow.com/questions/3844595/how-can-i-make-java-print-quotes-like-hello) – OneCricketeer Apr 21 '17 at 15:15
  • Edit : Please read the question before marking is as "duplicate". I'm not trying to print out my own string with double quotes. I'm trying to do it with the user's input. "\"scanner\"" will print out "scanner" and not "blah". – Crypto Apr 21 '17 at 15:22
  • `scanner` is **just a string**. You replace `Hello` there with your variable – OneCricketeer Apr 21 '17 at 15:34
  • I'm trying to output the user's input with double quotes in the output. **not my own string**. The link that you provided as a duplicate does not show how to do what I'm trying to do. – Crypto Apr 21 '17 at 15:41
  • `String scanner = "Hello";` Is no different than `String scanner = input.nextLine();`... It is simply the concept of the variable that you seem to be missing :/ – OneCricketeer Apr 21 '17 at 16:03
  • For example, http://stackoverflow.com/questions/3559063/how-to-enter-quotes-in-a-java-string – OneCricketeer Apr 21 '17 at 16:06
  • I understand your point now, sorry for the my confusion. I thought they were different things. Thank you. – Crypto Apr 21 '17 at 16:17

3 Answers3

3

Try this

System.out.print("\"" + scanner + "\"" + "is not a valid command. Please try again : ");

This escapes the " by using \.

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
1
String scanner = "\"" + input.nextLine() + "\"";
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
0

The other Answers are correct, using an escape sequence in Java syntax. But escaping can get tiresome and annoying.

Alternate characters.

You could use alternate characters such as “curly” quotes or «guillemets». See Wikipedia.

No need for escaping with these characters as they are not used in Java syntax.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154