Variable declared, compiler cannot find symbol - java

So for a project I'm working on this program. It's intended to take an input from the user and use it to convert the measurement. I'm getting these errors:
ConversionWilson.java:69: error: cannot find symbol
kilometers = meters * 0.001;
^
symbol: variable kilometers
location: class ConversionWilson
ConversionWilson.java:70: error: cannot find symbol
System.out.println(meters + " meters converted to kilometers becomes: " + kilometers + "km");
^
symbol: variable kilometers
location: class ConversionWilson
ConversionWilson.java:75: error: cannot find symbol
inches = meters * 39.37;
^
symbol: variable inches
location: class ConversionWilson
ConversionWilson.java:76: error: cannot find symbol
System.out.println(meters + " meters converted to inches becomes: " + inches + "in");
^
symbol: variable inches
location: class ConversionWilson
ConversionWilson.java:81: error: cannot find symbol
feet = meters * 3.281;
^
symbol: variable feet
location: class ConversionWilson
ConversionWilson.java:82: error: cannot find symbol
System.out.println(meters + " meters converted to feet becomes: " + feet + "ft");
^
symbol: variable feet
location: class ConversionWilson
ConversionWilson.java:87: error: cannot find symbol
switch (conversion)
^
symbol: variable conversion
location: class ConversionWilson
ConversionWilson.java:90: error: cannot find symbol
showKilometers(meters);
^
symbol: variable meters
location: class ConversionWilson
ConversionWilson.java:94: error: cannot find symbol
showInches(meters);
^
symbol: variable meters
location: class ConversionWilson
ConversionWilson.java:98: error: cannot find symbol
showFeet(meters);
^
symbol: variable meters
location: class ConversionWilson
10 errors
I have all the variables declared. Not sure what exactly it is that's going wrong.
double meters; // Distance as set by the user.
String input; // Input by the user.
char conversion; // Code for the type of conversion.
double kilometers; // The kilometers from the conversion.
double inches; // The inches from the conversion.
double feet; // The feet from the conversion.
// Scanner object to read input
Scanner keyboard = new Scanner (System.in);
// Prompt the user for distance and conversion.
System.out.println("Welcome to the Conversion Program.");
System.out.println("With this program, you can enter a distance and convert it to another form of measurement.");
System.out.print("Please enter the distance in meters: ");
if (meters >= 0)
{
meters = keyboard.nextDouble();
}
else
{
System.out.println("Meters cannot be a negative number. Please choose a positive number.");
}
System.out.print("Please enter the number of the conversion you want to make: \n" +
"1. Convert to Kilometers \n" + "2. Convert to Inches \n" + "3. Convert to Feet \n" +
"4. Quit Program");
input = keyboard.nextLine();
conversion = input.charAt(0);
// Deciding what conversion method to call.
switch (conversion)
{
case '1':
showKilometers(meters);
break;
case '2':
showInches(meters);
break;
case '3':
showFeet(meters);
break;
case '4':
System.out.println("Beep boop bop. Quitting the program now. Later.");
break;
default:
System.out.println("You did not select a possible choice. Please run the program again and be sure to choose a correct number.");
}
}
public static void showKilometers(double meters)
{
kilometers = meters * 0.001;
System.out.println(meters + " meters converted to kilometers becomes: " + kilometers + "km");
}
public static void showInches(double meters)
{
inches = meters * 39.37;
System.out.println(meters + " meters converted to inches becomes: " + inches + "in");
}
public static void showFeet(double meters)
{
feet = meters * 3.281;
System.out.println(meters + " meters converted to feet becomes: " + feet + "ft");
}
It must be something simple I'm just missing right?

You need to declare this variables as instance variable to be accessible within all your class methods.
I think that you have declared these variables inside you main method or one of your class method, so you need to move them out side the method.
public class MyClass {
private double meters; // Distance as set by the user.
private String input; // Input by the user.
private char conversion; // Code for the type of conversion.
private double kilometers; // The kilometers from the conversion.
private double inches; // The inches from the conversion.
private double feet; // The feet from the conversion.
public static void showKilometers(double meters)
{
kilometers = meters * 0.001;
System.out.println(meters + " meters converted to kilometers becomes: " + kilometers + "km");
}
}

Related

Can't call data from second string

