30
Output, Strings, Input 7 January 2019 OSU CSE 1

Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Output, Strings, Input

7 January 2019 OSU CSE 1

Page 2: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Simplest Java Program?public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");

}}

7 January 2019 OSU CSE 2

Page 3: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Simplest Java Program?public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");

}}

public class declares this code to be a software component for which bytecode should be

generated by the compiler;HelloWorld is the name of

the class;details later.

7 January 2019 OSU CSE 3

Page 4: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Simplest Java Program?public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");

}}

public static void is required here when you want

a class to include a “main” program that can be

executed by the JVM (and it must be called main);

details later.

7 January 2019 OSU CSE 4

Page 5: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Simplest Java Program?public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");

}}

String[] args means that main expects the JVM to hand it

an array of Strings (called command-line arguments)

when it is executed;details later.

7 January 2019 OSU CSE 5

Page 6: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Simplest Java Program?public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");

}}

System.out is an object you may use to give output to the user;

println is a method of that object that you may call (invoke) to output

something on its own line;details later.

7 January 2019 OSU CSE 6

Page 7: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Simplest Java Program?public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");

}}

"Hello World!" is a character string to be output

to the user;details later.

7 January 2019 OSU CSE 7

Page 8: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Another Version (sans Comments)import components.simplewriter.SimpleWriter;import components.simplewriter.SimpleWriter1L;public final class HelloWorld {

private HelloWorld() {}public static void main(String[] args) {SimpleWriter out = new SimpleWriter1L();out.println("Hello World!");out.close();

}}

7 January 2019 OSU CSE 8

Page 9: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

import components.simplewriter.SimpleWriter;import components.simplewriter.SimpleWriter1L;public final class HelloWorld {

private HelloWorld() {}public static void main(String[] args) {SimpleWriter out = new SimpleWriter1L();out.println("Hello World!");out.close();

}}

import indicates you want to use a component in your code;components is a package

containing OSU CSE components;its simplewriter package offers a few advantages over using built-

in System.out;details later.

7 January 2019 OSU CSE 9

Another Version (sans Comments)

Page 10: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

import components.simplewriter.SimpleWriter;import components.simplewriter.SimpleWriter1L;public final class HelloWorld {

private HelloWorld() {}public static void main(String[] args) {SimpleWriter out = new SimpleWriter1L();out.println("Hello World!");out.close();

}}

public means anyone can use this class;

final means no one can incrementally change this

class by using inheritance;details later.

7 January 2019 OSU CSE 10

Another Version (sans Comments)

Page 11: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

import components.simplewriter.SimpleWriter;import components.simplewriter.SimpleWriter1L;public final class HelloWorld {

private HelloWorld() {}public static void main(String[] args) {SimpleWriter out = new SimpleWriter1L();out.println("Hello World!");out.close();

}}

private HelloWorld()means the HelloWorld class does not define a type, i.e., no one can create an object from

the class HelloWorld because it is a utility class;

details later.7 January 2019 OSU CSE 11

Another Version (sans Comments)

Page 12: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

import components.simplewriter.SimpleWriter;import components.simplewriter.SimpleWriter1L;public final class HelloWorld {

private HelloWorld() {}public static void main(String[] args) {SimpleWriter out = new SimpleWriter1L();out.println("Hello World!");out.close();

}}

SimpleWriter is the type of a newly declared variable;out is the name of that

variable;details later.

7 January 2019 OSU CSE 12

Another Version (sans Comments)

Page 13: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

import components.simplewriter.SimpleWriter;import components.simplewriter.SimpleWriter1L;public final class HelloWorld {

private HelloWorld() {}public static void main(String[] args) {SimpleWriter out = new SimpleWriter1L();out.println("Hello World!");out.close();

}}

7 January 2019 OSU CSE 13

Another Version (sans Comments)new creates a new object to which the variable out is a reference;SimpleWriter1L is the class

whose code should be used when any method of out is called;

details later.

Page 14: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

import components.simplewriter.SimpleWriter;import components.simplewriter.SimpleWriter1L;public final class HelloWorld {

private HelloWorld() {}public static void main(String[] args) {SimpleWriter out = new SimpleWriter1L();out.println("Hello World!");out.close();

}}

out has a println method, too, nearly identical to that of

System.out;details later.

7 January 2019 OSU CSE 14

Another Version (sans Comments)

Page 15: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

import components.simplewriter.SimpleWriter;import components.simplewriter.SimpleWriter1L;public final class HelloWorld {

private HelloWorld() {}public static void main(String[] args) {SimpleWriter out = new SimpleWriter1L();out.println("Hello World!");out.close();

}}

out has a close method as well, and you need to call it when you are done using

out; details later.

7 January 2019 OSU CSE 15

Another Version (sans Comments)

Page 16: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Output: SimpleWriter

• The OSU CSE components provide a simple way to provide output to a user via the console or a fileSimpleWriter consoleOut =

new SimpleWriter1L();

SimpleWriter fileOut =

new SimpleWriter1L("foo.txt");

7 January 2019 OSU CSE 16

Page 17: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Output ExamplesconsoleOut.print("Prompt: ");

consoleOut.println();

fileOut.println("A line.");

7 January 2019 OSU CSE 17

Page 18: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Closing Output

• When you are done writing output to a SimpleWriter stream, you must closethe stream:consoleOut.close();

fileOut.close();

7 January 2019 OSU CSE 18

Page 19: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Character Strings

• Java has special features to deal with character strings

• ExamplesSimpleWriter fileOut =

new SimpleWriter1L("foo.txt");

fileOut.print("Hi, Mr. Foo.");

• This intro is just the tip of the iceberg!

7 January 2019 OSU CSE 19

Page 20: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Character Strings

• Java has special features to deal with character strings

• ExamplesSimpleWriter fileOut =

new SimpleWriter1L("foo.txt");

fileOut.print("Hi, Mr. Foo.");

• This intro is just the tip of the iceberg!

a character string

7 January 2019 OSU CSE 20

Page 21: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Character Strings

• Java has special features to deal with character strings

• ExamplesSimpleWriter fileOut =

new SimpleWriter1L("foo.txt");

fileOut.print("Hi, Mr. Foo.");

• This intro is just the tip of the iceberg!

a character string

7 January 2019 OSU CSE 21

Page 22: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Character-String Literals

• Character-string constants, also called String literals, are enclosed in double-quotes, e.g.:"Hello World!"

• Character strings can be concatenated(joined together to create new character strings) using the + operator, e.g.:"Hello " + "World!"

7 January 2019 OSU CSE 22

Page 23: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

String Variables

• You may declare a String variable, and assign an initial character-string value to it, as follows:String cheer = "Go";

"Go"

7 January 2019 OSU CSE 23

Page 24: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

String Variables

• You may assign any other character-string value to the same variable later, e.g.:cheer = cheer + " Bucks!";

• Before assignment above:

7 January 2019 OSU CSE 24

"Go"

Page 25: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

String Variables

• You may assign any other character-string value to the same variable later, e.g.:cheer = cheer + " Bucks!";

• After assignment above:

7 January 2019 OSU CSE 25

"Go Bucks!"

Page 26: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Input: SimpleReader

• The OSU CSE components provide a simple way to get input from a user via the keyboard or a fileSimpleReader keyboardIn =

new SimpleReader1L();

SimpleReader fileIn =

new SimpleReader1L("foo.txt");

7 January 2019 OSU CSE 26

Page 27: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Input ExamplesString line = keyboardIn.nextLine();

line = fileIn.nextLine();

7 January 2019 OSU CSE 27

Page 28: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Input ExamplesString line = keyboardIn.nextLine();

line = fileIn.nextLine();

7 January 2019 OSU CSE 28

This method, which reads up through and including the next line separator, and returns everything it reads exceptthat next line separator, is really the only method you need to read input

from the keyboard and text files.

Page 29: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Closing Input

• When you are done reading input from a SimpleReader stream, you must closethe stream:keyboardIn.close();

fileIn.close();

7 January 2019 OSU CSE 29

Page 30: Output, Strings, Input - Computer Science and …web.cse.ohio-state.edu/.../03.Output-Strings-Input.pdfInput Examples String line = keyboardIn.nextLine(); line = fileIn.nextLine();

Resources

• Java Tutorials ("Hello World" program)– http://docs.oracle.com/javase/tutorial/getStarted/application/index.html

• OSU CSE components API (SimpleWriter, SimpleReader)– http://cse.osu.edu/software/common/doc/

7 January 2019 OSU CSE 30