Sunday 21 August 2016

java - How to get Scanner to read next line of numbers?

I am new to java and programming. I have sort of a grasp on the concept but not entirely sure. This problem uses a scanner to read from another file to get information on amount of energy produced.






import java.util.Scanner;
import java.io.File;

public class HJUnit3
{
public static void main(String[] args) throws Exception
{
// Scanner stdIn = new Scanner(System.in);
Scanner stdIn = new Scanner(new File("energyProduced.txt"));
stdIn.nextLine();

double energy;
energy = stdIn.nextLine();
int systemsCost; //Total Systems Cost
double ttlEnergy; //Total energy produced in a week
double savingsWeek; //Total savings for one week
double savingsDay; //Total savings for one day
double recoupDay; //Days to recoup
double recoupYear; //Years to recoup
final double electricCost = 0.085;


systemsCost = (savingsWeek * energy);
System.out.println("Total System Cost = $" + systemsCost );
System.out.println("Total Energy Produced in one week" + (energy * 7) + "Kwh");
savingsWeek = (ttlEnergy * electricCost);
System.out.println("Total Savings for one week =" + savingsWeek);
savingsDay = (savingsWeek / 7);
System.out.println("Total Savings for one day =" + savingsDay);
recoupDay = (systemsCost / savingsDay);
System.out.println("Days to recoup cost(truncated)" + recoupDay);
recoupYear = (recoupDay / 365);

System.out.println("Years to recoup cost(truncated)");
} // end main
} // end HJUnit3 class




It gives me this error "error: incompatible types: String cannot be converted to double
energy = stdIn.next();"

and this "error: incompatible types: possible lossy conversion from double to int

systemsCost = (savingsWeek * energy);"



What did I do wrong, not really sure.



EDIT
So I got the program to work but in the program produces only one set of answers from the .txt file but the file has more numbers and I'm not sure what to add to make it read the next line.





import java.util.Scanner;

import java.io.File;

public class HJUnit3
{
public static void main(String[] args) throws Exception
{
// Scanner stdIn = new Scanner(System.in);
Scanner stdIn = new Scanner(new File("energyProduced.txt"));
stdIn.nextDouble();
double energy;

energy = stdIn.nextDouble();
int systemsCost; //Total Systems Cost
double ttlEnergy; //Total energy produced in a week
double savingsWeek; //Total savings for one week
double savingsDay; //Total savings for one day
double recoupDay; //Days to recoup
double recoupYear; //Years to recoup
final double electricCost = 0.085;
//


ttlEnergy = (energy * 7);
savingsWeek = (ttlEnergy * electricCost);
savingsDay = (savingsWeek / 7);
systemsCost = (int) (savingsWeek * energy);
recoupDay = (systemsCost / savingsDay);
recoupYear = (recoupDay / 365);

System.out.println("Total System Cost = $" + systemsCost );
System.out.println("Total Energy Produced in one week" + (ttlEnergy) + "Kwh");
System.out.println("Total Savings for one week =" + savingsWeek);

System.out.println("Total Savings for one day =" + savingsDay);
System.out.println("Days to recoup cost(truncated)" + recoupDay);
System.out.println("Years to recoup cost(truncated)" + recoupYear);
} // end main
} // end HJUnit3 class





The .txt file is like a bunch of numbers going down a column

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...