Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By subzero1012
  • 1 Post By pbrockway2

Thread: Can Java print speech marks or brackets?

  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    2

    Default Can Java print speech marks or brackets?

    I want to print some speech marks and brackets inside using println but I keep getting errors when compiling.

    For example, if I wanted to print:
    In the first line of the text, Tom said "Pickles"
    Why can't I type?:
    Java Code:
    System.out.println("In the first line of the text, Tom said "Pickles"");
    What do I type instead?

    I am a Java beginner, sorry for such a simple question.

  2. #2
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    2

    Default Re: Can Java print speech marks or brackets?

    This is the original program I wrote which won't work because of it:

    Java Code:
    class MathExamples {
    
    public static void LineSpace() {
    System.out.println(" ");
    }
    
    public static void main(String[] args) {
    System.out.println("To write PI in java, we use "Math.PI" inside the brackets of print functions (without any speech marks)");
    System.out.println("The code for the line below is "System.out.println(Math.PI)"");
    System.out.println(Math.PI);
    LineSpace();
    System.out.println("To raise something to a power of another number, we use "Math.pow(x, y)", where x is the number and y is it's power");
    System.out.println("The code for 2 to the power of 3 is "Math.pow(2, 3)", the result of this is printed below");
    System.out.println(Math.pow(2, 3));
    LineSpace();
    System.out.println("These work because both PI and pow are methods which are defined in the Math class. This is a class which is installed on any system which has java.");
    System.out.println("The "Math" part of "Math.xxx" states the class that the information is found in. The "xxx" part specifies which method should be used from that class. They could be thought of as directories to files.");
    }
    
    }

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    2

    Default Re: Can Java print speech marks or brackets?

    This is the version which works but it then gives grammatical errors and readability issues when ran:

    Java Code:
    class MathExamples {
    
    public static void LineSpace() {
    System.out.println(" ");
    }
    
    public static void main(String[] args) {
    System.out.println("To write PI in java, we use Math.PI inside the brackets of print functions (without any speech marks)");
    System.out.println("The code for the line below is System.out.println(Math.PI)");
    System.out.println(Math.PI);
    LineSpace();
    System.out.println("To raise something to a power of another number, we use Math.pow(x, y), where x is the number and y is it's power");
    System.out.println("The code for 2 to the power of 3 is Math.pow(2, 3), the result of this is printed below");
    System.out.println(Math.pow(2, 3));
    LineSpace();
    System.out.println("These work because both PI and pow are methods which are defined in the Math class. This is a class which is installed on any system which has java.");
    System.out.println("The Math part of Math.xxx states the class that the information is found in. The xxx part specifies which method should be used from that class. They could be thought of as directories to files.");
    }
    
    }

  4. #4
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default Re: Can Java print speech marks or brackets?

    Try something like this

    System.out.println("\"hello\"");
    awinston likes this.

  5. #5
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    2

    Default Re: Can Java print speech marks or brackets?

    ah, so:

    /" = " ?

    does that mean /( = ( ?

    and /) = ) ?

  6. #6
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Can Java print speech marks or brackets?

    Hi,

    It's actually a backslash that allows you to do double quotations within a println statement.

    So, for example

    Java Code:
    System.out.println("In the first line of the text, Tom said \"Pickles\"");
    would give you the desired results you wanted

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Can Java print speech marks or brackets?

    Quote Originally Posted by kkid View Post
    ah, so:

    /" = " ?

    does that mean /( = ( ?

    and /) = ) ?
    Read carefully! It's a \.

    Using a " within a quoted string would cause the compiler no end of trouble. (Once it hits the first " how would it ever know when the string had ended?). This sort of problem is common when writing expressions for a computer and the solution is to use a character (\ is very common) to mean "the following character is special". \ is known as an "escape character" and the quote symbol is said to be "escaped" in the string literal.

    The exact rules for escaping things vary from situation to situation, but for Java string literals they are described in the Java Language Specification in the section 3.10.5. String Literals. That description, or just playing about, should answer your further question, but say if not.
    kammce likes this.

  8. #8
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    2

    Default Re: Can Java print speech marks or brackets?

    so \" does mean " when within a string?

    does this work for brackets too?

  9. #9
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    2

    Default Re: Can Java print speech marks or brackets?

    what if you actually wanted to type \"?

    would you then type \\"?

    and if you wanted to type \\", you would use \\\"?

    i.e. you use one more \ than needed?

  10. #10
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Can Java print speech marks or brackets?

    Yes if you put a backslash (\) before a special character in a string, then it denotes the character following the backslash. Here is an example of some the special characters that need backslashes before them \" \' \\ . So if you wanted to display ( \" ) you would need a backslash before both special characters, therefore looking like ( \\\" )

  11. #11
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    2

    Default Re: Can Java print speech marks or brackets?

    oh, right, so it just works in pairs?

    If single, the character uses its "special" function
    If in a pair with a backslash, the backslash will change the character from one performing its "special" function into a character which is just a normal character?

    I understand now, thanks

  12. #12
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Can Java print speech marks or brackets?

    That is correct (ie with \n using newline)

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Can Java print speech marks or brackets?

    Get the list of special characters, straight from the horse's mouth: Chapter*3.*Lexical Structure

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java Speech API
    By JavaCy in forum New To Java
    Replies: 1
    Last Post: 04-29-2011, 05:09 PM
  2. Replies: 21
    Last Post: 03-02-2011, 03:34 PM
  3. Marks programm
    By blackmoon in forum AWT / Swing
    Replies: 5
    Last Post: 12-08-2009, 10:06 PM
  4. Retrieving double quotation marks from xml using java
    By fireportal in forum New To Java
    Replies: 3
    Last Post: 11-12-2009, 11:13 AM
  5. Help with Java Speech API (Synthesizer)
    By mohitkhanna3v_infinate in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2009, 06:21 PM

Posting Permissions

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