Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By milovan

Thread: Printing, reading, operators

  1. #1
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Printing, reading, operators

    Hello,
    I am new to java and I am supposed to get an output like the following:

    Type in your first name: John


    Hi, "John"
    The name of this exercise is 'Modifying printing'.
    Tabulator, line change and quotations have already been used in this exercise.
    Backward slash (\) is also usable in printing.


    *** End of exercise ***
    But I couldn't get the name part, John, within "". Please any one help me. My code looks as follows:

    ------------------------------------------------------------------------------------------------------------------------------------------------
    import java.util.Scanner;
    public class apples {
    public static void main(String[] args) {
    String s;
    String m;
    Scanner reader= new Scanner(System.in);
    System.out.print("Type in your first name: ");
    s= reader.nextLine();
    m="Forward slash (\\) is also usable in printing.";
    System.out.println();
    System.out.println();
    System.out.println("Hi, "+s);
    System.out.println("\tThe name of this exercise is 'Modifying printing'.");
    System.out.println("\tTabulator, line change and quotations have already been used in this exercise.");
    System.out.println("\t"+m);
    System.out.println();
    System.out.println();
    System.out.println("\t\t*** End of exercise ***");
    }
    }

    ------------------------------------------------------------------------------------------------------------------------------------------------

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Printing, reading, operators

    To embed a double quote within a string, precede it with a backslash.
    Java Code:
    System.out.println("\"John\"");
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Re: Printing, reading, operators

    Hi Jim,
    Thanks for the reply but it is supposed to scan(get) any name and print it
    within " " like Hi, "John", Hi "David" etc
    Regards,
    Daniel

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Printing, reading, operators

    I showed you how to do that. Escape the double quote with a backslash (\). I am not certain what you are asking.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Printing, reading, operators

    The \ is called an escape character.

    Normally, when you use a quotation mark (") inside of a System.out.println, it sees it as starting or ending a string.

    However, by placing a \ in front of the ", the program knows to ignore the "functionality" of that character and simply display it.

    Try running this, and figure out the answer for your program:
    Java Code:
    class test {
    public static void main ( String[] args ) {
    System.out.println("Here is a simple \"test\"!);
    }
    }

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Printing, reading, operators

    Java Code:
    String name = "danielki";
    System.out.println("\"" + name + "\"");

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Printing, reading, operators

    Which is in essence what I and others have told the OP. That one needs to use a \ to embed double quotes within Strings.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Printing, reading, operators

    Yes, yes.. this is just addition to yours posts because OP asked again...
    AlexGraal likes this.

Similar Threads

  1. Replies: 1
    Last Post: 03-15-2013, 10:04 AM
  2. Reading .txt contents and printing them to java GUI
    By hinkey1 in forum AWT / Swing
    Replies: 1
    Last Post: 09-30-2012, 06:41 AM
  3. Replies: 6
    Last Post: 01-19-2012, 05:16 PM
  4. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  5. reading lines from URL and printing backwards
    By luxurymode in forum Advanced Java
    Replies: 1
    Last Post: 03-30-2011, 08:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •