Need help with a java program

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(;
		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 =;
			if (item=="Item 1")
			{System.out.print("Enter Quantity : ");
			q1 = sc.nextInt();
			totalP1 = q1*price1; 
			stock1 = stock1 -q1;}
			{System.out.print("Enter The Item : ");
			item =;
			System.out.print("Enter Quantity : ");
			q2 = sc.nextInt();
			totalP2 = q2*price1; 
			stock2 = stock2 -q2;
			System.out.print("Enter The Item : ");
			item =;
			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);
		}//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(

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.)

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Local variables in Java need to be initialized (explicitly assigned a value) before being read. If you enter the if-block then the variable totalP1 is assigned a value (and totalP2 and totalP3 are not). However, if you enter the else-block then the variables totalP2 and totalP3 are assigned a value (and totalP1 is not). This means when your code reaches the line total = totalP1 + totalP2 + totalP3 at least one variable that it references/reads is uninitialized. That’s the error that the compiler is warning you about.

The fix is to set totalP1, totalP2, and totalP3 to some value (0.0 is fine) when you declare them.