Here is a Java program.
It reads characters from the keyboard and creates a String object to contain them.
It creates a Scanner object to read from the keyboard. Scanner is a input connection.
The line import java.util.Scanner; says to use the Scanner class from the package java.util. Keyboard input was not built into Java so you have to add it in.
Type in the program and test it to see if you can get it to work, then read about how it works and make the changes at the bottom of the page
The program defines a class named Echo that contains a single method, its main() method.
All main() methods start this way. Every program should have a main() method.
The program creates a String object referred to by the reference variable inData.
This creates a Scanner object, referred to by the reference variable scan.
This calls the method println to print the characters "Enter the data:" to the monitor.
This uses the nextLine() method of the object referred to by scan to read a line of characters from the keyboard. A String object (referred to by inData) is created to contain the characters.
This first creates a String by concatenating "You entered:" to characters from inData, then calls println() to print that String to the monitor.
Change the program so that it will ask for 2 different lines of information, store the information in 2 different string variables and print them out.
|