I have looked through some of the other questions asking about a similar issue, but I am trying to call the double 'thirdPrice' from calculationMethod() to main(). The purpose of this program is to request data in main(), pass some of the info to calculationMethod() and then return that data back to main() for final output. I am using DrJava, here is my code so far.
import java.util.Scanner; //Imports input device
public class CraftPricing
{
public static void main(String[] args)
{
Scanner inputDevice = new Scanner(System.in); //Sets up input device
String productName; //Used for naming product
double costMaterials, hoursWorked; //Gives variables decimal format
System.out.println("Enter the name of the product "); //Enter product name
productName = inputDevice.nextLine(); //Passes variable for calculation
System.out.println("Enter the cost of materials prior to discount "); //Enter cost of materials
costMaterials = inputDevice.nextDouble(); //Passes variable for calculation
System.out.println("Enter the number of hours worked "); //Enter hours worked
hoursWorked = inputDevice.nextDouble(); //Passes variable for calculation
System.out.printf("The cost of " + productName + " is %.2f\n" , thirdPrice);
//Output product name and cost
}
public static void calculationMethod() //Method used to calcualte price
{
double itemDiscount = 0.75; //Gives decimal format to variable
double payRate = 14.00; //Gives decimal format to variable
double shipHandle = 6.00; //Gives decimal format to variable
double firstPrice = payRate * 7; //Calculates fisr portion of equation
double secondPrice = 7 + firstPrice; //Calculates second portion of equation
final double thirdPrice = itemDiscount * secondPrice + shipHandle;
//Calculates final portion of equation
return thirdPrice; //Returns double to main() for output
}
}
The errors I receive when trying to compile are as follows:
2 errors found:
File: C:\Users\unkno\DrJava\Java\CraftPricing.java [line: 18]
Error: cannot find symbol
symbol: variable thirdPrice
location: class CraftPricing
File: C:\Users\unkno\DrJava\Java\CraftPricing.java [line: 28]
Error: incompatible types: unexpected return value
Why do you need a variable at all?
Call the method.
System.out.printf("The cost of %s is %.2f\n" , productName, calculationMethod());
Or learn about variable scope.
And also fix the return type.
public static double calculationMethod()

My Programming Lab standard output errors

I've been working on this code and everything seems to be working, but when MyProgrammingLab actually runs my code it says there is a problem with my standard output.
Here is the problem:
Write a Temperature class that will hold a temperature in Fahrenheit and provide methods to get the temperature in Fahrenheit, Celsius, and Kelvin. The class should have the
following field:
• ftemp: a double that holds a Fahrenheit temperature.
The class should have the following methods :
• Constructor : The constructor accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field.
• setFahrenheit: The set Fahrenheit method accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field.
• getFahrenheit: Returns the value of the ftemp field as a Fahrenheit temperature (no conversion required)
• getCelsius: Returns the value of the ftemp field converted to Celsius. Use the following formula to convert to Celsius:
Celsius = (5/9) * (Fahrenheit - 32)
• getKelvin: Returns the value of the ftemp field converted to Kelvin. Use the following formula to convert to Kelvin:
Kelvin = ((5/9) * (Fahrenheit - 32)) + 273
Demonstrate the Temperature class by writing a separate program that asks the user for a
Fahrenheit temperature. The program should create an instance of the Temperature class ,
with the value entered by the user passed to the constructor . The program should then
call the object 's methods to display the temperature in the following format (for example,
if the temperature in Fahrenheit was -40):
The temperature in Fahrenheit is -40.0
The temperature in Celsius is -40.0
The temperature in Kelvin is 233.0
And now here is my code:
import java.io.*;
import java.util.Scanner;
public class Temperature
{
private double ftemp;
public Temperature(double ftemp)
{
this.ftemp = ftemp;
}
public void setFahrenheit(double ftemp)
{
this.ftemp = ftemp;
}
public double getFahrenheit()
{
return ftemp;
}
public double getCelsius()
{
return (5.0/9.0) * (ftemp - 32.0);
}
public double getKelvin()
{
return (5.0/9.0) * ((ftemp - 32.0) + 273.0);
}
}
class myTemperature
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double input;
System.out.print("Enter a Fahrenheit temperature:");
input = keyboard.nextDouble();
Temperature temp1 = new Temperature(input);
System.out.println("The temperature in Fahrenheit is " + temp1.getFahrenheit());
System.out.println("The temperature in Celsius is " + temp1.getCelsius());
System.out.println("The temperature in Kelvin is " + temp1.getKelvin());
}
}
These are the errors it gives me:
http://imgur.com/gallery/0D2RkW7/new
I don't have enough rep to post images, sorry!
I really just don't understand what the problem could be, any help would be greatly appreciated.
public double getKelvin()
{
return ((5.0/9.0) * (ftemp - 32.0)) + 273.0;
}
Note the changes in ()

