-
problem with variables
Hey! I'm new to all this so be kind :o! I am having trouble with variables, I get the following errors and don't know how to fix it! Any help would be greatly appreciated! Thanks!
----jGRASP exec: javac -g H:\insurance_sales.java
insurance_sales.java:28: non-static variable carValue cannot be referenced from a static context
carValue = 22000;
^
insurance_sales.java:31: non-static variable carValue cannot be referenced from a static context
carValue = 23000;
^
insurance_sales.java:34: non-static variable carValue cannot be referenced from a static context
carValue = 30000;
^
insurance_sales.java:37: non-static variable carValue cannot be referenced from a static context
carValue = 100000;
^
insurance_sales.java:40: non-static variable carValue cannot be referenced from a static context
carValue = 15000;
^
insurance_sales.java:43: non-static variable carValue cannot be referenced from a static context
carValue = 26000;
^
insurance_sales.java:51: non-static variable fuelType cannot be referenced from a static context
if ( fuelType = "Petrol" ) //IF statements assigns value to petrol/diesel
^
insurance_sales.java:51: incompatible types
found : java.lang.String
required: int
if ( fuelType = "Petrol" ) //IF statements assigns value to petrol/diesel
^
insurance_sales.java:51: incompatible types
found : int
required: boolean
if ( fuelType = "Petrol" ) //IF statements assigns value to petrol/diesel
^
insurance_sales.java:52: non-static variable fuelType cannot be referenced from a static context
fuelType = 350;
^
insurance_sales.java:55: non-static variable fuelType cannot be referenced from a static context
if ( fuelType = "Diesel")
^
insurance_sales.java:55: incompatible types
found : java.lang.String
required: int
if ( fuelType = "Diesel")
^
insurance_sales.java:55: incompatible types
found : int
required: boolean
if ( fuelType = "Diesel")
^
insurance_sales.java:56: non-static variable fuelType cannot be referenced from a static context
fuelType = 200;
^
insurance_sales.java:62: cannot find symbol
symbol : variable transmisson
location: class insurance_sales
switch (transmisson) {
^
insurance_sales.java:64: cannot find symbol
symbol : variable transmissonPrice
location: class insurance_sales
transmissonPrice = 200;
^
insurance_sales.java:67: cannot find symbol
symbol : variable transmissonPrice
location: class insurance_sales
transmissonPrice = 400;
^
insurance_sales.java:79: non-static variable enginePrice cannot be referenced from a static context
enginePrice = 300;
^
insurance_sales.java:82: non-static variable enginePrice cannot be referenced from a static context
enginePrice = 500;
^
insurance_sales.java:85: non-static variable enginePrice cannot be referenced from a static context
enginePrice = 600;
^
insurance_sales.java:88: non-static variable enginePrice cannot be referenced from a static context
enginePrice = 800;
^
insurance_sales.java:91: non-static variable enginePrice cannot be referenced from a static context
enginePrice = 1000;
^
insurance_sales.java:96: non-static variable totalPrice cannot be referenced from a static context
System.out.println("Premium payable " + totalPrice);
^
23 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
My code looks like this
public class insurance
{
String Name;
String Address;
String carName;
String fuelType;
String carValue;
int Age;
String transmission;
int engineSize;
public insurance (String Name, String Address, String carName, int Age,String Transmission, int engineSize)
{
this.Name = Name;
this.Address = Address;
this.carName = carName;
this.Age = Age;
this.transmission = transmission;
this.engineSize = engineSize;
}
String Name()
{
return Name;
}
String Address()
{
return Address;
}
String carName()
{
return carName;
}
String transmisson()
{
return transmission;
}
String carValue()
{
return carValue;
}
String fuelType()
{
return fuelType;
}
int engineSize()
{
return engineSize;
}
}
__________________________________________
import java.util.Scanner;
class insurance_sales //name of class
{
String Name = "";
String Address = "";
int carName = 0;
int fuelType = 0;
int transmissionPrice =0;
int engineSize = 0;
int carValue = 0;
int enginePrice = 0;
int totalPrice = carValue + fuelType + transmissionPrice + enginePrice;
public static void main(String args[])
{
System.out.println("!Welcome to Cheap Insurances.Com!");
System.out.println(" Please fill in the information, if you are eligible for cover, a quote will be given");
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the make of car:");
int carName = scan.nextInt();
switch (carName) { //switch statement for the name of the car and to assign the values to each car
case 1: System.out.println("Ford");
carValue = 22000;
break;
case 2: System.out.println("Fiat");
carValue = 23000;
break;
case 3: System.out.println("BMW");
carValue = 30000;
break;
case 4: System.out.println("Ferrari");
carValue = 100000;
break;
case 5: System.out.println("Opel");
carValue = 15000;
break;
case 6: System.out.println("Alfa");
carValue = 26000;
break;
default: System.out.println("Invalid Car.");break;
}
System.out.println("Please choose fuel type (Petrol/Diesel?):");
String fuel = scan.nextLine();
if ( fuelType = "Petrol" ) //IF statements assigns value to petrol/diesel
fuelType = 350;
else
if ( fuelType = "Diesel")
fuelType = 200;
System.out.println("Please choose transmission type: Manual/Automatic");
int transmission = scan.nextInt(); //switch statement to choose transmission type
switch (transmisson) {
case 1: System.out.println("Manual");
transmissonPrice = 200;
break;
case 2: System.out.println("Automatic");
transmissonPrice = 400;
break;
default: System.out.println("Invalid type");
break;
}
System.out.println("Please enter engine size (In cc only eg. 1200cc, 2000cc");
//IF statement to determine the engine size and price
int engineSize = scan.nextInt();
if ( engineSize >= 1200 )
enginePrice = 300;
else
if ( engineSize >= 1400)
enginePrice = 500;
else
if ( engineSize >= 1600)
enginePrice = 600;
else
if ( engineSize >= 1800)
enginePrice = 800;
else
if ( engineSize < 2000)
enginePrice = 1000;
System.out.println("Premium payable " + totalPrice);
}
}
-
Code:
class insurance_sales //name of class
{
String Name = "";
String Address = "";
[B]int carName = 0;[/B]
int fuelType = 0;
int transmissionPrice =0;
int engineSize = 0;
int carValue = 0;
int enginePrice = 0;
int totalPrice = carValue + fuelType + transmissionPrice + enginePrice;
public static void main(String args[])
{
System.out.println("!Welcome to Cheap Insurances.Com!");
System.out.println(" Please fill in the information, if you are eligible for cover, a quote will be given");
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the make of car:");
[B]int carName [/B]= scan.nextInt();
Man you declared two times on one variable.Be attentive.
-
= != ==
You have to correct the comparation operator in your "if" statements.
Code:
if ( fuelType [B][COLOR="Red"]=[/COLOR][/B] "Petrol" )
.
.
.
if ( fuelType [COLOR="red"][B]=[/B][/COLOR] "Diesel")
Use "=="
Luck,
CJSL
-
You're referencing instance variables from within your main method, which is a static method (method belonging to the class, not to an instance). You need to instantiate your class first before referencing those variables.
-
Another one...
Found another glitch...
fuelType is define as an int, but is being compared against a string
Code:
[B][COLOR="Red"]int[/COLOR][/B] fuelType = 0;
.
.
.
if ( fuelType == [B][COLOR="red"]"Petrol"[/COLOR][/B] ) //this is the correct comparation I mentioned in my previous post
Luck,
CJSL