So far we have been working with values that have been included in the program code. Now we would like to allow users to interface with the program and allow them to enter values.
Typically when input is required, there is a prompt to the user. We will be using the IO
class to accept input into the program.
Introduction to Methods
To gain input we use methods. A method is a block of code that has been defined to perform a specific task or algorithm. Methods can have parameters that allow values to be made available to use in the algorithm. Methods can have a return value to provide a result of the algorithm.
Methods can be static methods. Static methods are methods that belong to the class and should be called using the class name instead of an object.
Methods can be instance methods. Instance methods are methods that belong to the object and are called using an object of the class.
The IO
class contains static methods for input and output.
The IO
class is in preview for Java 24. It is part of the java.io.IO package. The class has been implicitly imported for your use and calling the methods does not require the class name. This will change in Java 25, so watch for updates.
The IO
class contains the following methods to use for both input and output.
IO Class Methods
Method |
Method Description |
print(Object obj) |
Writes a string representation of obj to the console. Does not move to the next line after the string representation of obj is printed to the console. Any new calls to print will continue directly after what was printed. |
println() |
Stands for print line. Stops printing on the current line and moves to the next line. |
println(Object obj) |
Writes a string representation of obj to the console and moves to the next line. Any new calls to print or println will start on the next line. |
readln() |
Reads a single line of text from the console.
|
readln(String prompt) |
Writes prompt to the screen as if calling print . Then, reads a single line of text from the console. |
Output
The Java Playground is great for trying out segments of code, but if we write print
statements to show that the output doesn't move to the next line, it won't behave as we expect it to. That is because it will treat each print
statement distinctly. At this time, to test out some code, we will want to move over to an IDE. If you need help, please reference the Getting Set-up page.
In this section, we will be using preview features in JDK 24. To use features that are still in preview, you will need to enable preview features in your IDE.
The main Method
When using an IDE, we need to write a class in Java. This class will have one method, the main
method. With preview features turned on, creating the class is really easy, you just need to create a new Java file in your IDE and name it with the extension .java
. The name of the file will be the name of the class and should start with an uppercase letter. The naming of a class follows some of the same rules as naming variables:
- they can consist of letters, numbers, underscore (
_
), and dollar sign ($
). By convention, the dollar sign ($
) is never used.
- they cannot start with a number. By convention, they always start with an uppercase letter.
- they cannot be a Java keyword. A Java keyword is a word that is set aside with a specific meaning in the language. Our primitive data types, for example, are all considered Java keywords.
This first class will only have one method, the main
method. The header for this method will be as follows:
void main() {
}
void
is the return type for this method. A return type provides the data type of a value that would result from calling this method. This is referred to as returning a value. Not all methods have a value that is being returned. When there is no value being returned the void return type is used.
main
is the name of the method.
()
represents the parameter list. In this case, there are no parameters in the parameter list. Parameters are values that are being made available for the method to use when executing its algorithm.`
{}
are used to denote the start and end of the code segment that is in the body of this method. This is the program code that would be executed when the program is run.
After you enable preview features, create a new java file called SampleOutput.java
that contains the following code:
void main() {
print("Let's play with Java!");
}
When you run this code, you should see the following in the console window: Let's play with Java!
For each of the following examples, modify the body of the main
method to be the code segment provided.
Code Segment 1:
String name = "Duke";
print("Hi " + name + "! Are you having fun with Java?");
This should print:
Hi Duke! Are you having fun with Java?
The value of name is added to "Hi " giving us "Hi Duke". Then "! Are you having fun with Java?" is appended giving the final resulting value that is printed to the console.
Code Segment 2:
print("This is on the first line.");
print("This is also on the first line.");
This should print:
This is on the first line.This is also on the first line.
Code Segment 3:
print("This is on the first line.");
println();
print("This is on the second line.");
This should print:
This is on the first line.
This is on the second line.
Code Segment 4:
println("This is on the first line.");
print("This is on the second line.");
This should print:
This is on the first line.
This is on the second line.
If you would like to get values from the user, you can prompt the user for the input.
Modify the main
method used for the output samples as follows:
String name = readln("Enter your name");
print("Hi " + name + "! Are you having fun with Java?");
This will allow you to enter your name into the console as input. For this example, if I entered in Duke
, the following would be output.
Hi Duke! Are you having fun with Java?
Here is an example of what it could look like in Visual Studio Code.
Guiding Questions
As you start to write programs, you will need to further develop your abilities to algorithmically design solutions. The following are good guiding questions to help you design your program.
- What data is the program using?
- What data is being produced?
- What is the type of this data?
- What are good variables names for this data?
- What are the steps to write the program?
For example, if you were asked to write a program to print on a sign the two teams that are playing football that weekend, you might have the following as answers to these questions:
- What data is the program using?
- The names of the two teams
- What data is being produced?
- The output of team1 vs. team2
- What is the type of this data?
- The names of the teams are both type
String
- What are good variables names for this data?
- What are the steps to write the program?
- Prompt the user for the name of
team1
- Prompt the user for the name of
team2
- Output "team1 vs. team2"
The program would look like this:
void main(){
String team1 = readln("Enter the name of the first team: ");
String team2 = readln("Enter the name of the second team: ");
println(team1 + " vs. " + team2);
}
Resources