Results 1 to 7 of 7
  1. #1
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

    Question Making a class to use in a project

    I am working on some assignments for my college class and I have years of programming but only a month of Java and Netbeans. One thing I had to do in my first assignment and now going to do in this one too is pull in a 2-dimentional array and expand the first dimension by 1. Here is what I have: the .java file I want to pull in, named Dimension2.java with the function addRow(string array[][]), and Task2A.java, which is the program that is to run. In Task2A the import of Arrays.Dimension2; shows no errors, but when I try to call addRow(ReadData); I get an error that it cannot find symbol. Baisily even though I import Dimension2, it does not appear Dimension2 is working

    Netbean hierarchy:
    Making a class to use in a project-screen-shot-2012-10-25-4.12.58-am.png

    The Dimension2.java code is
    Java Code:
    public class Dimension2 {
         /**
         *
         * @param oldArray Original array for adding a row to.
         * @return
         */
        public static String[][] addRow(String[][] oldArray) {
    
    	int oldRowCount = oldArray.length;
    	int ColCount = oldArray[0].length;
    	String[][] newArray = new String[oldRowCount + 1][ColCount]; //make new array one length larger than first
    	//(old array, start position, new array, start position, new length
    	System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
    
    	return newArray; //returning to original array with added dimension should keep
        }
    }
    Task2A import line:
    Java Code:
    import Arrays.Dimension2;
    Funtion that calls addRow:
    Java Code:
    private static String[][] readfile(File inFile) {  //Read listings file and save to a set
    	String[][] ReadData = new String[0][4];
    	ReadData = addRow(ReadData);
    	return ReadData;
        }
    How do I get addRow to work besides just copying the function back into Task2A. That will work for now but down the road when I want to do larger imports that will be more difficult.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Making a class to use in a project

    I'm having trouble keeping track of exactly what the error is, and on what line it occurs. I recommend posting an SSCCE that demonstrates the exact problem in as few lines as possible (for your problem, accessing an empty function from another class is enough).
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

    Default Re: Making a class to use in a project

    Ok, here is the full SSCCE code for the two java files:
    Java Code:
    package task2a;
    
    import task2a.Dimension2;
    
    public class Task2A {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    	String[][] ReadData = new String[0][4];
    	ReadData = addRow(ReadData);//readys array for new data
    
        }
    }
    Java Code:
    package task2a;
    
    public class Dimension2 {
    
        public static String[][] addRowXY(String[][] oldArray) {
    
    	return oldArray; //returning to original array with added dimension should keep
        }
    }
    and they are layed out in netbeans as so:
    Making a class to use in a project-screen-shot-2012-10-25-5.05.15-pm.png

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Making a class to use in a project

    ... newArray = Dimension2.addRow(oldArray) (Java will not look to other classes to resolve addRow.... you need to specify where addRow is, import != include. You don't need the import here, anyway.)

  5. #5
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

    Default Re: Making a class to use in a project

    That works. Now my origional problem was that Dimension2 was in a different project (see image in first post). In that case I do need to import correct? when I remove the import line I get an error

  6. #6
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Making a class to use in a project

    In that case, yes, you'll need to import. You do not need an import for classes in the same package. (but that won't hurt, either)

  7. #7
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

    Default Re: Making a class to use in a project

    Ok, thanks for the help

Similar Threads

  1. Problems with making a class. New to Java.
    By zendavis in forum New To Java
    Replies: 9
    Last Post: 07-24-2011, 11:39 AM
  2. help making class print in jpanel
    By Grid_iso in forum New To Java
    Replies: 1
    Last Post: 05-19-2011, 02:39 PM
  3. making a working project from source and .jar libs
    By stringargs in forum NetBeans
    Replies: 0
    Last Post: 05-08-2011, 12:51 PM
  4. Making Data available to every class in a project.
    By JOHNINALBANY in forum New To Java
    Replies: 5
    Last Post: 12-15-2010, 09:09 AM
  5. Making a netbeans project into a executable jar file
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 12-03-2010, 05:09 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
  •