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.
Related
In my introductory Java course, we are required to create a program that gets and sets info on an array of cars based off what our teacher posted on our class website. One of the requirements is to make a function that will print the info of all three cars
public static void printAllCarsInfo() // function for printing info needed for all three cars
{
for ( int index = 0 ; index < NUM_CARS ; index++ )
cars[index].printInfo();
The printInfo() function is in another class and looks like:
public void printInfo()
{
if ( fuelGallons > 0 )
System.out.println("The " + getModel() + " has driven " + getMilesDriven() + " miles and has " + getFuelGallons() + " gallons left.");
else
System.out.println("The " + getModel() + " has driven " + getMilesDriven() + " miles and is out of gas.");
Now when I debug it, I get an error message that goes:
Smith12.java:31: error: cannot find symbol
cars[index].printInfo();
^
symbol: variable cars
location: class Smith12
Smith12.java:36: error: cannot find symbol
cars[index].drive(miles);
^
symbol: variable cars
location: class Smith12
2 errors
(31 & 36 are the line numbers where the error arises)
The error comes from the for loop, but even before I switched this to a loop and just had them all listed outright, I was getting the same error. I've tried so many things that I'm just beat at this point. Someone please help me out.
Here's the main code since it isn't too long:
import java.util.Scanner;
public class Smith12
{
public static final int NUM_CARS = 3;
public static void main(String[] args)
{
Car[] cars = new Car[NUM_CARS];
cars[0] = new Car( "Toyota Camry", 3400 );
cars[1] = new Car( "Ford F-150", 5000 );
cars[2] = new Car( "Honda Civic", 3000 );
for ( int index = 0 ; index < NUM_CARS ; index++ )
cars[index].drive(10);
printAllCarsInfo();
cars[0].setFuelGallons(5);
Scanner scanner = new Scanner(System.in);
System.out.println("How many miles is your road trip? ");
double roadTrip = scanner.nextDouble();
driveAllCars( 10 );
printAllCarsInfo();
}
public static void printAllCarsInfo() // function for printing info needed for all three cars
{
for ( int index = 0 ; index < NUM_CARS ; index++ )
Car[index].printInfo();
}
public static void driveAllCars( double miles )
{
for ( int index = 0 ; index < NUM_CARS ; index++ )
cars[index].drive(miles);
}
}
And here is the Car class:
public class Car
{
private static String model;
private static double mpg;
private static double milesDriven;
private static double fuelGallons;
public Car( String carModel, double weight )
{
model = carModel;
if ( weight > 4000 )
mpg = 20.0;
else
mpg = 30.0;
milesDriven = 7;
fuelGallons = 15;
}
public String getModel()
{
return model;
}
public double getMPG()
{
return mpg;
}
public double getMilesDriven()
{
return milesDriven;
}
public double getFuelGallons()
{
return fuelGallons;
}
public void setFuelGallons( double gallons )
{
fuelGallons += gallons;
}
public void setMilesDriven( double distance )
{
milesDriven += distance;
}
public double getMilesLeft()
{
return mpg * fuelGallons;
}
public void drive( double miles )
{
if ( miles <= getMilesLeft() )
{
milesDriven += miles;
fuelGallons -= ( miles / mpg );
}
else
{
milesDriven += getMilesLeft();
fuelGallons = 0;
}
}
public void printInfo()
{
if ( fuelGallons > 0 )
System.out.println("The " + this.getModel() + " has driven " + this.getMilesDriven() + " miles and has " + this.getFuelGallons() + " gallons left.");
else
System.out.println("The " + this.getModel() + " has driven " + this.getMilesDriven() + " miles and is out of gas.");
}
}
I've changed some of the variables to static, though I'm not sure it's correct. I've tried the comments but I'm here asking for help because I'm not very good at programming in general. Thank you all for your help and comments.
In static funcions you can only use other static variables and arguments that were passed to this functions. There is no this context.
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.
I keep getting this error message I have checked my file names, and my public class is the same as my .java file. Besides checking that I have no idea where to go.
Main.java:3: error: class ClassGenderPercentages is public, should be declared in a file named ClassGenderPercentages.java
public class ClassGenderPercentages
^
Here is my code
import java.util.Scanner;
public class ClassGenderPercentages
{
public static void main (String args[])
{
Scanner keyboard = new Scanner(System.in);
int maleStudents, femaleStudents;
int totalStudents;
double maleStudentPercentage, femaleStudentPercentage;
maleStudents = keyboard.nextInt();
System.out.println("Enter number of male registered:" + maleStudents);
femaleStudents = keyboard.nextInt();
System.out.println("Enter number of female registered:" + femaleStudents);
totalStudents = (int) (maleStudents + femaleStudents);
maleStudentPercentage = ((100 * maleStudents) / totalStudents);
femaleStudentPercentage = ((100 * femaleStudents) / totalStudents);
System.out.println("The percentage of males registered is: " + maleStudentPercentage + "%");
System.out.println("The percentage of females registed is: " + femaleStudentPercentage + "%");
}
}
Your files is named Main.java, your class ClassGenderPercentages.
You can rename your file to ClassGenderPercentages.java
(that is already said in the error you received from the compiler)
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 9 years ago.
I'm trying to make a menu for a homework assignment for my computer science course. I thought I had everything down, but I keep getting errors that I can't seem to fix. Also I can only use up to Java 1.4.2, so I can't use Scanner. Here is the code:
UPDATED CODE
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class JavaBurger
{
static ArrayList MenuItems = new ArrayList();
public static void main(String[] args)
{
InputStreamReader inp = null;
BufferedReader input = null;
int nOption = 0;
double price = 0;
double amount;
double amount1 = 0;
double amount2 = 0;
double amount3 = 0;
double amount4 = 0;
double amount5 = 0;
double amount6 = 0;
double amount7 = 0;
double amount8 = 0;
double amount9 = 0;
double amount10 = 0;
amount = (amount1 + amount2 + amount3 + amount4 + amount5 + amount6 + amount7 + amount8 + amount9 + amount10);
try
{
inp = new InputStreamReader(System.in);
input = new BufferedReader(inp);
while(true)
{
System.out.println("Choose a Menu Option");
System.out.println("1. Burger - 13.49");
System.out.println("2. Pasta - 16.79");
System.out.println("3. Salad - 13.49");
System.out.println("4. Salmon - 18.99");
System.out.println("5. Chicken - 16.99");
System.out.println("6. Nachos - 13.99");
System.out.println("7. Soup - 6.99");
System.out.println("8. Fajitas - 18.49");
System.out.println("9. Ribs - 23.99");
System.out.println("10. Calamari-9.99");
System.out.println("\nChoose an option(1-10) >> ");
System.out.println("Subtotal: $" + amount);
System.out.println("Total: $" + (amount * 1.13));
nOption = Integer.parseInt(input.readLine());
switch(nOption)
{
case 1:
Burger(input);
break;
case 2:
Pasta(input);
break;
case 3:
Salad(input);
break;
case 4:
Salmon(input);
break;
case 5:
Chicken(input);
break;
case 6:
Nachos(input);
break;
case 7:
Soup(input);
break;
case 8:
Fajitas(input);
break;
case 9:
Ribs(input);
break;
case 10:
Calamari(input);
break;
}
}
}
catch(Exception exp)
{
}
}
public static void Burger(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many Burgers would you like? ");
int a = Integer.parseInt(input.readLine());
int aa = a.nextInt();
double aaa = Math.pow(1 + a, aa);
amount1 = (aaa * 13.49);
break;
}
}
public static void Pasta(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many orders of Pasta would you like? ");
int b = Integer.parseInt(input.readLine());
int bb = b.nextInt();
double bbb = Math.pow(1 + b, bb);
amount2 = (bbb * 16.79);
break;
}
} public static void Salad(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many Salads would you like? ");
int c = Integer.parseInt(input.readLine());
int cc = c.nextInt();
double ccc = Math.pow(1 + c, cc);
amount3 = (ccc * 13.49);
break;
}
} public static void Salmon(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many orders of Salmon would you like? ");
int d = Integer.parseInt(input.readLine());
int dd = d.nextInt();
double ddd = Math.pow(1 + d, dd);
amount4 = (ddd * 18.99);
break;
}
} public static void Chicken(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many orders of Chicken would you like? ");
int e = Integer.parseInt(input.readLine());
int ee = e.nextInt();
double eee = Math.pow(1 + e, ee);
amount5 = (eee * 16.99);
break;
}
} public static void Nachos(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many orders of Nachos would you like? ");
int f = Integer.parseInt(input.readLine());
int ff = f.nextInt();
double fff = Math.pow(1 + f, ff);
amount6 = (fff * 13.99);
break;
}
} public static void Soup(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many orders of Soup would you like? ");
int g = Integer.parseInt(input.readLine());
int gg = g.nextInt();
double ggg = Math.pow(1 + g, gg);
amount7 = (ggg * 6.99);
break;
}
} public static void Fajitas(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many orders of 2 Fajitas would you like? ");
int h = Integer.parseInt(input.readLine());
int hh = h.nextInt();
double hhh = Math.pow(1 + h, hh);
amount8 = (hhh * 18.49);
break;
}
} public static void Ribs(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many racks of Ribs would you like? ");
int i = Integer.parseInt(input.readLine());
int ii = i.nextInt();
double iii = Math.pow(1 + i, ii);
amount9 = (iii * 23.99);
break;
}
} public static void Calamari(BufferedReader input) throws IOException
{
while(true)
{
System.out.println("How many orders of Calamari would you like? ");
int j = Integer.parseInt(input.readLine());
int jj = j.nextInt();
double jjj = Math.pow(1 + j, jj);
amount10 = (jjj * 9.99);
break;
}
}
}
UPDATED ERRORS:
C:\Java\bin>javac JavaBurger.java
JavaBurger.java:98: error: int cannot be dereferenced
int aa = a.nextInt();
^
JavaBurger.java:100: error: cannot find symbol
amount1 = (aaa * 13.49);
^
symbol: variable amount1
location: class JavaBurger
JavaBurger.java:114: error: int cannot be dereferenced
int bb = b.nextInt();
^
JavaBurger.java:116: error: cannot find symbol
amount2 = (bbb * 16.79);
^
symbol: variable amount2
location: class JavaBurger
JavaBurger.java:128: error: int cannot be dereferenced
int cc = c.nextInt();
^
JavaBurger.java:130: error: cannot find symbol
amount3 = (ccc * 13.49);
^
symbol: variable amount3
location: class JavaBurger
JavaBurger.java:143: error: int cannot be dereferenced
int dd = d.nextInt();
^
JavaBurger.java:145: error: cannot find symbol
amount4 = (ddd * 18.99);
^
symbol: variable amount4
location: class JavaBurger
JavaBurger.java:158: error: int cannot be dereferenced
int ee = e.nextInt();
^
JavaBurger.java:160: error: cannot find symbol
amount5 = (eee * 16.99);
^
symbol: variable amount5
location: class JavaBurger
JavaBurger.java:173: error: int cannot be dereferenced
int ff = f.nextInt();
^
JavaBurger.java:175: error: cannot find symbol
amount6 = (fff * 13.99);
^
symbol: variable amount6
location: class JavaBurger
JavaBurger.java:187: error: int cannot be dereferenced
int gg = g.nextInt();
^
JavaBurger.java:189: error: cannot find symbol
amount7 = (ggg * 6.99);
^
symbol: variable amount7
location: class JavaBurger
JavaBurger.java:202: error: int cannot be dereferenced
int hh = h.nextInt();
^
JavaBurger.java:204: error: cannot find symbol
amount8 = (hhh * 18.49);
^
symbol: variable amount8
location: class JavaBurger
JavaBurger.java:217: error: int cannot be dereferenced
int ii = i.nextInt();
^
JavaBurger.java:219: error: cannot find symbol
amount9 = (iii * 23.99);
^
symbol: variable amount9
location: class JavaBurger
JavaBurger.java:232: error: int cannot be dereferenced
int jj = j.nextInt();
^
JavaBurger.java:234: error: cannot find symbol
amount10 = (jjj * 9.99);
^
symbol: variable amount10
location: class JavaBurger
20 errors
Errors:
You are using null variables:
double amount;
double amount1;
None of these are initialized with any value. They are all null. You can inline the definition and give them all proper values.
double amount = 0, amount1 = 2, amount3 = 5;//etc
You are defining amount twice:
double amount;
double amount = (amount1 + amount2 + amount3 + amount4 + amount5 + amoun
t6 + amount7 + amount8 + amount9 + amount10);
Remove the double from the second one:
amount = (amount1 + amount2 + amount3 + amount4 + amount5 + amoun
t6 + amount7 + amount8 + amount9 + amount10);
Next, you always need to cast from String to Double.
(input.readLine() * 18.49); // should be
Double.parseDouble(input.readLine()) * 18.49;
The readLine() method does not return a numeric value.
Try declaring your amounts as global variables outside the main method. What the cannot find symbol error means is the program can't access the variables you're telling it to access.
Also, you might want to try updating the total amount after you update the original amounts in each method.
You try to declare amount twice:
double amount;
double amount1;
[...]
double amount = (amount1 + amount[...]
remove the double from the last line to make it just an assignment.
amount1 = (input.readInt * 13.49);
BufferedReader has no readInt method. You might want to replace this with
amount1 = (Integer.parseInt(input.readLine) * 13.49);
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.