0

I cant seem to get he code I wrote for my Java programming class to work.

/*Coby Bushong
Java Programming
Professor Lehman
August 9th 2014*/

import java.util.Scanner; // Needed for the Scanner class

/*This program will calculate the commission of a stock broker
at a 2% commission given the number of shares at a contant rate.*/

public class StockCommission 
{
   public static void main(String[] args)
   {
      //Constants
      final double STOCK_VALUE = 21.77;       //price per share
      final double BROKER_COMMISSION = 0.02;  //broker commission rate

      //variables
      double shares;     //To hold share amount
      double shareValue; //To hold total price of shares
      double commission; //To hold commission
      double total;      //To hold total cost

      //create scanner for keyboard input
      Scanner Keyboard = new Scanner(System.in);

      //creates dialog box for user input
      String inputShares;
      inputShares = JOptionPane.showInputDialog(null, "How many stocks do you own?");
      shares = Integer.parseInt(inputShares);

      //calculate total
      shareValue = shares * STOCK_VALUE;

      //calculate commission
      commission = shareValue * BROKER_COMMISSION;

      //calculate total
      total = commission + shareValue;

      //display results
      System.out.println("Shares Owned:" + shares);
      System.out.println("Value of Shares:         $" + shareValue);
      System.out.println("Comission:         $" + commission);
      System.out.println("Total:       $" + total);
   }
}

I get this error:

Errors: 1

StockCommission.java:30: error: cannot find symbol
  inputShares = JOptionPane.showInputDialog(null, "How many stocks do you own?");
                ^
PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
Coby Bushong
  • 17
  • 1
  • 7

2 Answers2

4

In Java, you have to specify where you want to find a class. Here, that class is JOptionPane. It is located in the javax.swing package.

You can read about using packages in here. Basically, you'll have to either

  • use the fully qualified class name:

    inputShares = javax.swing.JOptionPane.showInputDialog(null, "How many stocks do you own?");
    
  • import the class or the whole package in the top of the file:

    import javax.swing.JOptionPane;
    

    or

    import javax.swing.*;
    

    The latter method of importing the whole package is generally considered bad practice, so I would not recommend doing that.

Community
  • 1
  • 1
PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
3

You have to import the JOptionPane class; add this at the top of the file:

import javax.swing.JOptionPane;
Óscar López
  • 232,561
  • 37
  • 312
  • 386