Java
This assignment will give you practice with line based file processing and scanner methods.
Modify the Hours program we did in class. You are going to write a program that allows the user to search for a person by ID. Your program is required to exactly reproduce the format and behavior of the log of execution as follows:
Enter an ID: 456 Brad worked 36.8 hours (7.36 hours/day) Do you want to search again? y Enter an ID: 293 ID #293 not found Do you want to search again? y Enter an ID: Kim ID is not valid Do you want to search again? nope Have a nice day!
At minimum, your program should have the following static methods in addition to method main: /* Your submission should have the exact same method headers as follows */
public static String findPerson(Scanner input, int searchId) {
public static void processLine(String line) {
When you ask the user whether or not to search again, you should use the “next()” method of the Scanner class to read a one-word answer from the user. You should continue playing if this answer begins with the letter “y” or the letter “Y”. Notice that the user is allowed to type words like “yes”. You are to look just at the first letter of the user’s response and see whether it begins with a “y” or “n” (either capitalized or not) to determine whether to play again.
hours file
123 Kim 12.5 8.1 7.6 3.2
456 Brad 4 11.6 6.5 2.7 12
789 Stef 8 7.5
012 Nancy 3 9 2.5 4.5 1.5 1
345 Kevin 40
678 Dayne 1.5 2.0 3.5 4.5 5.0
901 Iva 9 8.5 7 6.5 5 4.5
234 Jordan 0 0 0 1 0 0 0
567 Gabe 5.0 3.0 2.5
890 Lisa 12.5 16.5
ANSWER:-
CODE:-
import java.io.*;
import java.util.*;
class FileReading{
public static String findPerson(Scanner input, int
searchId){
// This function searches seachId
in file
// If it is found that line will be
returned
// else empty string will be
returned
while(input.hasNext()){
String p =
input.nextLine();
String[] person
= p.split("\\s+");
int id =
Integer.parseInt(person[0]);
if(id ==
searchId)
return p;
}
return "";
}
public static void processLine(String line){
// This funtion processes
line
// prints output in required
format
String[] values =
line.split("\\s+");
String name = values[1];
double totalHours = 0.0;
double avg;
if(values.length>1){
for(int
i=2;i<values.length;i++){
totalHours += Float.parseFloat(values[i]);
}
avg =
totalHours/(values.length-2);
System.out.printf("%s worked %.1f hours (%.2f
hours/day)\n\n",name,totalHours,avg);
}
}
public static void main(String[] args) {
try{
// make a
connection to file using Scanner
Scanner input =
new Scanner(new File("hours.txt"));
// create
another scanner object
// for reading
input from user
Scanner sc = new
Scanner(System.in);
String ch;
int id;
do{
try{
// Ask user to enter ID
System.out.print("\nEnter an
ID: ");
id = sc.nextInt();
String line =
findPerson(input,id);
if(line.length()>0){
processLine(line);
}else{
System.out.println("ID #"+id+" not found\n");
}
}catch (Exception e) {
System.out.println("ID is not
valid\n");
}
sc.nextLine();
System.out.print("Do you want to search again?
");
ch = sc.nextLine();
ch = ch.toLowerCase();
}while(ch.charAt(0)!='n');
System.out.println("\nHave a nice day!");
}catch (Exception e) {
System.out.println(e);
}
}
}
hours.txt:-
123 Kim 12.5 8.1 7.6 3.2
456 Brad 4 11.6 6.5 2.7 12
789 Stef 8 7.5
012 Nancy 3 9 2.5 4.5 1.5 1
345 Kevin 40
678 Dayne 1.5 2.0 3.5 4.5 5.0
901 Iva 9 8.5 7 6.5 5 4.5
234 Jordan 0 0 0 1 0 0 0
567 Gabe 5.0 3.0 2.5
890 Lisa 12.5 16.5
NOTE:- To execute this without error, java file and txt should be in same directory. otherwise it shows file not found exception. or else you need give txt file's path in scanner object.
NOTE:- If you need any modifications in the code,please comment below.Please give positive rating.THUMBS UP.
THANK YOU!!!!
OUTPUT:-
Java This assignment will give you practice with line based file processing and scanner methods. Modify...
JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...
Information About This Project In the realm of database processing, a flat file is a text file that holds a table of records. Here is the data file that is used in this project. The data is converted to comma separated values ( CSV ) to allow easy reading into an array. Table: Consultants ID LName Fee Specialty 101 Roberts 3500 Media 102 Peters 2700 Accounting 103 Paul 1600 Media 104 Michael 2300 Web Design...
Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...
You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...
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...
In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...
In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...
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...
You will be writing some methods that will give you some practice working with Lists. Create a new project and create a class named List Practice in the project. Then paste in the following code: A program that prompts the user for the file names of two different lists, reads Strings from two files into Lists, and prints the contents of those lists to the console. * @author YOUR NAME HERE • @version DATE HERE import java.util.ArrayList; import java.util.List; import...
Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...