Celsius temperature table error

So, i'm supposed to display a table that gives the Fahrenheit to Celsius conversion from 94F to 104F by 0.5 increments using a for loop and a celsius method that accepts a Fahrenheit temperature as an argument.
(we are just learning about methods this week)
why am I getting this error? and is this code headed in the right direction?
FahrenheitToCelsius.java:28: error: cannot find symbol
fhdeg, celdeg(fhdeg) );
^
symbol: method celdeg(double)
location: class FahrenheitToCelsius
1 error
FahrenheitToSelsius.java
/* PC 5.6 - Fahrenheit to Celsius
-------------------------
Programmer: xxxxxxxxxxx
Date: xxxxxxxxxxxxxxxxx
-------------------------
This program will convert Fahrenheit to Celsius and display the
results.
*/
// ---------1---------2---------3---------4---------5---------6---------7
// 1234567890123456789012345678901234567890123456789012345678901234567890
public class FahrenheitToCelsius
{
public static void main(String[] args)
{
System.out.println("Temperature Conversion Table");
System.out.println("____________________________");
System.out.println(" Fahrenheit Celsius ");
}
public static double celsius(double fhdeg)
{
for (fhdeg = 0; fhdeg >=94; fhdeg+=0.5)
{
double celdeg = 0;
celdeg = 5.0/9 * fhdeg-32;
System.out.printf( " %5.1d %4.1d\n",
fhdeg, celdeg(fhdeg) );
}
}
}
The expression celdeg(fhdeg) means that you are calling a method called celdeg passing an argument called fhdeg. You get the error because there is no method called celdeg().
By the statement of the problem, though, I guess you don't need to create such method. Instead, you just need to iterate over a range of degrees in Fahrenheit and display the equivalent value in Celsius. Your for loop could be something like this:
public static void main(String[] args) {
System.out.println("Temperature Conversion Table");
System.out.println("____________________________");
System.out.println(" Fahrenheit Celsius ");
// From 94F to 104F, with increments of 0.5F
for (double fhdeg = 94.0; fhdeg < 104.5; fhdeg += 0.5) {
// Calculate degree in Celsius by calling the celsiusFromFahrenheit method
double celdeg = celsiusFromFahrenheit(fhdeg);
// Display degrees in Fahrenheit and in Celsius
System.out.printf( " %.2f %.2f\n", fhdeg, celdeg);
}
}
static double celsiusFromFahrenheit(double fhdeg) {
return (5. / 9.) * fhdeg - 32;
}

Why do I keep getting incompatible type error in Java for the temperature converter I am trying to make?

