Reputation: 111
I have officially come to the end of my rope. I cannot find what I did wrong. I have done this program almost exactly like another program I wrote a few days ago but I am having problems compiling. I do not know why I am getting errors on the output lines. Please help:
THIS IS THE RUNNING FILE:
package inventory1;
import java.util.Scanner;
public class RunApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DataCollection theProduct = new DataCollection();
String Name = "";
double pNumber = 0.0;
double Units = 0.0;
double Price = 0.0;
while (true) {
System.out.print("Enter Product Name: ");
Name = input.next();
theProduct.setName(Name);
if (Name.equalsIgnoreCase("stop")) {
return;}
System.out.print("Enter Product Number: ");
pNumber = input.nextDouble();
theProduct.setpNumber(pNumber);
System.out.print("Enter How Many Units in Stock: ");
Units = input.nextDouble();
theProduct.setUnits(Units);
System.out.print("Enter Price Per Unit: ");
Price = input.nextDouble();
theProduct.setPrice(Price);
System.out.print("\n Product Name: " + theProduct.getName());
System.out.print("\n Product Number: " + theProduct.getpNumber());
System.out.print("\n Amount of Units in Stock: " + theProduct.getUnits());
System.out.print("\n Price per Unit: " + theProduct.getPrice() + "\n\n");
System.out.printf("\n Total cost for %s in stock: $%.2f\n\n\n", theProduct.getName(), theProduct.calculatePrice());
}
}
}
THIS IS THE DATA COLLECTIONS FILE:
package inventory1;
public class DataCollection {
String productName;
double productNumber, unitsInStock, unitPrice, totalPrice;
public DataCollection() {
productName = "";
productNumber = 0.0;
unitsInStock = 0.0;
unitPrice = 0.0;
}
// setter methods
public void setName(String name) {
productName = name;
}
public void setpNumber(double pNumber) {
productNumber = pNumber;
}
public void setUnits(double units) {
unitsInStock = units;
}
public void setPrice(double price) {
unitPrice = price;
}
// getter methods
public void getName(String name) {
productName = name;
}
public void getpNumber(double pNumber) {
productNumber = pNumber;
}
public void getUnits(double units) {
unitsInStock = units;
}
public void getPrice(double price) {
unitPrice = price;
}
public double calculatePrice() {
return (unitsInStock * unitPrice);
}
}
Upvotes: 0
Views: 142
Reputation: 40149
The reason why it doesn't compile is because your main code:
System.out.print("\n Product Name: " + theProduct.getName());
System.out.print("\n Product Number: " + theProduct.getpNumber());
System.out.print("\n Amount of Units in Stock: " + theProduct.getUnits());
System.out.print("\n Price per Unit: " + theProduct.getPrice() + "\n\n");
System.out.printf("\n Total cost for %s in stock: $%.2f\n\n\n", theProduct.getName(), theProduct.calculatePrice());
Requires getName()
But your getName() implementation did not exist. You didn't have the proper signature. Change it to a proper getter and it should work. Same goes for the other getters.
Instead of:
// getter methods
public void getName(String name) {
productName = name;
}
public void getpNumber(double pNumber) {
productNumber = pNumber;
}
public void getUnits(double units) {
unitsInStock = units;
}
public void getPrice(double price) {
unitPrice = price;
}
Use:
// getter methods
public String getName() {
return productName;
}
public double getpNumber() {
return productNumber;
}
public double getUnits() {
return unitsInStock;
}
public double getPrice() {
return unitPrice;
}
Upvotes: 2
Reputation: 13403
I believe the problem is in the way you are reading the values. While it is safe to read Strings
using Scanner.next()
but for other data structures you might need to clear a new line character from your input stream before you actually read the value.
But this is only in theory because you have given virtually nothing about your problem.
Upvotes: 0