I get this message when I try to compile may work. Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - inner classes cannot have static declarations
But I am so new at this that when I read your suggestion of fixing the problem, because there are two classes, I know what you are speaking of but I am not sure how to fix it.

This is the code, but I just don't what to do to fix it.

import java.util.ArrayList;
import java.util.*;


/**
 *
 * @author gail
 */
public class CashForMetals2
{


  //welcome method for customers
    public static void printWelcome()
    {
        System.out.println(
                "***                            ***");
        System.out.println(
                "***    Welcome to Cash for Metals Calculator!!!    ***");
        System.out.println(
                "***                            ***");
    }
    private class Customer
    {
    private long customerId;
    private String customerName;
    private String customerAddress;
    private String customerHomePhone;
    private String customerWorkPhone;



    public Customer(String name)
    {
       customerName = name;
       SetCustomerId();
    }

       public Customer()
    {
       SetCustomerId();
    }


    public void SetCustomerId()
    {
        customerId =  (long)(Math.random() * 100000);
    }

    public long GetCustomerId()
    {
       return  customerId ;
    }

    public String GetCustomerName()
    {
        return customerName;
    }

    public void setCustomerName(String name)
    {
       customerName = name;
    }

    public String GetCustomerAddress()
    {
        return customerAddress;
    }

    public void setCustomerAddress(String address)
    {
       customerAddress = address;
    }

 public String GetCustomerHomePhone()
    {
        return customerHomePhone;
    }

    public void setCustomerHomePhone(String HomePhone)
    {
       customerHomePhone = HomePhone;
    }

    public String GetCustomerWorkPhone()
    {
        return customerWorkPhone;
    }

    public void setCustomerWorkPhone(String WorkPhone)
    {
       customerWorkPhone = WorkPhone;
    }
   public String toString()
    {
 return
       "Customer ID " + customerId + "\n" +
       "Customer Name " + customerName+ "\n" +
       "Customer Address " + customerAddress;
    }

     public static void main( String[] args )

     {
    //create new Customer ArrayList
         int NumberOfCustomers = 0;
       String EnterAnotherCustomer ="";

     ArrayList<Customer> customerArray = new ArrayList<Customer> ();

     Scanner input = new Scanner( System.in );
        do
        {
         System.out.print ("Enter Customer Name :  ");
         String name = input.nextLine();
         System.out.print("Enter Customer Address :  ");
         String address = input.nextLine();


     Customer customer = new Customer();
     customer.setCustomerAddress(address);
     customerArray.add(customer);
     NumberOfCustomers++;

     System.out.print("Enter YES to enter another Customer ");
         EnterAnotherCustomer = input.nextLine();
        }
         while(EnterAnotherCustomer.trim().equalsIgnoreCase("YES"));

         System.out.print(NumberOfCustomers);



//end class arraylist
//enhanced for loop iterate through elements in arraylist and print
 for(Customer c : customerArray)
         {
             System.out.println(c);
             System.out.println("***********************************");
         }
          System.out.print(NumberOfCustomers);
     }
    }
    }

Recommended Answers

All 2 Replies

Can you post the full text of the error message? The error message should have the source code line number in it. You've left that off when you entered the error message.

Also use code tags when posting code to keep its formatting.

I strongly suggest that you use an IDE instead of just a text editor and console to compile - it will help you to debug your own programs :)

I added your code to a new project in Eclipse and it claimed that the error was on this line:

public static void main( String[] args )

With the error message "The method main cannot be declared static; static methods can only be declared in a static or top level type".

There were two suggestions:

  • Remove 'static' modifier of 'main'(..)
  • Add 'static' modifier to parent type

Someone may come in to correct me later and say that I've made the wrong decision here, but I'd suggest doing the second suggestion.

Go up to where you have this line, and add the static modifier:

private  class Customer

..becomes..

private static  class Customer

----------------------------------------------------

Your program will then compile. I tried using it, and found another problem that you will need to fix :

Enter Customer Name :  leiger
Enter Customer Address :  MyAddress
Enter YES to enter another Customer No
1Customer ID 70846
Customer Name null
Customer Address MyAddress
***********************************
1

Note how the customer name is displayed as 'null'.

Why is this happening? It's fairly easy to spot - just look at your code where you ask the user for input and then create the Customer object. Something is missing... ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.