I am new to the Java language. The only other language I learned was Python. I'm trying to do my assignment for class but I keep getting this error and I have no idea what it even means.
import java.util.*; //for Scanner class
public class TempConverter { //an application which will convert temperature from F to C or C to F
public static void main (String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Welcome to the Temperature Converter! Enter 0 to convert F--> C and 1 to convert from C--> F.\n" + "Enter Control-Z when done!");
int F2C;
F2C = stdin.hasNext(); //tells which conversion user wants to do
if (F2C == 0) { //converting Fahrenheit to Celsius
System.out.println("Welcome to the F--> C Converter!");
double tF = stdin.hasNext(); //gets the temperature to convert
while (stdin.hasNext()) { //keep converting every number the user inputs
double tC = ((tF-32.0)*5.0) / 9; //formula for converting to Celsius
System.out.println(tF + "F =" + tC + "C"); //prints converted temperature
tF = stdin.hasNext(); //takes NEXT temperature to convert
}
}
if (F2C == 1) { //converting Celsius to Fahrenheit
System.out.println("Welcome to the C--> F Converter!");
double tC = stdin.hasNext(); //gets the temperature to convert in Celsius
while (stdin.hasNext()) { //keep converting every number that user inputs
double tF = ((tC*9.0)/5) + 32.0; //formula for converting to Fahrenheit
System.out.println(tC + "C = " + tF + "F");
tC = stdin.hasNext();
}
}
System.out.println("Goodbye!");
}
}
I keep getting these errors:
TempConverter.java:7: error: incompatible types: boolean cannot be converted to int
F2C = stdin.hasNext(); //tells which conversion user wants to do
^
TempConverter.java:10: error: incompatible types: boolean cannot be converted to double
double tF = stdin.hasNext(); //gets the temperature to convert
^
TempConverter.java:14: error: incompatible types: boolean cannot be converted to double
tF = stdin.hasNext(); //takes NEXT temperature to convert
^
TempConverter.java:19: error: incompatible types: boolean cannot be converted to double
double tC = stdin.hasNext(); //gets the temperature to convert in Celsius
^
TempConverter.java:23: error: incompatible types: boolean cannot be converted to double
tC = stdin.hasNext();
^
5 errors
As the error message say stdin.hasNext(); returns a boolean value. You assign it to a int F2C. What you whant i guess is F2C = stdin.next();
This reads the next value from stdin.hasNext() only check if there something to read.
The hasNext() method of the Scanner class returns a boolean telling you whether there are more items or not. It does not give you the next item. Please refer to the documentation for more information:
Scanner (Java Platform SE 7)
You should use next() to retrieve the item, as a String. If you're expecting a specific type, you can use nextInt(), etc. Like so, for your first loop:
int F2C;
F2C = stdin.nextInt(); //tells which conversion user wants to do
if (F2C == 0) { //converting Fahrenheit to Celsius
System.out.println("Welcome to the F--> C Converter!");
double tF = stdin.nextDouble(); //gets the temperature to convert
while (stdin.hasNext()) { //keep converting every number the user inputs
double tC = ((tF-32.0)*5.0) / 9; //formula for converting to Celsius
System.out.println(tF + "F =" + tC + "C"); //prints converted temperature
tF = stdin.nextDouble(); //takes NEXT temperature to convert
}
}
You need to use stdin.next(), stdin.nextDouble, etc.
You are mixing up those with stdin.hasNext() (which returns a boolean) - thus the type errors.
The error log is describing the problem, but I'll try to explain:
You're calling the hasNext() method on stdin, which is a Scanner. hasNext() is a method that returns a boolean value (true or false) depending on whether there is more input that can be read.
You're trying to assign this boolean value to int (F2C) or double (tF and tC) variables.
You probably meant to use the nextInt() and nextDouble() methods
You also want to read the next double after checking that there is one and not before.
System.out.println("Welcome to the F--> C Converter!");
while (stdin.hasNextDouble()) { //keep converting every number the user inputs
double tF = stdin.nextDouble(); //gets the temperature to convert
double tC = ((tF-32.0)*5.0) / 9; //formula for converting to Celsius
System.out.println(tF + "F =" + tC + "C"); //prints converted temperature
}

