Question

Description:

This Java program will read in data from a file students.txt that keeps records of some students. Each line in students.txt contains information about one student, including his/her firstname, lastname, gender, major, enrollmentyear and whether he/she is a full-time or part-time student in the following format.

FIRSTNAME      LASTNAME        GENDER              MAJORENROLLMENTYEAR FULL/PART

The above six fields are separated by a tab key.

A user can input a keyword indicating what group of students he is searching for. For example, if the user inputs “IT”, it means he is interested and expecting to retrieve information about students majored in IT; if the user inputs “Female”, it means he wants to retrieve some information about those female students.

Based upon the user’s input, the program is expected to collect the records of those students that satisfy the searching criteria, and write these records into another specified file. The resulting file will be a subset of the original file.

Specifications:

In addition to the main method, you need to create another method

  • processData()

main Method

  1. The main method should first create a Scanner object connected to file students.txt.
  2. The main method should create a second Scanner object for users’ input.
  3. The main will then prompt a message asking for users’ input of the searching keyword, and read in this keyword.
  4. The main will then prompt a message asking the user for the name of the output file, and read in that file name.
  5. The main method will then create a PrintStream object connected to the file with the specified output file name.
  6. The main method will then call the method processData(), passing in the Scanner object for the input file, the PrintStream object for the output file, and the searching keyword which is a string.
  7. Close the input file by closing the Scanner object.
  8. Close the output file by closing the PrintStream object.
  9. Please apply the try/catch statement in the main method to prevent the FileNotFoundException exception occurring.

processData Method

  1. The processData() method has three parameters: a Scanner object, a PrintStream object, and a String for the searching keyword.
  2. The processData() method will read in data line by line, and then process every token on that line.
  3. If any token in a line matches with the searching keyword, output the entire line to the specified output file.
  1. Please do NOT use the split() method in this program to split a string into a string array, but create a Scanner object for a line, and process every token on that line.

Output Your output should look something like follows $java Students Please input the keyword you are searching for: IT Pleas

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

import java.io.*;

import java.util.*;

public class Students {

public static void main(String[] args) {

try {

Scanner fileIn = new Scanner(new File("students.txt"));

Scanner in = new Scanner(System.in);

System.out.print("Please input the keyword you are searching for: ");

String keyword = in.next();

System.out.print("Please input the name of the output file: ");

String fname = in.next();

PrintStream ps = new PrintStream(new File(fname));

processData(fileIn, ps, keyword);

fileIn.close();

ps.close();

} catch (FileNotFoundException e) {

System.out.println(e.getMessage());

}

}

public static void processData(Scanner scnr, PrintStream ps, String keyword) {

while(scnr.hasNextLine()){

String line = scnr.nextLine();

StringTokenizer st = new StringTokenizer(line);

boolean isContained = false;

while(st.hasMoreTokens()){

if(keyword.equalsIgnoreCase(st.nextToken())){

isContained = true;

break;

}

}

if(isContained)

ps.println(line);

}

}

}

Add a comment
Know the answer?
Add Answer to:
Description: This Java program will read in data from a file students.txt that keeps records of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains...

    Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains a Java program. Your program should read the file (e.g., InputFile.java) and output its contents properly indented to ProgramName_Formatted.java (e.g., InputFile_Formatted.java). (Note: It will be up to the user to change the name appropriately for compilation later.) When you see a left-brace character ({) in the file, increase your indentation level by NUM_SPACES spaces. When you see a right-brace character (}), decrease your indentation...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • Java Programming Reading from a Text File Write a Java program that will use an object...

    Java Programming Reading from a Text File Write a Java program that will use an object of the Scanner class to read employee payroll data from a text file The Text file payroll.dat has the following: 100 Washington Marx Jung Darwin George 40 200 300 400 Kar Car Charles 50 22.532 15 30 The order of the data and its types stored in the file are as follows: Data EmployeelD Last name FirstName HoursWorked double HourlyRate Data Tvpe String String...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • Write a Java program to read customer details from an input text file corresponding to problem...

    Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT