Reading text input:
In order to give you an idea of the power of using streams for input and output, we consider how to input text, first from the keyboard and then from a file. This shows how similar the programming of input is, regardless of the source. Normally we combine a source stream with one or more processing or data manipulation streams to get the desired behaviour. Java 1.5 introduces a convenience class for text processing called Scanner, in the java.util package.
Reading text from keyboard
Recall that the keyboard is normally the source of data for the standard input stream System.in. This stream can be passed to an instance of Scanner to read keyboard input that may be returned as string or primitive data. An example is show below:
public void hello()
{
Scanner sc = new Scanner(System.in);
//Ask what is your name
String name = sc.nextLine();
//Ask what is your age
int age = sc.nextInt();
//Display name and age here
sc.close();//take care
}
The program creates a Scanner object that reads data into an area of memory. It allows the user to enter a name and age and have the program repeat them back.
Note the use of the close method to close the Scanner when we have finished using it. This is usually good practice with streams and sometimes essential to proper operation, as a later example in this section will show. However, closing Scanner can also close its underlying stream, and so you should not normally close a Scanner unless you also opened its source. In this case, closing the Scanner closes System.in, which is probably to be avoided, as it will mean that you cannot read any further data from the keyboard.
Reading text from a file
To read text from a source other than the keyboard, we simply change the stream defining the source. So instead of using System.in, we can use a file. The Scanner class has a constructor receiving a File as an argument and then provides the same methods to read from the file. For example:
Scanner sc = new Scanner
(new File ("source/use.txt"));
Note that the File constructor requires a string corresponding to the filename in a form appropriate to the local operating system. You can specify absolute or relative path names, such as:
"C:/javasource/datafiles/users.txt"
This uses the forward slash character as a separator and should work across all platforms. If you want to use Windows-style filenames, you must use a double backslash character to indicate the normal Windows filename separator (one backslash) like this:
"C:\\javasource\\datafiles\\users.txt"
This is because the backslash is used in strings to indicate escape characters, such as \n and \t for newline and tab respectively.
As usual, the close method of the Scanner class is invoked when we have finished reading the file. This ensures that any data still in a buffer is processed before the program continues.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Reading text input homework and assignments? Live Reading text input experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Reading text input homework help, java assignment help and Reading text input projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours