Write in Java please.
The purpose of this program is to read a file, called WaterData.csv. It will output date and gallons. So it would look something like "2/04/15 40 Gallons". The pseudo code is as follows.
prompt the user for a file name
open the file
if that file cannot be opened
display an error message and quit
endif
create a String variable 'currentDate' and initialize it to the empty string.
create a variable gallonsUsed and initialize it to 0
while the file has contents
read a line
if the line begins with an octothorpe (#) or is length 0
throw the line away
end if
split the line into fields called String[] dataFields
if there are < or > 2 fields
add an error message to an error list (ArrayList<String>)
throw the line away
end if
split dataFields[0] (the date field) into dateFields (what is the delimiter?)
ignore the time field.
if currentDate is empty
assign dataField[0] to currentDate
add the hours' reading to gallonsUsed
else if currentDate <> dataField[0]
we've found a new day
output the date and the gallonsUsed for that date
do not display any more than 1 digit to the right of the decimal point
assign dataField[0] to currentDate
reset gallonsUsed
end if
end while
output the last reading
The water.csv program looks like this
"
#DateTime,Gallons 1/28/2019 16:00,0.7 1/28/2019 15:00,5.5 1/28/2019 14:00,0 1/28/2019 13:00,0.3 1/28/2019 12:00,0.7 "
The output should look like this:
1/28/19 43 Gallons 1/27/19 56 Gallons
Solution(s):
I will be writing this code in Python. However, I do not have the CSV file. So, I will be providing a partial solution. Thumbs up if you like the solution.
# Displaying error if the file can not be opened.
import sys
try:
f = open('WaterData.csv', 'rb')
except IOError:
print ("Could not read file: WaterData.csv")
sys.exit()
#Defining the variables.
currentDate = ''
gallonsUsed = 0
#Reading the CSV file.
import csv
with open("WaterData.csv", "r") as f:
reader = csv.reader(f, delimiter="\t") #Change the delimeter
according to your file.
for i, line in enumerate(reader):
if not line.startswith('#') or not line.startswith(''):
if len(list)==2:
else:
print('Error in the line.')
continue:
Write in Java please. The purpose of this program is to read a file, called WaterData.csv....
The purpose of this program is to read a file, called WaterData.csv. It will output date and gallons. So it would look something like "2/04/15 40 Gallons". The pseudo code is as follows. prompt the user for a file name open the file if that file cannot be opened display an error message and quit endif create a String variable 'currentDate' and initialize it to the empty string. create a variable gallonsUsed and initialize it to 0 while the file...
The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...
Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following requirement. You and your partner will be writing a simple Java program that implements some basic file I/O operations. You can use this code as the basis for some of your next project. FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each...
Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...
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...
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...
write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...
This assignment involves 2 Java programs 1) Write a Java program to read a CSV (Comma separated values) text file. The text file will contain comma separated values in each line. Each line contains data in this format: String(20), String(5),String(10), double. There will be multiple lines with each line containing the same number of values. Ask the user to enter the path for the CSV file for reading the file. After reading the data from the file output the data...
Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...
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...