d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Basic Java Assignment Help
Add Reply New Topic New Poll
Member
Posts: 11,849
Joined: Aug 24 2005
Gold: 4,113.84
Jun 25 2017 12:48am
Ok I am in an intro class for Java and the assignment is just confusing the hell out of me. I know it should be simple but I just don't understand it.
Quote

Create a program that reads solar energy production data for the last week from a file, gets
input for the cost of the system, and outputs production, savings, days to recoup cost and
years to recoup cost.

Program should read in a file called “energyProduced.txt”. The contents of the file are
at the bottom of the requirements. The file should be a normal text file with no
special formatting. The file should be in the same directory as the program.
• Use a named constant for the cost of electricity at .085.
• Read in all data from the file. (refer to pages 102-103)
o Get input from the file and store production values for each in doubles.
• Get input for “Total system cost” in dollars and stored as an integer. Get input
on same line as prompt, see example.
• Output the following items per the sample below:
• Total Energy Produced in one week.
• Total Savings for one week (total production times cost for electricity)
• Total Savings per day (the average)
• Days to recoup the system cost (truncated)
o Use typecasting to integer to get rounded value
• Years to recoup the system cost (truncated)
o Use typecasting to integer to get rounded value
• Ensure columns are aligned, use escape characters to align columns.

Data for Text File (name it energyProduced.txt)
70.3
72.2
71.6
70.5
69.0
72.2
72.6


This is the sample output


I imported java.util.Scanner and java.io.File. Got the the line Scanner stdIn = new Scanner(new File(energyProduced.txt") and then went brain dead. I have no idea how to sequentially pull the data from each line in the file and assign them as a variable for each day of the week.


Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Jun 25 2017 08:47am
The way your professor wants you to do it is:
Scanner stdIn = new Scanner(new File("C:/temp/tmps.txt"));
List<Double> numbers = new ArrayList<>();
while(stdIn.hasNext()) {
numbers.add(new Double(stdIn.next()));
}

The more functional way to do it would be like:
Path p = Paths.get("C:", "temp", "tmps.txt");
Stream<String> lines = Files.lines(p);
Member
Posts: 11,849
Joined: Aug 24 2005
Gold: 4,113.84
Jun 25 2017 10:34am
Quote (waraholic @ 25 Jun 2017 09:47)
The way your professor wants you to do it is:
Scanner stdIn = new Scanner(new File("C:/temp/tmps.txt"));
List<Double> numbers = new ArrayList<>();
while(stdIn.hasNext()) {
numbers.add(new Double(stdIn.next()));
}

The more functional way to do it would be like:
Path p = Paths.get("C:", "temp", "tmps.txt");
Stream<String> lines = Files.lines(p);


I ended up using scanner for the file read, but for some reason I couldn't get it to read past the first line. The way you have it makes sense but I wouldn't have thought of it because we haven't done arrays.
Code

Scanner inputReader = new Scanner(System.in);
System.out.println("Enter total system cost in dollars");
int sysTotal = inputReader.nextInt();
Scanner stdIn = new Scanner(new File("energyProduced.txt"));
double energy;
energy = stdIn.nextDouble();

I tried that which wouldn't read past line one in the .txt. Obviously I have no real clue what I am doing lol, so it's pretty sloppy.

This post was edited by sbc'soneandonly on Jun 25 2017 10:38am
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Jun 26 2017 07:32pm
Code

double energySunday;
double energyMonday;
energySunday= stdIn.nextDouble();
energyMonday= stdIn.nextDouble();
Member
Posts: 11,849
Joined: Aug 24 2005
Gold: 4,113.84
Jun 30 2017 05:44am
Quote (waraholic @ 26 Jun 2017 20:32)
Code
double energySunday;
double energyMonday;
energySunday= stdIn.nextDouble();
energyMonday= stdIn.nextDouble();


So do you need to call nextDouble for each day 7 times?
Member
Posts: 6,266
Joined: May 10 2017
Gold: 1,500.00
Aug 11 2017 02:51am
Quote (sbc'soneandonly @ Jun 30 2017 06:44am)
So do you need to call nextDouble for each day 7 times?


unless you wannted to do an array
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll