0

Im just a beginner and anything would be helpful. So basically this program asks a question and depending on input, affects the price of tshirts. Also it asks how many tshirts wanted to be purchased also affecting the price. It keeps on running until "exit" is typed.

The problem I have is that I want the output to have the total sales and other info that was accumulated from the if/else if statements before. But when I compile it says local variable may not have been initialized. Any help would be much appreciated

import java.util.Scanner;
class ScannerTest{
public static void main (String args[]){
 int b, c;
 String endProgram = "exit";
 String userInput;
 java.util.Scanner input = new java.util.Scanner(System.in);


 do{
    System.out.println("\n Welcome to the SAC t-shirt sales software"); 
    System.out.println("\n Does the student have an activity fee? Yes/No/Exit");
    userInput = input.nextLine();
    if (userInput.equalsIgnoreCase("yes")){
        System.out.println("How many Tshirts do you want to buy?");
        b = input.nextInt();
        input.nextLine(); 
        System.out.println("Cost:" + b*5 + " dollars");
    }
    else if(userInput.equalsIgnoreCase("no")){
        System.out.println ("How many Tshirts do you want to buy?");
        c = input.nextInt(); 
        input.nextLine();
        System.out.println("Cost:" + c*6 + " dollars");
    }


 }while (!userInput.equalsIgnoreCase(endProgram));

 System.out.println ("Activity Card Sales:" + b + "Non-activity Card Sales:" + c + "Total Money Collected" + ((b*5)+(c*6)));

 input.close();
 }
}
azro
  • 53,056
  • 7
  • 34
  • 70
Adam Lam
  • 3
  • 2
  • Don't sure what you want to see before what ? – azro Sep 09 '17 at 16:21
  • Please always [search the site first](https://www.google.com/search?q=site%3Astackoverflow.com+java+local+variables+may+have+not+been+initialized) (please click on the link) before asking. – Hovercraft Full Of Eels Sep 09 '17 at 16:21
  • Since your variables are declared in a function, they will not be initialized with the default values. You have to explicitly initialize them with values. You can initialize them to an integer and the code will compile. (b=0, c=0) or any other default value that you like. – Abhinav P Sep 09 '17 at 16:21

1 Answers1

0

just set in the begging b and c to 0 and you will have no problems.

Antonio
  • 24
  • 4