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?:
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.
Re: Can Java print speech marks or brackets?
This is the original program I wrote which won't work because of it:
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.");
}
}
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:
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.");
}
}
Re: Can Java print speech marks or brackets?
Try something like this
System.out.println("\"hello\"");
Re: Can Java print speech marks or brackets?
ah, so:
/" = " ?
does that mean /( = ( ?
and /) = ) ?
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
Code:
System.out.println("In the first line of the text, Tom said \"Pickles\"");
would give you the desired results you wanted
Re: Can Java print speech marks or brackets?
Quote:
Originally Posted by
kkid
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.
Re: Can Java print speech marks or brackets?
so \" does mean " when within a string?
does this work for brackets too?
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?
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 ( \\\" )
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
Re: Can Java print speech marks or brackets?
That is correct (ie with \n using newline)
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