I am still learning java. I can’t figure out this error.
import java.util.Scanner;
public class PointOfSale {
public static void main(String args[]) {
String item;
int stock1 = 1000;
int stock2 = 1000;
int stock3 = 1000;
float price1 = 40.00f;
float price2 = 50.00f;
float price3 = 30.00f;
int q1,q2,q3;
float totalP1, totalP2, totalP3, total;
Scanner sc = new Scanner(System.in);
while (stock1 >0| stock2 >0 | stock3 >0 ) {
System.out.println("We have "+ stock1 + " of Item 1.");
System.out.println("We have "+ stock2 + " of Item 2.");
System.out.println("We have "+ stock3 + " of Item 3.");
System.out.print("Enter The Item : ");
item = sc.next();
if (item=="Item 1")
{System.out.print("Enter Quantity : ");
q1 = sc.nextInt();
totalP1 = q1*price1;
stock1 = stock1 -q1;}
else
{System.out.print("Enter The Item : ");
item = sc.next();
System.out.print("Enter Quantity : ");
q2 = sc.nextInt();
totalP2 = q2*price1;
stock2 = stock2 -q2;
System.out.print("Enter The Item : ");
item = sc.next();
System.out.print("Enter Quantity : ");
q3 = sc.nextInt();
totalP3 = q3*price3;
stock3 = stock3 -q3;}
total = totalP1 + totalP2 + totalP3 ;
System.out.println("Total Bill : " + total);
System.out.println("Remaing Stock of Item 1 : " + stock1);
System.out.println("Remaing Stock of Item 2 : " + stock2);
System.out.println("Remaing Stock of Item 3 : " + stock3);
System.out.println();
System.out.println();
System.out.println();
}//while loop end
System.out.print("No more items.");
}// main end
}// class end
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable totalP1 may not have been initialized
The local variable totalP2 may not have been initialized
The local variable totalP3 may not have been initialized
at PointOfSale.main(PointOfSale.java:51)
If i delete the code with out the if statement I can run the program.
(My goal is to create a simple sales program where the customer can buy any 3 of the given 3 items. After the sale the total bill and the remaining stock should be the output.)