Difference between revisions of "Output using System.out."

From CompSciWiki
Jump to: navigation, search
m (Removed extra </code> tag)
 
(42 intermediate revisions by 10 users not shown)
Line 1: Line 1:
Java uses System.out. for program output.
+
{{Template:1010Topic
 +
|Chapter_TOC=[[Java Fundamentals]]
 +
|Previous=[[Comments]]
 +
|Next=[[Input/Output using JOptionPane|Output using JOptionPane]]
 +
|Body=
  
==Printing text to the screen==
+
==Introduction==
To print text to the screen you can use:
+
Programs would not be very interesting if they could not interact with the user.  This section covers how to use the Java <code>System.out</code> function to display a message to the user within a console window.  We will start off by displaying general text, then move on to combining text and numbers to create complex output.
  
'''System.out.println("text");'''
+
==Giving the User Information==
  
or
+
[[Image:Console.png|left]]  One method of interacting with a user is through the console, which is also known as command line interface. Java can write to the console using methods within the System package. Sending information to the user through the command line interface is a lot like printing to paper. You tell the computer what to print and then it prints your message to the console for the user to read.
  
'''System.out.print("text");'''
+
The System package contains two methods that allows you to write a [[Strings|String]] to the console window: <code>System.out.print</code> and <code>System.out.println</code>.
  
Whatever you put inside of the quotes will be printed to the screen. The difference between System.out.println() and System.out.print() is that System.out.println() will print a newline after the text unlike System.out.print() which just prints the text.
+
===<code>System.out.print</code>===
  
===Example 1===
+
The <code>System.out.print</code>[[http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#print(java.lang.String) ]] method call is used to write a [[Strings|String]] to the console window.  As you can see in the code block below, to write to the console all you need to do is put the [[Strings|String]] inside the brackets.  When we look at the output however, we see that the command line prompt is right next to the text we are trying to show the user - this makes it hard to read.  Although there are situations where this is beneficial, it would be better to use <code>System.out.println</code>.
This program prints the message "Go Bisons!" to the screen.
+
<pre>
+
public class Output1
+
{
+
  public static void main(String args[])
+
  {
+
    System.out.println("Go Bisons!");
+
  }
+
}
+
</pre>
+
  
===Example 2===
+
====Code====
This program prints the message "Go Bisons!" to the screen three times.
+
{{CodeBlock
<pre>
+
|Code=
public class Output2
+
public class PrintDemo
 
{
 
{
  public static void main(String args[])  
+
    public static void main(String args[])  
  {
+
    {
    System.out.println("Go Bisons!");
+
        System.out.print("Hello World");
    System.out.println("Go Bisons!");
+
        System.out.print("Hello World");
    System.out.println("Go Bisons!");
+
        System.out.print("Hello World");
  }
+
    }
 
}
 
}
</pre>
+
}}
'''Output'''
+
<pre>
+
Go Bisons!
+
Go Bisons!
+
Go Bisons!
+
</pre>
+
  
===Example 3===
+
====Output====
This program also prints the message "Go Bisons!" to the screen three times. However, it does not print a new line after each message.
+
 
<pre>
+
{{OutputBlock
public class Output3
+
|Code=
 +
C:\>javac PrintDemo.java
 +
C:\>java PrintDemo
 +
Hello WorldHello WorldHello World
 +
C:\>
 +
}}
 +
 
 +
===<code>System.out.println</code>===
 +
 
 +
The <code>System.out.println</code>[[http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println()]] method call is almost identical to the <code>System.out.print</code> method, except it add a line terminator, new line character, to the end of the [[Strings|String]].  As you can see in the code below it is used exactly the same way.  In the output, each [[Strings|String]] appears on its own line.
 +
 
 +
====Code====
 +
 
 +
{{CodeBlock
 +
|Code=
 +
public class PrintDemo
 
{
 
{
  public static void main(String args[])  
+
    public static void main(String args[])  
  {
+
    {
    System.out.print("Go Bisons!");
+
        System.out.println("Hello World");
    System.out.print("Go Bisons!");
+
        System.out.println("Hello World");
    System.out.print("Go Bisons!");
+
        System.out.println("Hello World");
  }
+
    }
 
}
 
}
</pre>
+
}}
  
'''Output'''
+
====Output====
<pre>
+
Go Bisons!Go Bisons!Go Bisons!
+
</pre>
+
  
==Using Variables with System.out.println()==
+
{{OutputBlock
You can also print the contents of a variable using System.out.println()
+
|Code=
 +
C:\>javac PrintDemo.java
 +
C:\>java PrintDemo
 +
Hello World
 +
Hello World
 +
Hello World
 +
C:\>
 +
}}
  
===Example 4===
+
==Printing More Than Just Text in Quotes==
<pre>
+
Now that you know how the two methods work, here are some examples demonstrating how to print more than one kind of argument. Remember, ultimately, the type of the argument in the print methods must always be a [[Strings|String]]. Even though you probably haven't worked much with variables you can still get the general idea here.
public class PrintVariable
+
 
 +
{{CodeBlock
 +
|Code=
 +
public class PrintNum
 
{
 
{
  public static void main(String args[])  
+
    public static void main(String args[])  
  {
+
    {
    int num;
+
        int num = 5;
 +
        System.out.println("The number is " + num);
 +
    }
 +
}
 +
}}
  
    num = 5;
+
{{OutputBlock
 +
|Code=
 +
C:\>javac PrintNum.java
 +
C:\>java PrintNum
 +
The number is 5
 +
C:\>
 +
}}
  
    System.out.println(num);
+
There are a couple key things going on here. First of all, the argument contains a [[Strings|String]] in quotes, but also an [[Common Primitive Variables|int]] variable. Secondly there is a plus sign between the two. These two things work together to make things happen.
  }
+
}
+
</pre>
+
  
'''Output'''
+
Firstly, the plus sign is telling the compiler to join the two items, the [[Strings|String]] in quotes and the [[Common Primitive Variables|int]] variable, and together they makeup a single argument for the println method. Secondly, the println method exclusively takes a [[Strings|String]] argument. What happens is, by using the plus sign to tell the compiler to join the two items, you are also telling it to implicitly [[Casting|cast]] the [[Common Primitive Variables|int]] variable to its [[Strings|String]] value. For all [[Common Primitive Variables|int]]'s this is just its value (i.e. 5 is "5" and -987654321 is "-987654321" and so on). The [[Common Primitive Variables|int]] variable is first cast to a [[Strings|String]] so it is allowed to be joined to another [[Strings|String]], and together they form a single [[Strings|String]] argument for the print method.
<pre>
+
5
+
</pre>
+
  
In this example we create an integer called '''num''' and use it to store the number '''5'''. We then print the contents of '''num''' by using System.out.println(). Notice that we did not put quotes around '''num'''. Variables do not need to be put in quotes. Only a string of text needs to be put inside of quotes.
+
You can join as many items together as you wish, however it quickly becomes unreadable.
  
===Example 5===
+
The next likely question is what if we want to print a number, but no words? How do we convert the int to a [[Strings|String]]? The answer involves a little trick as shown below:
In this example we will show you how to print a string of text and a variable.
+
  
<pre>
+
{{CodeBlock
public class PrintTemp
+
|Code=
 +
public class PrintNum
 
{
 
{
  public static void main(String args[])  
+
    public static void main(String args[])  
  {
+
    {
    int temp;
+
        int num = 99;
 +
        System.out.println("" + num);
 +
    }
 +
}
 +
}}
  
    temp = 25;
+
{{OutputBlock
 +
|Code=
 +
C:\>javac PrintNum.java
 +
C:\>java PrintNum
 +
99
 +
C:\>
 +
}}
  
     System.out.println("The temperature is currently " + temp);
+
This code works exactly the same way, except this time we don't have anything between our quotes. Why do we need this? Why can't we just have the [[Common Primitive Variables|int]] part? Well if we just gave the println method an int argument, it would give us an error because it only takes [[Strings|String]]. By including the empty [[Strings|String]] "" we are telling the compiler we want to [[Casting|cast]] the [[Common Primitive Variables|int]] variable to a [[Strings|String]] again, but this time we want to join it with nothing, we want the number by itself.
  }
+
 
 +
{{CodeBlock
 +
|Code=
 +
public class PrintRocks
 +
{
 +
     public static void main(String args[])
 +
    {
 +
        String text = "Comp 1010 Rocks!";
 +
        System.out.println(text);
 +
    }
 
}
 
}
</pre>
+
}}
  
'''Output'''
+
{{OutputBlock
<pre>
+
|Code=
The temperature is currently 25
+
C:\>javac PrintRocks.java
</pre>
+
C:\>java PrintRocks
 +
Comp 1010 Rocks!
 +
C:\>
 +
}}
  
===Example 6===
+
The two above examples of code would produce identical results. There are several ways to give the print methods a [[Strings|String]] argument. We can define it in a [[Strings|String]] variable, like the first example, and give it the variable, or we can define it right in the argument itself as in the second example.
We can also put the variable in the middle of a sentence.
+
<pre>
+
public class PrintTemp
+
{
+
  public static void main(String args[])
+
  {
+
    int temp;
+
  
    temp = 25;
 
  
    System.out.println("The temperature is currently " + temp + " degrees Celsius.");
+
More information on creating Strings expressions can be found in the [[Strings#String_Expressions|String]] section.
  }
+
}
+
</pre>
+
  
'''Output'''
 
<pre>
 
The temperature is currently 25 degrees Celsius.
 
</pre>
 
  
We use a '''+''' sign to join a string of text and a variable.
+
==Summary==
 +
In this section you have learned how to write text to the console using <code>System.out</code>.  But not all users want to see information in a console window.  In the next section you will learn how to create a message box to display a message to the user using <code>JOptionPane</code>.
 +
}}

Latest revision as of 22:39, 7 December 2011

COMP 1010 Home > Java Fundamentals


Introduction

Programs would not be very interesting if they could not interact with the user. This section covers how to use the Java System.out function to display a message to the user within a console window. We will start off by displaying general text, then move on to combining text and numbers to create complex output.

Giving the User Information

Console.png
One method of interacting with a user is through the console, which is also known as command line interface. Java can write to the console using methods within the System package. Sending information to the user through the command line interface is a lot like printing to paper. You tell the computer what to print and then it prints your message to the console for the user to read.

The System package contains two methods that allows you to write a String to the console window: System.out.print and System.out.println.

System.out.print

The System.out.print[[1]] method call is used to write a String to the console window. As you can see in the code block below, to write to the console all you need to do is put the String inside the brackets. When we look at the output however, we see that the command line prompt is right next to the text we are trying to show the user - this makes it hard to read. Although there are situations where this is beneficial, it would be better to use System.out.println.

Code

 public class PrintDemo
{
    public static void main(String args[]) 
    {
        System.out.print("Hello World");
        System.out.print("Hello World");
        System.out.print("Hello World");
    }
} 

Output

 C:\>javac PrintDemo.java
C:\>java PrintDemo
Hello WorldHello WorldHello World
C:\> 

System.out.println

The System.out.println[[2]] method call is almost identical to the System.out.print method, except it add a line terminator, new line character, to the end of the String. As you can see in the code below it is used exactly the same way. In the output, each String appears on its own line.

Code

 public class PrintDemo
{
    public static void main(String args[]) 
    {
        System.out.println("Hello World");
        System.out.println("Hello World");
        System.out.println("Hello World");
    }
} 

Output

 C:\>javac PrintDemo.java
C:\>java PrintDemo
Hello World
Hello World
Hello World
C:\> 

Printing More Than Just Text in Quotes

Now that you know how the two methods work, here are some examples demonstrating how to print more than one kind of argument. Remember, ultimately, the type of the argument in the print methods must always be a String. Even though you probably haven't worked much with variables you can still get the general idea here.

 public class PrintNum
{
    public static void main(String args[]) 
    {
        int num = 5;
        System.out.println("The number is " + num);
    }
} 
 C:\>javac PrintNum.java
C:\>java PrintNum
The number is 5
C:\> 

There are a couple key things going on here. First of all, the argument contains a String in quotes, but also an int variable. Secondly there is a plus sign between the two. These two things work together to make things happen.

Firstly, the plus sign is telling the compiler to join the two items, the String in quotes and the int variable, and together they makeup a single argument for the println method. Secondly, the println method exclusively takes a String argument. What happens is, by using the plus sign to tell the compiler to join the two items, you are also telling it to implicitly cast the int variable to its String value. For all int's this is just its value (i.e. 5 is "5" and -987654321 is "-987654321" and so on). The int variable is first cast to a String so it is allowed to be joined to another String, and together they form a single String argument for the print method.

You can join as many items together as you wish, however it quickly becomes unreadable.

The next likely question is what if we want to print a number, but no words? How do we convert the int to a String? The answer involves a little trick as shown below:

 public class PrintNum
{
    public static void main(String args[]) 
    {
        int num = 99;
        System.out.println("" + num);
    }
} 
 C:\>javac PrintNum.java
C:\>java PrintNum
99
C:\> 

This code works exactly the same way, except this time we don't have anything between our quotes. Why do we need this? Why can't we just have the int part? Well if we just gave the println method an int argument, it would give us an error because it only takes String. By including the empty String "" we are telling the compiler we want to cast the int variable to a String again, but this time we want to join it with nothing, we want the number by itself.

 public class PrintRocks
{
    public static void main(String args[]) 
    {
        String text = "Comp 1010 Rocks!";
        System.out.println(text);
    }
} 
 C:\>javac PrintRocks.java
C:\>java PrintRocks
Comp 1010 Rocks!
C:\> 

The two above examples of code would produce identical results. There are several ways to give the print methods a String argument. We can define it in a String variable, like the first example, and give it the variable, or we can define it right in the argument itself as in the second example.


More information on creating Strings expressions can be found in the String section.


Summary

In this section you have learned how to write text to the console using System.out. But not all users want to see information in a console window. In the next section you will learn how to create a message box to display a message to the user using JOptionPane.

Previous Page: Comments Next Page: Output using JOptionPane