Results 1 to 7 of 7
  1. #1
    Karnij is offline Member
    Join Date
    Jun 2009
    Rep Power

    Default Problems compiling

    Hello I very new to Java, and I am having problems with this program I can not get it to compile or run. Can someone please help me figure this out it for a class I am taking

    Here is the code

    * To change this template, choose Tools | Templates
    * and open the template in the editor.

    * @author Chris

    // Program uses class Scanner to accept user input
    // Program Uses Class NumberFormat to format number output
    // Program Needs to Use U.S. Currency

    import java.util.Scanner;
    import java.text.NumberFormat;
    import java.util.Locale;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class InventoryProgram4
    private int MaxNumberOfCameras = 100;

    // array of cameras form the inventory
    private Camera myCamera[] = new Camera[MaxNumberOfCameras]; // Create a camera array
    // number of cameras that the user has stored
    private int numberOfCameras = 0;
    // location of the camera in the array that is on the screen
    private int index = 0;

    // Set Currency to U.S. Dollars
    NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);

    // GUI elements to display currently selected camera's information
    private JLabel cameraIDLabel;
    private JTextField cameraIDText;

    private JLabel cameraNameLabel;
    private JTextField cameraNameText;

    private JLabel cameraTypeLabel;
    private JTextField cameraTypeText;

    private JLabel cameraWeightLabel;
    private JTextField cameraWeightText;

    private JLabel numberInStockLabel;
    private JTextField numberInStockText;

    private JLabel cameraPriceLabel;
    private JTextField cameraPriceText;

    private JLabel cameraRestockingFeeLabel;
    private JTextField cameraRestockingFeeText;

    private JLabel cameraInventoryValueLabel;
    private JTextField cameraInventoryValueText;

    private JLabel entireInventoryValueLabel;
    private JTextField entireInventoryValueText;

    private JFrame cameraGUIFrame;

    // setup buttons
    // button to display next inventory item
    JButton nextButton;

    public InventoryProgram4() {
    // setup the element of the GUI
    cameraNameLabel = new JLabel("Name:");
    cameraIDLabel = new JLabel("ID:");
    cameraTypeLabel = new JLabel("Type:");
    cameraWeightLabel = new JLabel("Weight:");
    numberInStockLabel = new JLabel("Unit in Stock:");
    cameraPriceLabel = new JLabel("Unit Price:");
    cameraRestockingFeeLabel = new JLabel("Restocking fee:");
    cameraInventoryValueLabel = new JLabel("Camera Inventory Value");
    entireInventoryValueLabel = new JLabel("Overall Camera Inventory Value");

    entireInventoryValueText = new JTextField("");

    cameraInventoryValueText = new JTextField("");

    cameraRestockingFeeText = new JTextField("");

    cameraPriceText = new JTextField("");

    numberInStockText = new JTextField("");

    cameraWeightText = new JTextField("");

    cameraTypeText = new JTextField("");

    cameraNameText = new JTextField("");

    cameraIDText = new JTextField("");

    // setup the code for the buttons
    nextButton = new JButton("Next");
    nextButton.addActionListener(new ActionListener() // register event handler
    public void actionPerformed(ActionEvent event) // process button event
    // update the current item to be displayed by the inventory
    // if we've gone forward back too far, then wrap around
    if ( index == numberOfCameras ) {
    index = 0;

    cameraGUIFrame = new JFrame(); // JFrame container
    cameraGUIFrame.setLayout(new BorderLayout()); // set layout

    // create and setup panel to hold buttons
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(1, 4, 0, 4));

    JPanel entireInventoryValuePanel = new JPanel();
    entireInventoryValuePanel.add(entireInventoryValue Label);
    entireInventoryValuePanel.add(entireInventoryValue Text);

    JPanel infoPanel = new JPanel();
    infoPanel.setLayout(new GridLayout(2, 1));

    // setup a panel to collect all the components.
    // this will have 8 rows and 2 columns
    JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));


    // row 2

    // row 3

    // row 4

    // row 5

    // row 6

    // row 7

    // add the panel to the center of the GUI's window
    cameraGUIFrame.getContentPane().add(centerPanel, BorderLayout.CENTER);

    // adds buttons to frame
    cameraGUIFrame.getContentPane().add(infoPanel, BorderLayout.SOUTH);

    // title of window
    cameraGUIFrame.setTitle("Camera Inventory");

    // quit the program when the window is closed
    cameraGUIFrame.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);

    // create Scanner to obtain input from command window
    Scanner input = new Scanner( );

    // Show Program Welcome Message
    System.out.println( ); // Show an Empty Line
    System.out.println( "Welcome to the Inventory Program!!!"); // Show Welcome Message
    System.out.println( ); // Show an Empty Line

    // Prompt for the number of cameras
    System.out.print( "Please enter the number of digital cameras: "); // prompt message
    numberOfCameras = input.nextInt(); // read the number of cameras
    input.nextLine(); // Need this to get leftover carriage return --> input queue cleanup

    // Loop for Camera Data Entry
    for (int i = 0; i < numberOfCameras; i++)

    myCamera[i] = new Camera(); // Instantiate the object --> Create object memory space

    // Show an Empty Line
    System.out.println( );

    // Prompt for Camera Type
    System.out.print( "Enter a Camera #" + (i+1) + " Name: " ); // prompt message
    myCamera[i].setName(input.nextLine()); // read the camera name

    // Prompt for Camera ID
    System.out.print( "Enter an ID for Camera #" + (i+1) + ": " ); // prompt message
    myCamera[i].setID(input.nextLine()); // read the camera ID

    // Prompt for Camera Type
    System.out.print( "Enter the type for Camera #" + (i+1) + ": " ); // prompt message
    myCamera[i].setType(input.nextLine()); // read the camera type

    // Prompt for Camera Weight
    System.out.print( "Enter the weight for Camera #" + (i+1) + ": " ); // prompt message
    myCamera[i].setWeight(input.nextFloat()); // read the camera weight

    // Prompt for Camera's Unit Price
    System.out.print( "Enter the Unit Price for Camera #" + (i+1) + " ($): " ); // prompt message
    myCamera[i].setUnitPrice(input.nextFloat()); // read the camera's unit price

    // Prompt for Camera's Stock Inventory
    System.out.print( "Enter the Number of Camera#" + (i+1) + " Units in Stock: " ); // prompt message
    myCamera[i].setUnitsInStock(input.nextLong()); // read the number Camera's in stock

    input.nextLine(); // Need this to get leftover carriage return --> input queue cleanup


    sortCameraInventory(myCamera, numberOfCameras);

    // display the GUI on screen


    // each time the program data is modified, called repaint() to get the most recent information on the screen
    public void repaint() {

    // display the current item on the screen's text area
    // get the Camrea at the current index

    Camera theCamera = myCamera[index];

    // print out its information to the screen
    if (theCamera != null) {

    cameraInventoryValueText.setText(currency.format(t heCamera.getTotalInventoryValue()));
    cameraRestockingFeeText.setText("" + currency.format(theCamera.getUnitPrice() * theCamera.getRestockingFee()/100));
    cameraPriceText.setText(currency.format(theCamera. getUnitPrice()));
    numberInStockText.setText("" + theCamera.getUnitsInStock());
    cameraWeightText.setText("" + theCamera.getWeight());
    cameraTypeText.setText("" + theCamera.getType());
    cameraIDText.setText("" + theCamera.getID());


    entireInventoryValueText.setText(currency.format( getTotalInventoryValue(myCamera, numberOfCameras) ) );

    // Method that Computes the Overall Value of a Camera Inventory Array
    public static float getTotalInventoryValue(Camera cameraInventory[], int numberOfCameras)

    float totalInventoryValue = 0; // Placeholder for the overall inventory value

    // Loop through all the cameras in the supplied inventory array
    for (int i = 0; i < numberOfCameras; i++)

    totalInventoryValue = totalInventoryValue + cameraInventory[i].getTotalInventoryValue(); // Add the total value of a camera to the overall camera inventory value


    return totalInventoryValue; // Return the overall camera inventory value for the supplied camera array

    } // end getTotalInventoryValue method

    // Method that Sorts the Camera Inventory Based on the Camera Name
    public static void sortCameraInventory(Camera cameraInventory[], int numberOfCameras)

    // Temporary Camera Placeholder
    Camera tempCamera = new Camera();

    // Loop that specifies the last element in the sorting comparisons
    for (int j = numberOfCameras; j > 0; j--)

    // Loop throught the camera elements
    for (int i = 0; i < (j-1); i++)

    // Compare the names of the camera elements and swap them, if necessary, to get them in the correct order
    if ( cameraInventory[i].getName().compareToIgnoreCase( cameraInventory[i+1].getName() ) > 0 )

    // Swap the two camera positions in the camera inventory array
    tempCamera = cameraInventory[i+1]; // Temporarily store camera i+1 in tempCamera
    cameraInventory[i+1] = cameraInventory[i]; // Replace the "i+1" camera element with the "i" camera element
    cameraInventory[i] = tempCamera; // Set the "i" camera element to the previous "i+1" camera element value

    } // end if statement

    } // end for loop

    } // end for loop

    } // end sortCameraInventory method

    // main method begins execution of Java application
    public static void main( String args[] )
    new InventoryProgram4();

    } // end method main

    } //end class InventoryProgram4

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    I am having problems with this program I can not get it to compile or run
    A few points:

    Use the code button (it looks like #) when posting code. Alternatively put [code] at the start of your code and [/code] at the end. This will make the code readable.

    Say why the code won't compile. That is copy paste and post the exact and entire messages that the compiler is giving you. These messages are very useful and provide your first step towards solving your problem.

    That's a lot of code. Consider starting again and working one step at a time making sure at each step that everything compiles and runs as you expect.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    And also, if you are not keen in Java, please start learn from the beginning. Seems to me you copied this code segment from somewhere and paste here. It's ok if you have complete idea about the code. If so looking at the error message you can find the issue. :)

    Anyway, send your complete error message here to see. It's very useful to us, in commenting pbrockway2 explain in the above post.

  4. #4
    Karnij is offline Member
    Join Date
    Jun 2009
    Rep Power


    ok sorry, thank you for the posting tips

    Ok this will not compile or run because it does not have a main class set.

    now if this is java 101 error then I lost

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    What you mean Java 101? If you comes with an error please post it here to see. It's easy to us.

  6. #6
    Karnij is offline Member
    Join Date
    Jun 2009
    Rep Power


    I was just referring to Java 101 a the basic learning of Java, but i got the problem fixed, thanks for trying to help

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    Okay fine. Once you comes with an error in your application, please read it more carefully. Actually all the thing you want to do is there, to fix your issue. And at the same time, try to refer the Java doc as well. Good luck! ;)

    If you have solve the problem please mark it as solved from the thread tools menu.

Similar Threads

  1. compiling problems.
    By Tikasas in forum New To Java
    Replies: 7
    Last Post: 04-13-2009, 10:00 PM
  2. Replies: 2
    Last Post: 03-20-2009, 10:58 AM
  3. Having error while compiling
    By Kodeee in forum New To Java
    Replies: 12
    Last Post: 03-17-2009, 11:08 AM
  4. Package Compiling
    By kaperks in forum Advanced Java
    Replies: 1
    Last Post: 02-23-2009, 02:56 PM
  5. problems when compiling
    By valery in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts