Instructables

How to write useful programs in JAVA

Step 4: Make it say something

ok, so you already have the code

class Pythagorean {
public static void main(String[] args){
}
}

right?
good
now make it say
class Pythagorean {
public static void main(String[] args){

String greeting = "Hello. Welcome to the Pythagorean Theorem Application. Today we will be computing the Pythagorean Theorem for you.";

String prompt = "Please enter a number for the first leg of a right triangle, followed by pressing ENTER or RETURN, followed by the remaining leg. Then hit ENTER or RETURN one more time.";

System.out.println(greeting);
System.out.println(prompt);
}
}

let's break it down!
A string is any line of text
this is a variable: it can vary
duh
greeting and prompt are the names of the strings
so in the line
String greeting;
you are creating a string called greeting
note the semicolon
all statements in java should end in a semicolon or else ERROR
you use = to assign the value to the variable
this can be done like this:

String greeting = "Hello. Welcome to the Pythagorean Theorem Application. Today we will be computing the Pythagorean Theorem for you.";

or like this:
String greeting;
greeting = "Hello. Welcome to the Pythagorean Theorem Application. Today we will be computing the Pythagorean Theorem for you.";

in this case you are declaring and assigning the value of the variable in two steps
in the former case you are doing in one
both work equally well

all the text that you want in the variable must be put it "quotes"

the line System.out.println call for the SYSTEM OUTPUT to PRINT the LINE of the variable
if you called System.out.println("HI");
the terminal would print
HI
but note the quotes
the quotes say that what's inside is text, not the name of a variable
if you called System.out.println(HI);
the terminal would respond with an error because it thinks you wanted to print the value of the variable HI
memorize this command: you'll use it all the time

 
Remove these adsRemove these ads by Signing Up
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!