Java - "Cannot find symbol" error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
It's my first time writing a class and I am getting all these errors:
C:\Users\Eamon\programming\java>javac -Xlint:unchecked Shop1.java
Shop1.java:20: error: cannot find symbol
if (cart.itemsInCart.products.get(itemIndex).quantity != 0)
^
symbol: variable quantity
location: class Object
Shop1.java:21: error: cannot find symbol
System.out.println(cart.itemsInCart.products.get(itemIndex).quantity
^
symbol: variable quantity
location: class Object
Shop1.java:22: error: cannot find symbol
+ " " + cart.itemsInCart.products.get(itemIndex).name
^
symbol: variable name
location: class Object
Shop1.java:23: error: cannot find symbol
+ " $"+ df.format(cart.itemsInCart.products.get(itemIndex).price)
^
symbol: variable price
location: class Object
Shop1.java:25: error: cannot find symbol
((cart.itemsInCart.products.get(itemIndex).quantity
^
symbol: variable quantity
location: class Object
Shop1.java:26: error: cannot find symbol
* cart.itemsInCart.products.get(itemIndex).price)));
^
symbol: variable price
location: class Object
Shop1.java:35: error: cannot find symbol
subtotal += cart.itemsInCart.products.get(itemIndex).quantity
^
symbol: variable quantity
location: class Object
Shop1.java:36: error: cannot find symbol
* cart.itemsInCart.products.get(itemIndex).price;
^
symbol: variable price
location: class Object
Shop1.java:126: error: cannot find symbol
if (codeInput.equals(this.itemsInCart.products.get(itemIndex).code))
^
symbol: variable code
location: class Object
Shop1.java:138: error: cannot find symbol
this.itemsInCart.products.get(itemIndex).quantity = scanner.nextInt();
^
symbol: variable quantity
location: class Object
Shop1.java:140: error: cannot find symbol
if (this.itemsInCart.products.get(itemIndex).quantity > 100)
^
symbol: variable quantity
location: class Object
Shop1.java:143: error: cannot find symbol
+ this.itemsInCart.products.get(itemIndex).quantity
^
symbol: variable quantity
location: class Object
Shop1.java:145: error: cannot find symbol
+ this.itemsInCart.products.get(itemIndex).quantity
^
symbol: variable quantity
location: class Object
Shop1.java:147: error: cannot find symbol
if (scanner.nextInt() != this.itemsInCart.products.get(itemIndex).quantity)
^
symbol: variable quantity
location: class Object
Shop1.java:148: error: cannot find symbol
this.item[itemIndex].quantity = 0;
^
symbol: variable item
Shop1.java:150: error: cannot find symbol
if (this.itemsInCart.products.get(itemIndex).quantity < 0)
^
symbol: variable quantity
location: class Object
Shop1.java:151: error: cannot find symbol
this.itemsInCart.products.get(itemIndex).quantity = 0;
^
symbol: variable quantity
location: class Object
Shop1.java:50: error: cannot find symbol
ArrayList products = new Arraylist<Product>(3);
^
symbol: class Arraylist
location: class Catalogue
Shop1.java:54: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
products.add(new Product("Condensed Powdered water", "P3487", 2.50
^
where E is a type-variable:
E extends Object declared in class ArrayList
Shop1.java:56: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
products.add(new Product("Distilled Moonbeams", "K3876", 3.00
^
where E is a type-variable:
E extends Object declared in class ArrayList
Shop1.java:58: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
products.add(new Product("Anti-Gravity Pills", "Z9983", 12.75
^
where E is a type-variable:
E extends Object declared in class ArrayList
Shop1.java:80: error: Illegal static declaration in inner class Catalogue.Product
static final Pattern productCodeRegex =
^
modifier 'static' is only allowed in constant variable declarations
Shop1.java:83: error: Illegal static declaration in inner class Catalogue.Product
public static boolean isValidCode(String codeInput)
^
modifier 'static' is only allowed in constant variable declarations
Shop1.java:92: error: cannot find symbol
+ this.products.get(itemIndex).name
^
symbol: variable name
location: class Object
Shop1.java:93: error: cannot find symbol
+ " [" + this.products.get(itemIndex).code + "], $"
^
symbol: variable code
location: class Object
Shop1.java:94: error: cannot find symbol
+ df.format(this.products.get(itemIndex).price) + " "
^
symbol: variable price
location: class Object
Shop1.java:95: error: cannot find symbol
+ this.products.get(itemIndex).rate + ".");
^
symbol: variable rate
location: class Object
24 errors
3 warnings
I'm working my way back, and I can find those variables just fine; what gives?
import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.text.DecimalFormat;
public class Shop1
{
public static void main(String args[])
{
Cart cart = new Cart(new Catalogue());
printOrder(cart);
}
public static void printOrder(Cart cart)
{
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Your order:");
for(int itemIndex = 0; itemIndex < cart.itemsInCart.products.size();
itemIndex++)
if (cart.itemsInCart.products.get(itemIndex).quantity != 0)
System.out.println(cart.itemsInCart.products.get(itemIndex).quantity
+ " " + cart.itemsInCart.products.get(itemIndex).name
+ " $"+ df.format(cart.itemsInCart.products.get(itemIndex).price)
+ " = $" + df.format
((cart.itemsInCart.products.get(itemIndex).quantity
* cart.itemsInCart.products.get(itemIndex).price)));
double subtotal = 0;
int taxPercent = 20;
double tax;
double total;
for(int itemIndex = 0; itemIndex < cart.itemsInCart.products.size();
itemIndex++)
subtotal += cart.itemsInCart.products.get(itemIndex).quantity
* cart.itemsInCart.products.get(itemIndex).price;
tax = subtotal * taxPercent / 100;
total = subtotal + tax;
System.out.print("Subtotal: $" + df.format(subtotal)
+ " Tax # " + taxPercent + "%: $" + df.format(tax)
+ " Grand Total: $" + df.format(total));
}
}
class Catalogue
{
DecimalFormat df = new DecimalFormat("0.00");
ArrayList products = new Arraylist<Product>(3);
public Catalogue()
{
products.add(new Product("Condensed Powdered water", "P3487", 2.50
, "per packet"));
products.add(new Product("Distilled Moonbeams", "K3876", 3.00
, "a dozen"));
products.add(new Product("Anti-Gravity Pills", "Z9983", 12.75
, "for 60"));
}
class Product
{
String name;
double price;
String code;
String rate;
int quantity;
public Product(String startName, String startCode, double startPrice
, String startRate)
{
name = startName;
code = startCode;
price = startPrice;
rate = startRate;
quantity = 0;
}
static final Pattern productCodeRegex =
Pattern.compile("^[a-zA-Z][0-9]{4}$");
public static boolean isValidCode(String codeInput)
{return productCodeRegex.matcher(codeInput).matches();}
}
public void printCatalogue()
{
System.out.println("Our catalogue (product codes in brackets):");
for(int itemIndex = 0; itemIndex < this.products.size(); itemIndex++)
System.out.println("(" + (itemIndex + 1) + ") "
+ this.products.get(itemIndex).name
+ " [" + this.products.get(itemIndex).code + "], $"
+ df.format(this.products.get(itemIndex).price) + " "
+ this.products.get(itemIndex).rate + ".");
System.out.println("Buy something!");
}
}
class Cart
{
Scanner scanner = new Scanner(System.in); //
int size = 3; //
String codeInput = "";
Catalogue itemsInCart;
public Cart(Catalogue catalogue)
{
itemsInCart = catalogue;
catalogue.printCatalogue();
this.selectProducts();
}
public void selectProducts()
{
while (true)
{
System.out.print("Enter product code (0 to check out): ");
codeInput = scanner.next();
scanner.nextLine();
if (codeInput.equals("0")) return;
for (int itemIndex = 0; itemIndex < this.itemsInCart.products.size();
itemIndex++)
if (codeInput.equals(this.itemsInCart.products.get(itemIndex).code))
this.addToCart(itemIndex);
if (Product.isValidCode(codeInput))
System.out.println("This product code is not on record.");
else System.out.println
("Sorry, I don't understand! Use product codes only.");
}
}
public void addToCart(int itemIndex)
{
System.out.print("Enter quantity: ");
this.itemsInCart.products.get(itemIndex).quantity = scanner.nextInt();
scanner.nextLine();
if (this.itemsInCart.products.get(itemIndex).quantity > 100)
{
System.out.print("That is a large order, "
+ this.itemsInCart.products.get(itemIndex).quantity
+ " counts. Is this correct? Enter \""
+ this.itemsInCart.products.get(itemIndex).quantity
+ "\" to confirm, or, enter any other integer to cancel: ");
if (scanner.nextInt() != this.itemsInCart.products.get(itemIndex).quantity)
this.item[itemIndex].quantity = 0;
}
if (this.itemsInCart.products.get(itemIndex).quantity < 0)
this.itemsInCart.products.get(itemIndex).quantity = 0;
}
}
In Catalogue, you need
ArrayList<Product> products = new ArrayList<Product>(3);
instead of
ArrayList products = new Arraylist<Product>(3);
or else the compiler does not know which type will be returned by
cart.itemsInCart.products.get(itemIndex)
Only if the compiler knows the returned type here is Product, he knows you can access the quantity field. If any type could be returned, it could be types were .quantity is not present or not accessible.
For future reference, please provide the code you are actually using, because yours wouldn't even compile. You have Arraylist where it should be ArrayList (Java is case sensitive). Also it is good practice to program against the interface, so it would be even better to write List<Product> products = new ArrayList<Product>(3);
From what I can see, some symbols you're trying to use could be inaccessible. Try defining each class in its own file, then making the symbols you want to use public and see if that resolves the issue. After that, you can structure your code in a better manner (that is, with getters and setters instead of direct access).
EDIT: My mistake. jlordo's answer is the correct one.

Categories

Resources