-7

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

gunr2171
  • 10,315
  • 25
  • 52
  • 75
Jack Huynh
  • 33
  • 2
  • 10
  • Java cannot magically convert types for you. If you want to change a `String` into a a `Double`, use `Double.parseDouble()`. – azurefrog Oct 31 '16 at 17:10
  • 1
    @azurefrog: Or directly use `stdIn.nextDouble()` – Marvin Oct 31 '16 at 17:11
  • 1
    stdIn.next() returns a String. You're trying to assign it to a variable of type double. That's not possible, because a String is not a double. That's what the first error message means. The second means that you're trying to assign a double, which has a decimal part and is potentially very large, to a variable of type int, which does not have any decimal part and has a more limited range of values. That's also not possible, at least not without a cast informing the compiler that you understand what you're doing and accept to lose the precision. Now, take time thinking about what you must do. – JB Nizet Oct 31 '16 at 17:12
  • What would I need to replace? – Jack Huynh Oct 31 '16 at 17:12
  • @Marvin Eh... I'm not a huge fan of doing it that way, since it's easy to run into [this problem](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo). I prefer to read the line and then explicitly parse things. – azurefrog Oct 31 '16 at 17:12
  • @azurefrog: Agreed, I've removed the "even better" part. – Marvin Oct 31 '16 at 17:14
  • 1
    Please change your title to reflect the actual issue. – Sotirios Delimanolis Oct 31 '16 at 17:15

2 Answers2

2

The compiler says that a String cannot be converted into a Double. That's the problem. You're saying double energy = stdIn.nextLine(), but Scanner#nextLine() returns a String. Instead, you either want to convert it to a Double (Double.parseDouble(String)), or you could call stdIn.nextDouble(). http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

hyper-neutrino
  • 4,300
  • 1
  • 20
  • 40
0

You may need to brush up on your "datatypes" skills, because Java is a strongly typed language :-).

The error

error: incompatible types: String cannot be converted to double energy = stdIn.next();

means that the value returned by stdIn.next() has a datatype of "string", which does not match the datatype of the variable "energy", because "energy" is not a string. It is a "double".

You convert the value returned by stdIn.next() to a double by using

energy = Double.parseDouble(stdIn.next());

Beware that this will throw an exception when the string returned by stdIn.next() characters such as alphabets.

For your second error, why do you want systemsCost variable to be an integer? Do you want it to always be a rounded number? If so, then you need to round the result of the expression "savingsWeek * energy" to the nearest whole number, and then convert it into an integer. On the other hand, if you don't necessarily want systemsCost to be an integer, just declare it as a double.

nvkrj
  • 922
  • 6
  • 15
  • "strongly typed" Do you mean statically typed? – hyper-neutrino Oct 31 '16 at 18:01
  • I meant "strongly" typed. In simpler terms, it means that all variables in Java have predefined data-types, and, unlike "loosely" typed languages, you can not randomly assign a variable with one data-type to a variable with another data-type. For example, in JavaScript, you can do var x = 1.2 + " hi"; but you can't do that in Java because 1.2 is a double and " hi" is a string. adding them together makes no sense. – nvkrj Oct 31 '16 at 18:09
  • Okay. I thought that was called statically typed, but I may be wrong. – hyper-neutrino Oct 31 '16 at 18:09