I am new to java and have been trying to learn it and now I have been facing this error even though both the files are in the same folder :
BankTest.java:3: error: cannot find symbol
BankAccount b = new BankAccount( "M J W Morgan", "0012067" );
^
symbol: class BankAccount
location: class BankTest
BankTest.java:3: error: cannot find symbol
BankAccount b = new BankAccount( "M J W Morgan", "0012067" );
^
symbol: class BankAccount
location: class BankTest
BankTest.java:12: error: cannot find symbol
BankAccount c = new BankAccount( args[0], args[1] );
^
symbol: class BankAccount
location: class BankTest
BankTest.java:12: error: cannot find symbol
BankAccount c = new BankAccount( args[0], args[1] );
^
symbol: class BankAccount
location: class BankTest
BankTest.java:15: error: cannot find symbol
BankAccount c = new BankAccount( args[0], args[1], Double.parseDouble(args[2]) );
^
symbol: class BankAccount
location: class BankTest
BankTest.java:15: error: cannot find symbol
BankAccount c = new BankAccount( args[0], args[1], Double.parseDouble(args[2]) );
^
symbol: class BankAccount
location: class BankTest
I have both the BankAccount.java and the BankTest.java in the same folder. I have tried downloading the source code and still face the same error.
javac BankAccount.java works perfectly but javac BankTest.java shows up the error.
What am I doing wrong here?
This is the BankAccount.java
public class BankAccount {
private String holderName;
private double balance;
private String number;
public BankAccount( String holderName, String number ){
this.holderName = holderName;
this.number = number;
balance = 0;
}
public BankAccount( String holderName, String number, double balance ){
this.holderName = holderName;
this.number = number;
this.balance = balance;
}
public String getHolderName(){
return holderName;
}
public void setName( String newName ){
holderName = newName;
}
public void deposit( double amount ){
balance += amount;
}
public void withdraw( double amount ){
balance -= amount;
}
public double checkBalance(){
return balance;
}
public String toString(){
String s = number + "\t" + holderName + "\t" + balance;
return s;
}
}
And this is the BankTest.java:
public class BankTest {
public static void main(String[] args) {
BankAccount b = new BankAccount( "M J W Morgan", "0012067" );
System.out.println( b );
b.deposit( 100 );
System.out.println( b );
b.withdraw( 500 );
System.out.println( b );
System.out.println( "Balance is: " + b.checkBalance() );
if( args.length == 2 ){
BankAccount c = new BankAccount( args[0], args[1] );
System.out.println( c );
} else {
BankAccount c = new BankAccount( args[0], args[1], Double.parseDouble(args[2]) );
System.out.println( c );
}
}
}
You need to compile the classes, and have them on your classpath. A good way to manage this is to use the -d option. Make sure the directory "build" exists.
javac -d build BankAccount.java
That should compile and make a BankAccount.class in the "build" folder. Then you can do.
javac -cp build -d build BankTest.java
That should create the file BankTest.class in the build folder. Then you can run it.
java -cp build BankTest
You need to put the .class files on the classpath. That is why the -d option works well because you know what folder to put on the class path, it will also create directories for packages and put the class files in the correct location.
Related
import java.util.Random;
import java.util.*;
public class WhackAMole {
int score;
int molesLeft;
int attemptsLeft;
char[][] molegrid;
int actualDimension;
WhackAMole(int numAttempts,int gridDimension){
this.attemptsLeft=numAttempts;
this.actualDimension=gridDimension;
score=0;
molesLeft=10;
molegrid=new char[10][10];
for(int i=0;i<gridDimension-1;i++) {
for(int j=0;j<gridDimension-1;j++) {
molegrid[i][j]='*';
}
}
}
boolean place(int x,int y) {
if((x<actualDimension && x>=0)&& (y<actualDimension && y>=0)) {
molegrid[x][y]='M';
return true;
}
else {
return false;
}
}
void whack(int x,int y) {
if(molegrid[x][y]=='M'){
score++;
molesLeft--;
attemptsLeft--;
molegrid[x][y]='W';
System.out.println("You have made a whack");
System.out.println("You have" +attemptsLeft+"remaining tries");
}
else {
attemptsLeft--;
System.out.println("You have" +attemptsLeft+"remaining tries");
System.out.println("("+x+","+y+") doesnt have a mole");
}
}
void printGridToUser() {
for(int i=0;i<actualDimension;i++)
{
for(int j=0;j<actualDimension;j++) {
if(molegrid[i][j]=='M') {
molegrid[i][j]='*';
System.out.print(molegrid[i][j] );
molegrid[i][j]='M';
}
else {
System.out.print(molegrid[i][j] );
}
}
System.out.print("\n");
}
}
void printGrid() {
for(int i=0;i<actualDimension;i++) {
for(int j=0;j<actualDimension;j++) {
System.out.print(molegrid[i][j]);
}
System.out.print("\n");
}
}
public static void main(String[] args) {
WhackAMole a=new WhackAMole(50,10);
for(int i=0;i<10;i++) {
Random randomGenerator = new Random();
Random randomGenerator1 = new Random();
int molelocationx= randomGenerator.nextInt(9);
int molelocationy= randomGenerator1.nextInt(9);
boolean b= a.place(molelocationx, molelocationy);
if(b==true) {
System.out.println("Mole placed");
}
else {
System.out.println("Mole not placed");
}
}
System.out.println("You have maximum 50 chances");
for(int j=0;j<50;j++) {
Scanner scanner=new Scanner(System.in);
System.out.println("Enter first coordinate");
int userlocx=scanner.nextInt();
Scanner scanner1=new Scanner(System.in);
System.out.println("Enter second coordinate");
int userlocy=scanner1.nextInt();
if(userlocx==-1 && userlocy==-1)
{
System.out.println("Exiting");
System.out.println("Your score is"+a.score);
a.printGrid();
System.exit(1);
}
else if(userlocx>9 || userlocx<-1 || userlocy>9 || userlocy<-1 ){
System.out.println("Invalid");
continue;
}
else {
a.whack(userlocx, userlocy);
a.printGridToUser();
if(a.molesLeft==0) {
System.out.println("You have won!!");
System.exit(2);
}
}
}
System.out.println("Game over try again next time");
}
}
ERRORS SHOWN
/WhackAMoleTestGrader.java:98: error: cannot find symbol
int actualRow = whack.moleGrid.length;
^
symbol: variable moleGrid
location: variable whack of type WhackAMole
/home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:102: error: cannot find symbol
int actualCol = whack.moleGrid[i].length;
^
symbol: variable moleGrid
location: variable whack of type WhackAMole
/home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:107: error: cannot find symbol
char actualChar = whack.moleGrid[i][j];
^
symbol: variable moleGrid
location: variable whack of type WhackAMole
/home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:123: error: cannot find symbol
char actualOneOne = whack.moleGrid[1][1];
^
symbol: variable moleGrid
location: variable whack of type WhackAMole
/home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:125: error: cannot find symbol
assertEquals("Expected char at (1, 2): M, but actual: " + whack.moleGrid[1][2], 'M', whack.moleGrid[1][2]);
^
symbol: variable moleGrid
location: variable whack of type WhackAMole
/home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:125: error: cannot find symbol
assertEquals("Expected char at (1, 2): M, but actual: " + whack.moleGrid[1][2], 'M', whack.moleGrid[1][2]);
^
symbol: variable moleGrid
location: variable whack of type WhackAMole
/home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:129: error: cannot find symbol
assertEquals("Mole placed at wrong place: (" + i + ", " + j + ")", '*', whack.moleGrid[i][j]);
^
symbol: variable moleGrid
location: variable whack of type WhackAMole
/home/ccc_v1_c79431__48717/asn12900_Whack_a_Mole/asn12901_JUnit/asnlib/WhackAMoleTestGrader.java:139: error: cannot find symbol
assertEquals("(1, 1) doesn't have a mole", 'M', whack.moleGrid[1][1]);
^
symbol: variable moleGrid
location: variable whack of type WhackAMole
8 errors
(Failed)
Command exited with non-zero status 1
I can compile you code and there is no errors.
1) There is no actualRow in your code, but it is first error. Others are the same...
I think you need to write code that would be called by testing program. And they assume you have actualRow and other. Read your task again...
2) It doesn't look like a java compiler output. It looks like code compiled with C++ or Python compiler. Check what language you have to use for submission OR maybe there is check-box for selecting language of your submit.
So first I'm gonna post the code then ill explain my problems
import java.util.*;
class Loader
{
protected int BucketSize;
protected int bucket;
protected int price;
public void SetBucketSize(int b)
{
Scanner input = new Scanner(System.in);
System.out.println("What Bucket Size (1-5)?");
bucket = input.nextInt();
while (bucket <6)
{
System.out.println("Enter valid Bucket Size(1-5)");
}
if (bucket == 1)
{
price = 100;
}
if (bucket == 2)
{
price = 200;
}
if (bucket == 3)
{
price = 300;
}
if (bucket == 4)
{
price = 400;
}
if (bucket == 5)
{
price = 500;
}
b = price;
price = BucketSize;
}
public void GetBucketSize()
{
return this.BucketSize;
}
#Override
public void setRentalProfit()
{
RentalProfit = (RentalRate * RentalDays);
}
#Override
public String toString() {
return "Tractor (Rental days = " + RentalDays + ", Rental Rate = " + RentalRate +
", Rental profit = " + RentalProfit + ", VehicleID = " + VehicleID + BucketSize + ")";
}
}
Heres the errors :
Loader.java:46: error: incompatible types: unexpected return value
return this.BucketSize;
^
Loader.java:49: error: method does not override or implement a method from a supertype
#Override
^
Loader.java:52: error: cannot find symbol
RentalProfit = (RentalRate * RentalDays);
^
symbol: variable RentalProfit
location: class Loader
Loader.java:52: error: cannot find symbol
RentalProfit = (RentalRate * RentalDays);
^
symbol: variable RentalRate
location: class Loader
Loader.java:52: error: cannot find symbol
RentalProfit = (RentalRate * RentalDays);
^
symbol: variable RentalDays
location: class Loader
Loader.java:57: error: cannot find symbol
return "Tractor (Rental days = " + RentalDays + ", Rental Rate = " + RentalRate +
Things like RentalDays and other variables are in another class I'm just stuck on what to do here. I can't figured out why its telling me the return thisBucketSize is an incompatible type and also I'm not sure why its not finding the RentalDays and variables that i have in another class in same final. any help/tips would be appreciated
The issue with "return thisBucketSize" is that it's in a method which has a void return type. The issue with the override annotation is that Loader doesn't extend our implement anything, so there is no superclass to override. The other issues appear to be undeclared variables.
As a side note, class names are usually uppercase while variables are lowercase.
So I have been asked to create a program that can evaluate and print the value of...
0.1 + (0.1)^2 + (0.1)^3 . . . + (0.1)^n
using a while loop. So far I have
import java.util.Scanner;
class Power
{
public static void main(String[] args)
{
Double x;
Scanner input = new Scanner(System.in);
System.out.println("What is the maximum power of 0.1?");
x = input.nextLine;
Double n = 0.1;
Int exp = 1;
while (exp <= x)
{
Double Answer = Math.pow(n, exp); //Had to look this one up
exp++;
}
System.out.print(Answer);
}
}
I'm still having trouble trying to decode the following few Compile-time errors I am getting with this program.
Power.java:11: error: cannot find symbol
x = input.nextLine;
^
symbol: variable nextLine
location: variable input of type Scanner
Power.java:13: error: cannot find symbol
Int exp = 1;
^
symbol: class Int
location: class Power
Power.java:19: error: cannot find symbol
System.out.print(Answer);
^
symbol: variable Answer
location: class Power
Any fix? Thanks guys
~Andrew
Here you go:
import java.util.Scanner;
class Power
{
public static void main(String[] args)
{
Double x;
Scanner input = new Scanner(System.in);
System.out.println("What is the maximum power of 0.1?");
x = input.nextDouble(); //Use nextDouble to take in double
Double n = 0.1;
int exp = 1;
Double Answer = 0.0; //You have to declare Answer outside of the while loop below or else Answer will be undefined when you try to print it out in the last line.
while (exp <= x)
{
Answer = Math.pow(n, exp);
exp++;
}
System.out.print(Answer);
}
}
I found some code online to make my own little project as I want to learn Java on my spare time, I found some broken code and tried fixing it myself to the best of my ability, but now I got stuck.
The error I'm receiving is:
TempProg.java:53: error: cannot find symbol
Temperature tempConv = new Temperature();
^
symbol: class Temperature
location: class TempProg
TempProg.java:53: error: cannot find symbol
Temperature tempConv = new Temperature();
^
symbol: class Temperature
location: class TempProg
2 errors
import java.util.Scanner;
public class TempProg {
public double currentTemp;
public double TempF;
public double TempK;
public double newTemp;
public TempProg(double startCurrentTemp, double startTempF, double startTempK, double startnewTemp)
{
currentTemp = startCurrentTemp;
TempF = startTempF;
TempK = startTempK;
newTemp = startnewTemp;
}
private double Temperature(double currentTemp)
{
currentTemp = 100;
return currentTemp;
}
public double convertToF(double TempF, double currentTemp)
{
TempF = ((9 * currentTemp) / 5 ) + 32;
return TempF;
}
public double convertToK(double TempK, double currentTemp)
{
TempK = currentTemp + 273;
return TempK;
}
public double updateTempC(double currentTemp)
{
newTemp = currentTemp;
return currentTemp;
}
public double getTemp()
{
return currentTemp;
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Temperature tempConv = new Temperature();
int newTemp;
boolean entryValid;
final int MIN_TEMP = -273;
final int MAX_TEMP = 10000;
System.out.println("\tTemperature converter");
char selection = 'x';
while (selection != 'q') {
System.out.println("\n\tCurrent temperature in degrees C: " + tempConv.getTemp());
System.out.println("\tType f to display temperature in Fahrenheit");
System.out.println("\tType k to display temperature in Kelvin");
System.out.println("\tType c to set a new temperature");
System.out.println("\tType q to quit");
selection = scan.next().charAt(0);
switch(selection) {
case 'f':
System.out.println("\n\t" +tempConv.getTemp()+ " degrees C = "+tempConv.convertToF() +" degrees F" );
break;
case 'k':
System.out.println("\n\t" +tempConv.getTemp()+ " degrees C = "+tempConv.convertToK() +" degrees K" );
break;
case 'c':
entryValid=false;
while (!entryValid) {
System.out.print("\n\tPlease enter a new temperature: ");
newTemp = scan.nextInt();
if (newTemp < MIN_TEMP || newTemp > MAX_TEMP) {
System.out.println("\tPlease enter a valid temperature");
} else {
entryValid=true;
tempConv.updateTempC(newTemp);
}
}
break;
case 'q':
break;
default:
System.out.println("\n\tOption " + selection + " not understood");
}
}
}
}
At line 53, you are trying to create a new Temperature object with the following call:
Temperature tempConv = new Temperature();
The new operator in Java means that you are creating a new Object of the type specified after the new variable.
In order to create a new instance of a new Object, you must either have that class in the same package as the code that is creating the new instance or you must import the class.
The fact that you are getting the cannot find symbol error means that the compiler cannot find that class so it is not in the same package and it is not imported.
Normally, the fix for this is to import the class if it has already been created and it is in some other class. If this is code you are creating, you may need to create the Temperature object.
Later in your code, you have the following method:
private double Temperature(double currentTemp)
{
currentTemp = 100;
return currentTemp;
}
This creates a method called Temperature, but it does not create a Temperature object. This can be confusing. In Java, to avoid this confusion, method names should always start with lowerCase letters and classes should always start with UpperCase.
You are getting the error because you are trying to create new object by using method name.
new ClassName() is used for creating the object of that class. new keyword is used with the class name not with the method name. For calling the method first you have to create the object of that class and than with the help of that object you can call the method. In your TempProg class you don't have a default constructor you should write one default constructor if you want to create obj to TempProg without setting any value for your member variable like currentTemp,.....
In your case you should do like -
TempProg()
{}
TempProg tempObj = new TempProg(); //than you can create the obj of TempProg like that
if you want to use parameterized constructor than you have to do like
TempProg tempObj = new TempPRog(11.2,222,453,455); //whatever vallue you want to set for those variables
tempObj.Temperature(1122); // call the Temperature method by passing value.
I am just putting some value as a example in this post.
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.