Results 1 to 13 of 13
- 07-14-2012, 08:25 PM #1
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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?:
What do I type instead?Java Code:System.out.println("In the first line of the text, Tom said "Pickles"");
I am a Java beginner, sorry for such a simple question.
- 07-14-2012, 08:31 PM #2
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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."); } }
- 07-14-2012, 08:33 PM #3
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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."); } }
- 07-14-2012, 09:52 PM #4
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
Re: Can Java print speech marks or brackets?
Try something like this
System.out.println("\"hello\"");
- 07-16-2012, 12:09 AM #5
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can Java print speech marks or brackets?
ah, so:
/" = " ?
does that mean /( = ( ?
and /) = ) ?
- 07-16-2012, 12:32 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
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
would give you the desired results you wantedJava Code:System.out.println("In the first line of the text, Tom said \"Pickles\"");
- 07-16-2012, 12:33 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Can Java print speech marks or brackets?
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.
- 07-18-2012, 04:36 PM #8
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can Java print speech marks or brackets?
so \" does mean " when within a string?
does this work for brackets too?
- 07-18-2012, 04:45 PM #9
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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?
- 07-18-2012, 05:04 PM #10
Member
- Join Date
- Jul 2012
- Posts
- 55
- Rep Power
- 0
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 ( \\\" )
- 07-18-2012, 06:55 PM #11
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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
- 07-18-2012, 07:00 PM #12
Member
- Join Date
- Jul 2012
- Posts
- 55
- Rep Power
- 0
Re: Can Java print speech marks or brackets?
That is correct (ie with \n using newline)
- 07-18-2012, 10:24 PM #13
Re: Can Java print speech marks or brackets?
Get the list of special characters, straight from the horse's mouth: Chapter*3.*Lexical Structure
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Java Speech API
By JavaCy in forum New To JavaReplies: 1Last Post: 04-29-2011, 05:09 PM -
Java method that check brackets in String are matched?
By neonz in forum New To JavaReplies: 21Last Post: 03-02-2011, 03:34 PM -
Marks programm
By blackmoon in forum AWT / SwingReplies: 5Last Post: 12-08-2009, 10:06 PM -
Retrieving double quotation marks from xml using java
By fireportal in forum New To JavaReplies: 3Last Post: 11-12-2009, 11:13 AM -
Help with Java Speech API (Synthesizer)
By mohitkhanna3v_infinate in forum Advanced JavaReplies: 1Last Post: 07-23-2009, 06:21 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks