-1

import java.util.Scanner;

public class predictStockMarket {

public static void main(String args[]){
    int openingPrice;
    int currentPrice;
  int numberOfStocks;
    String buyOrSell;

    Scanner number=new Scanner(System.in);
  Scanner text=new Scanner(System.in);

    System.out.println("What is the Opening Price? ");//Asking for the opening price
    openingPrice=number.nextInt();            //storing into current price variable

    System.out.println("What is the Current Price? ");//Asking for the Current Price
    currentPrice=number.nextInt();            //storing into current price variable

    System.out.println("Do you want to buy or sell? ");//Asking if they want to buy or sell
    buyOrSell=text.nextLine();          

     if (buyOrSell.equalsIgnoreCase("buy")){
           System.out.println("How many stock do you want to buy ");//Asking the user for hours per week
        buyNumber=number.nextLine(); 
        int i;

        i = (buyNumber*currentPrice)-(openingPrice*buyNumber);
        System.out.println(i);

      }
         else {
          System.out.println("How many stock do you want to sell");
          sellNumber=number.nextLine();

           int i;
           i = (sellNumber*currentPrice)-(openingPrice*sellNumber);
            System.out.println(i);

     }

Extremely confused, i thought putting a local variable would make it an integer. Using Jgrasp

  • 2
    Well yes, you're not assigning any value to `buyOrSell`. What value do you expect it to have when you're comparing it with `"buy"`? You do realize that you've *commented out* the line where you'd otherwise assign a value, right? – Jon Skeet Dec 06 '16 at 19:44

1 Answers1

1

Uncomment this line:

// buyOrSell=text.nextLine(); 

Since it is commented, no value is assigned to it. What leads to a problem if you then try to access it :

 if (buyOrSell.equalsIgnoreCase("buy")){
UDKOX
  • 738
  • 3
  • 15