Question

Write in Java please. The purpose of this program is to read a file, called WaterData.csv....

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
Write in Java please. The purpose of this program is to read a file, called WaterData.csv....
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
  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    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...

  • Description: This Java program will read in data from a file students.txt that keeps records of...

    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...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    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...

  • 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...

  • write a c++ code to read multiple integers from input.dat until the end of file. Edit...

    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...

  • 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...

  • Write a C program which is called ‘multiple_copy’. This program copies one source file to two...

    Write a C program which is called ‘multiple_copy’. This program copies one source file to two destination files as follows: $./multiple_copy....... source_file....... destination_file1......... destination_file2 multiple_copy program copies the contents of source file (i.e., source_file) to any named two destination files in the command line. The number of arguments should be 4 including multiple_copy. If the number of arguments is not 4, the program should display error message saying: “Usage: multiple_copy source_file destination_file1 destination_file2”. When the source_file does not exist, the...

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    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 program that takes a file as input and checks whether or not the content...

    Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples:             [ ( ) ] { } à balanced.             [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...

  • IN PYTHON Write the script test3.py that creates and populates a list of OnlineStudent objects. It inputs a requested s...

    IN PYTHON Write the script test3.py that creates and populates a list of OnlineStudent objects. It inputs a requested student, then prints the fields for that student, and also the username of each student in the student's study group. See the pseudocode in Item 3 below. Use the new type of for loop that you can use for reading from a file: # Instead of writing a loop like this fin = open("input-file", "r") line = fin.readline( ) while line...

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