Jump to content

Java Programming/Basic IO: Difference between revisions

From Wikibooks, open books for an open world
[checked revision][checked revision]
Content deleted Content added
m Use standard {{Nav}} instead of {{Programming/Navigation}}
m Update syntaxhighlight tags - remove use of deprecated <source> tags
 
Line 18: Line 18:
{| style="width: 100%"
{| style="width: 100%"
|{{XExample|1='''Code section 9.1: Print "Hello world" without advancing to a new line'''
|{{XExample|1='''Code section 9.1: Print "Hello world" without advancing to a new line'''
<source lang="Java">
< lang="Java">
System.out.print("Hello world");
System.out.print("Hello world");
</source>}}
</>}}
|
|
{{XConsole|1='''Output on the screen'''
{{XConsole|1='''Output on the screen'''
Line 28: Line 28:
|-
|-
|{{XExample|1='''Code section 9.2: Print "Hello world" and advance to a new line'''
|{{XExample|1='''Code section 9.2: Print "Hello world" and advance to a new line'''
<source lang="Java">
< lang="Java">
System.out.println("Hello world");
System.out.println("Hello world");
</source>}}
</>}}
|
|
{{XConsole|1='''Output on the screen'''
{{XConsole|1='''Output on the screen'''
Line 43: Line 43:


{{XExample|1='''Code section 9.3: Inputting with <code>Scanner</code>'''
{{XExample|1='''Code section 9.3: Inputting with <code>Scanner</code>'''
<source lang="Java">
< lang="Java">
Scanner kbdIn = new Scanner(System.in); // Instantiating a new Scanner object
Scanner kbdIn = new Scanner(System.in); // Instantiating a new Scanner object
System.out.print("Enter your name: "); // Printing out the prompt
System.out.print("Enter your name: "); // Printing out the prompt
Line 49: Line 49:
// and putting it in a String variable called name
// and putting it in a String variable called name
System.out.println("Welcome, " + name); // Printing out welcome, followed by the user's name
System.out.println("Welcome, " + name); // Printing out welcome, followed by the user's name
</source>}}
</>}}


{{XConsole|1='''On the screen'''
{{XConsole|1='''On the screen'''
Line 60: Line 60:


{{XExample|1='''Code section 9.4: Line reader'''
{{XExample|1='''Code section 9.4: Line reader'''
<source lang="Java">
< lang="Java">
public String readLine() {
public String readLine() {
// Creates a new BufferedReader object
// Creates a new BufferedReader object
Line 68: Line 68:
return x.readLine();
return x.readLine();
}
}
</source>}}
</>}}
Note that the code above shouldn't be used in most applications, as it creates new Objects every time the method is run.
Note that the code above shouldn't be used in most applications, as it creates new Objects every time the method is run.
A better alternative would be to create a separate class file to handle keyboard input.
A better alternative would be to create a separate class file to handle keyboard input.

Latest revision as of 05:39, 16 April 2020

Topics:
Navigate User Interface topic:v  d  e )


This section covers the Java platform classes used for basic input and output. But before we begin we need to have a concrete understanding of what input and output means in programming. To grasp this concept, think of the Java platform as a system.

Understanding input and output

[edit | edit source]

The Java platform is an isolated entity, a space on your OS in a way, where everything outside this system is its environment. The interaction between the system and its environment is a two-way dialog of sorts. Either the system receives messages from its environment, or it conveys its messages to the same. When a message is received by the system, it is called an input, its opposite is an output. On a whole, this communication is termed input/output abbreviated as I/O.

The following chapters are designed to introduce basic input and output in Java, including reading text input from the keyboard, outputting text to the monitor, and reading/writing files from the file system. More advanced user interaction using Graphics and Graphical User Interface (GUI) programs is taken up in the later section on Swing.

There are two packages for I/O: the older java.io package (does not support symbolic links) and the newer java.nio ("new io") package that has improved exception handling at java.nio.file.

Simple Java Output: Writing to the Screen

[edit | edit source]

Writing to the screen is very easy, and can be accomplished using one of two methods:

Example Code section 9.1: Print "Hello world" without advancing to a new line
System.out.print("Hello world");
Standard input or output Output on the screen
Hello world
Example Code section 9.2: Print "Hello world" and advance to a new line
System.out.println("Hello world");
Standard input or output Output on the screen
Hello world

Simple Java Input: Inputting from the keyboard

[edit | edit source]

As of version 5, Java provides a class in the java.util package called Scanner that simplifies keyboard input.

Example Code section 9.3: Inputting with Scanner
Scanner kbdIn = new Scanner(System.in); // Instantiating a new Scanner object
System.out.print("Enter your name: "); // Printing out the prompt
String name = kbdIn.nextLine(); // Reading a line of input (until the user hits enter) from the keyboard
// and putting it in a String variable called name
System.out.println("Welcome, " + name); // Printing out welcome, followed by the user's name
Standard input or output On the screen
Enter your name: John Doe
Welcome, John Doe

Alternatively, one could write a method to handle keyboard input:

Example Code section 9.4: Line reader
public String readLine() {
  // Creates a new BufferedReader object
  BufferedReader x = new BufferedReader(new InputStreamReader(System.in));

  // Reads a line of input and returns it directly
  return x.readLine();
}

Note that the code above shouldn't be used in most applications, as it creates new Objects every time the method is run. A better alternative would be to create a separate class file to handle keyboard input.


Clipboard

To do:
Add some exercises like the ones in Variables