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

    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("");
    entireInventoryValueText.setColumns(30);
    entireInventoryValueText.setEditable(false);

    cameraInventoryValueText = new JTextField("");
    cameraInventoryValueText.setEditable(false);

    cameraRestockingFeeText = new JTextField("");
    cameraRestockingFeeText.setEditable(false);

    cameraPriceText = new JTextField("");
    cameraPriceText.setEditable(false);

    numberInStockText = new JTextField("");
    numberInStockText.setEditable(false);

    cameraWeightText = new JTextField("");
    cameraWeightText.setEditable(false);

    cameraTypeText = new JTextField("");
    cameraTypeText.setEditable(false);

    cameraNameText = new JTextField("");
    cameraNameText.setEditable(false);

    cameraIDText = new JTextField("");
    cameraIDText.setColumns(30);
    cameraIDText.setEditable(false);

    // 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
    index++;
    // if we've gone forward back too far, then wrap around
    if ( index == numberOfCameras ) {
    index = 0;
    }
    repaint();
    }
    });

    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));
    buttonPanel.add(nextButton);

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

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

    // 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));
    centerPanel.add(cameraNameLabel);
    centerPanel.add(cameraNameText);

    centerPanel.add(cameraIDLabel);
    centerPanel.add(cameraIDText);

    // row 2
    centerPanel.add(cameraTypeLabel);
    centerPanel.add(cameraTypeText);

    // row 3
    centerPanel.add(cameraWeightLabel);
    centerPanel.add(cameraWeightText);

    // row 4
    centerPanel.add(numberInStockLabel);
    centerPanel.add(numberInStockText);

    // row 5
    centerPanel.add(cameraPriceLabel);
    centerPanel.add(cameraPriceText);

    // row 6
    centerPanel.add(cameraRestockingFeeLabel);
    centerPanel.add(cameraRestockingFeeText);

    // row 7
    centerPanel.add(cameraInventoryValueLabel);
    centerPanel.add(cameraInventoryValueText);

    // 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( System.in );

    // 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
    cameraGUIFrame.pack();
    cameraGUIFrame.setVisible(true);
    repaint();

    }

    // 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());
    cameraNameText.setText(theCamera.getName());
    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
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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
    Posts
    3
    Rep Power
    0

    Default

    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
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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
    Posts
    3
    Rep Power
    0

    Default

    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
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

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