Chapter 2.4.1 – A First Text Input Example | Introduction to Programming Using Java

Chapter 2.4.1 – A First Text Input Example | Introduction to Programming Using Java

 

2.4.1 A First Text Input Example

 

The input routines in the TextIO class are static member functions. (Static member functions were introduced in the previous section.) Let’s suppose that you want your program to read an integer typed in by the user. The TextIO class contains a static member function named getlnInt that you can use for this purpose.

Since this function is contained in the TextIO class, you have to refer to it in your program as TextIO.getlnInt. The function has no parameters, so a complete call to the function takes the form “TextIO.getlnInt()”.

 

Chapter 2.4.1 - A First Text Input Example | Introduction to Programming Using Java

 

This function call represents the int value typed by the user, and you have to do something with the returned value, such as assign it to a variable. For example, if userInput is a variable of type int (created with a declaration statement “int userInput;”), then you could use the assignment statement userInput = TextIO.getlnInt();

When the computer executes this statement, it will wait for the user to type in an integer value. The value typed will be returned by the function, and it will be stored in the variable, userInput. Here is a complete program that uses TextIO.getlnInt to read a number typed by the user and then prints out the square of the number that the user types:

 

Chapter 2.4.1 - A First Text Input Example | Introduction to Programming Using Java

} //end of class PrintSquare

When you run this program, it will display the message “Please type a number:” and will pause until you type a response, including a carriage return after the number.

 

2.4.2 Text Output

 

The TextIO class contains static member subroutines TextIO.put and TextIO.putln that can be used in the same way as System.out.print and System.out.println. For example, although there is no particular advantage in doing so in this case, you could replace the two lines

 

Chapter 2.4.1 - A First Text Input Example | Introduction to Programming Using Java

 

For the next few chapters, I will use TextIO for input in all my examples, and I will often use it for output. Keep in mind that TextIO can only be used in a program if it is available to that program. It is not built into Java in the way that the System class is.

Let’s look a little more closely at the built-in output subroutines System.out.print and System.out.println. Each of these subroutines can be used with one parameter, where the parameter can be a value of any of the primitive types byte, short, int, long, float, double, char, or boolean.

The parameter can also be a String, a value belonging to an enum type, or indeed any object. That is, you can say “System.out.print(x);” or “System.out.println(x);”, where x is any expression whose value is of any type whatsoever.

The expression can be a constant, a variable, or even something more complicated such as 2*distance*time. Now, in fact, the System class actually includes several different subroutines to handle different parameter types.

There is one System.out.print for printing values of type double, one for values of type int, another for values that are objects, and so on. These subroutines can have the same name since the computer can tell which one you mean in a given subroutine call statement, depending on the type of parameter that you supply.

Having several subroutines of the same name that differ in the types of their parameters is called overloading. Many programming languages do not permit overloading, but it is common in Java programs.

 

a first text input example

 

The difference between System.out.print and System.out.println is that the println version outputs a carriage return after it outputs the specified parameter value. There is a version of System.out.println that has no parameters. This version simply outputs a carriage return, and nothing else.

A subroutine call statement for this version of the program looks like “System.out.println();”, with empty parentheses. Note that “System.out.println(x);” is exactly equivalent to “System.out.print(x); System.out.println();”; the carriage return comes after the value of x. (There is no version of System.out.print without parameters. Do you see why?)

As mentioned above, the TextIO subroutines TextIO.put and TextIO.putln can be used as replacements for System.out.print and System.out.println. The TextIO functions work in exactly the same way as the System functions, except that, as we will see below, TextIO can also be used to write to other destinations.

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.2.1 – Variables | Introduction to Programming Using Java

Chapter 2.2.2 – Types and Literals | Introduction to Programming Using Java

Chapter 2.2.3 – Variables in Programs | Introduction to Programming Using Java

Chapter 2.3.1 – Built-in Subroutines and Functions | Introduction to Programming Using Java

Chapter 2.3.2 – Operations on Strings | Introduction to Programming Using Java

Chapter 2.3.3 – Introduction to Enums | Introduction to Programming Using Java

1 thought on “Chapter 2.4.1 – A First Text Input Example | Introduction to Programming Using Java”

Leave a Comment