0% found this document useful (0 votes)
3 views

Java Lab 9

The document provides instructions for a Java lab assignment focused on reading from files. It requires creating a Java file that reads contents from two specified text files and counts occurrences of certain words. The document includes example output and notes on how to implement the file reading and string comparison functionalities.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Lab 9

The document provides instructions for a Java lab assignment focused on reading from files. It requires creating a Java file that reads contents from two specified text files and counts occurrences of certain words. The document includes example output and notes on how to implement the file reading and string comparison functionalities.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Lab 9 – Reading From Files

 Create a new Java file (empty Java file), save it to your H: drive, call it lab9
 Insert the following code to set up your program:
 Be sure to copy the test text files to the location where you saved your Java file

import java.util.Scanner;

import java.io.*;

public class lab9 {

public static void main (String args[]) throws IOException{

Scanner input = new Scanner(System.in);

File doc = new File("filename.txt");

Scanner fileInput = new Scanner(doc);

// Insert your while loop code here

fileInput.close();

100pt:

Write a program that:

 reads in from the test file “lab9pt1.txt”, found in the Test Files folder in the shared drive. The program
should print out the contents of the file.
 Reads in from the test file “lab9pt2.txt”. The program should print out how many times the following
words appear:
o Hello
o World
o A String that the user inputs

See the next page for example output and notes.


Example output:

Part 1:

1
2
3
4
5

Part 2:

>> What word would you like to search for?

>>> Ian

Hello: Found 3 times


World: Found 5 times
Ian: Found 2 times

Notes:

Be sure to copy the test files into the same directory that your java file is
saved.

Use the following while loop to read in the file, line by line:

while(fileInput.hasNextLine()){

String line = fileInput.nextLine();

// Do whatever you want with the String variable line

To compare strings, use the following syntax:

if(line.equals("Ian")){

// Do something…

You might also like