Results 1 to 8 of 8
  1. #1
    abeidson is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Error when adding an array into main method

    New to this forum and I have searched via google and other engines for the past 2 hours with no luck. Sorry if this is easy I for some reason just can figure out why it doesn't like my array declaration.

    I am getting the following error:

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol: class custArray
    location: class salesaccounts.SalesAccountTest
    at salesaccounts.SalesAccountTest.main(SalesAccountTe st.java:25)

    The code that i am getting this in is as follows. I know that it has to do with the array but not sure what is wrong. I am using Java How To Program as a reference by Deitel and this is almost identical to the example in the book. Any ideas?

    Thanks

    Java Code:
    public class SalesAccountTest {
        
         
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
           
         // Initiate an array for data entry
            
            custArray[] customer = new custArray[4];
            
            // customer records
            
            customer[ 0 ] = new SalesAccountSupplies( 12345, "Some Customer Here", 123.56, 5648.65, 9584.56);
            customer[ 1 ] = new SalesAccountsPaper( 65891, "Best Paper Customer Ever", 900, 6.98);
            customer[ 2 ] = new SalesAccountsServices( 78415, "Services Customer", 123.99, 200);
            customer[ 3 ] = new SalesAccountsServices( 95863, "Andy Eidson", 165.32, 500);
            
            for ( custArray currentCust : customer) {
                //output the proper customer, sales and total
                
                System.out.printf("%s \n", currentCust.toString());
            }
          }
         }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Error when adding an array into main method

    Have you written a class called custArray? Is it in your classpath when you try to compile this?

    BTW, custArray is a pretty bad name for a class. Read the link in my sig.
    Get in the habit of using standard Java naming conventions!

  3. #3
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: Error when adding an array into main method

    If you have not created a class called custArray, try this syntax:

    datatype[] customer = new datatype[4];
    Last edited by dalythe; 03-12-2013 at 07:18 AM.

  4. #4
    abeidson is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Error when adding an array into main method

    kjkrum - No I do not have a class called custArray. I am just trying to add a basic array and add data that is being formatted by other classes (SalesAccountSupplies, SalesAccountsPaper, and SalesAccountsServices). So do I need to add an actual class that converts the different datatypes into their own array? In both examples for the text I have the array was built right into main method like I have above. I can provide the code for the other classes well if necessary.

    dalythe - I can't actually add a specific datatype as there are multiple datatypes being processed by the method. I tried String and got an incompatible Datatype message on the lines where I am populating the array.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Error when adding an array into main method

    Maybe you just need a better understanding of arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Error when adding an array into main method

    Every array has to have a type. And if you're going to put different things in the same array (SalesAccountSupplies, SalesAccountsPaper, and SalesAccountsServices) then the type of the array has to be a common supertype of those things. Examples:

    Java Code:
    int[] foo = new int[42];  // holds ints
    Customer[] customers = new Customer[1000]; // holds Customers
    Object[] baz = new Object[10]; // holds anything, but this is bad because it bypasses Java's type safety
    Get in the habit of using standard Java naming conventions!

  7. #7
    abeidson is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Error when adding an array into main method

    Thanks for the direction. I was following examples in my text book for my class and that is what caused my confusion.
    I modified the code to look like below and it is working now.

    Java Code:
    public class SalesAccountTest {
        
         
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
           
         // Initiate an array for data entry
            
            String[] customer = new String[4];
            
            // customer records
            SalesAccountSupplies sp = new SalesAccountSupplies( 12345, "Some Customer Here", 123.56, 5648.65, 9584.56);        
            SalesAccountsPaper pp =  new SalesAccountsPaper( 65891, "Best Paper Customer Ever", 900, 6.98);
            SalesAccountsServices ss = new SalesAccountsServices( 78415, "Services Customer", 123.99, 200);
            SalesAccountsServices ss2 = new SalesAccountsServices( 95863, "Andy Eidson", 165.32, 500);
    
            customer[ 0 ] = sp.toString();
            customer[ 1 ] = pp.toString();
            customer[ 2 ] = ss.toString();
            customer[ 3 ] = ss2.toString();
            
            for ( String currentCust : customer) {
                //output the proper customer, sales and total
                
                System.out.printf("%s \n", currentCust.toString());
            }
          }
         }

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Error when adding an array into main method

    You're calling toString() on the String elements of the array, which is redundant. Read the API for String#toString() to understand why.

    I think a better approach would be to have the array be of type SalesAccountSupplies. A simple println would take care of printing the value returned by the class's toString() method.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Error: Main method not found! Please help!
    By xemkruz2012 in forum New To Java
    Replies: 2
    Last Post: 03-11-2013, 11:19 PM
  2. Replies: 8
    Last Post: 11-22-2012, 07:16 AM
  3. Replies: 5
    Last Post: 09-17-2012, 06:47 AM
  4. main method not found error in class . wat s the error?
    By kirankumaragnihotram in forum New To Java
    Replies: 10
    Last Post: 06-29-2012, 11:26 AM
  5. Int Array Allowed in main() Method?
    By Starclopsofish in forum New To Java
    Replies: 19
    Last Post: 10-03-2008, 04:22 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
  •