Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default writing array values to another class

    hi all,

    basic query here, i have a simple array that collects 6 values from the prompt and stores them in an array, this works fine (see code 1 below) when i create the array in the same class, what i need to do is initialize the array in another class and send the values back to that class from this class. i have tried declaring the array in another classed called MyMarks and then in the class below writing MyMarks marks= new MyMarks(); and removing the array declaration, but I keep getting symbol not found errors, any pointers on the correct syntax?? I need to use this other class beacuse I have to put more methods in there that work on the data sent..

    Java Code:
    -----code 1 ----------
    
    class MyMarksMain {
    	
        public static void main(String[] args){
        	
        	double [] mymarks = new double [5];	
    
    		SimpleInput keyboard = new SimpleInput();
    	 	
    	             System.out.println("enter result 1 =  ");
    		mymarks[0] = keyboard.nextDouble();
    		System.out.println("enter result 2 =  ");
    		mymarks[1] = keyboard.nextDouble();
    		System.out.println("enter result 3 =  ");
    		mymarks[2] = keyboard.nextDouble();
    		System.out.println("enter result 4 =  ");
    		mymarks[3] = keyboard.nextDouble();
    		System.out.println("enter result 5 =  ");
    		mymarks[4] = keyboard.nextDouble();
       				}				
            }

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Are you saying you need to copy an array? Or copy the values from one array to another? Sorry, I am a little unclear on what your problem is.

  3. #3
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    hi paul,

    i need these values to get stored in the array that is in the other class MyMarks, the question was to basically put the array, methods etc.. in one class and just get the input and display the results in the other class, it is to practice using getters and setters rather than putting it all together. hope this is a bit clearer.

  4. #4
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    from what you wrote I think you need to do it in the construtor and create a new instance of the array and then use getter and setter method.

  5. #5
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    can you give us a quick example???? cheers

  6. #6
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    Class Myarray {



    public Myarray(int i){

    double [] mymarks = new double [i];

    }

    }

    From another class

    Myarray marks= new Myarray(5);

    and some method as a setter/getter
    Last edited by ianjedi; 10-31-2008 at 04:51 PM. Reason: made a boob :)

  7. #7
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    hi ianjedi,

    cheers for the quick replies,

    it gives me cannot find symbol error when i call the array from the other class using Myarray marks= new Myarray(5);

  8. #8
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    did you make a construtor in the class? As the new instance in other class can only be made by the constructor from the my array class. You know what is meant by constructor?

    cheers

    Ian G

  9. #9
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    hi ian,

    i changed the names slightly to match the filenames/requirements etc.. and put

    StudentMarks marks= new StudentMarks(5);

    i also recomplied the origonal class as well, the error says

    canot find symbol

    symbol : constructor StudentMarks(int)
    location : class StudentMarks


    so it looks like it is going back to the original array class StudentMarks which i changed to read

    public void StudentMarks(int i){

    double [] marks = new double [i];

  10. #10
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    mine was just an example of how to make a class with a construtor.

    StudentMarks marks= new StudentMarks(5);

    you would need a class StudentMarks, with a construtor StudentMarks(int i);

    then an array inside the constructor to be initialize to i or could be set at 5 if that is th size you need.

  11. #11
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    Hi ian,

    i think i have all that, see below, but it still gives the cannot find symbol error when i try to compile the other class ??? is it because i have not assigned the getter setter methods to handle the values??? i thought this might not be required at this point???

    i have a class called studentMarks and inside it i have

    class StudentMarks {
    public static void main(String[] arg) {

    }

    // Create the array
    public void StudentMarks(int i){

    double [] marks = new double [i];

    }

  12. #12
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    sorry the void should not be there I made a mistake which I edit.

  13. #13
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    cheers ian, that error is not coming any more! but it is giving an

    array required, but StudentMarks found

    marks[0] = keyboard.nextDouble();

    and putting the arrow under the opening square bracket after the s on marks

  14. #14
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    can you post your code thanks.

  15. #15
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    this is StudentMarksMain

    Java Code:
    class StudentMarksMain {
    	
        public static void main(String[] args) {
        	    		
            StudentMarks marks= new StudentMarks(7);
    		
    		SimpleInput keyboard = new SimpleInput();
    	 	
    	    System.out.println("enter result 1 =  ");
    		marks[0] = keyboard.nextDouble();
    		System.out.println("enter result 2 =  ");
    		marks[1] = keyboard.nextDouble();
    		System.out.println("enter result 3 =  ");
    		marks[2] = keyboard.nextDouble();
    		System.out.println("enter result 4 =  ");
    		marks[3] = keyboard.nextDouble();
    		System.out.println("enter result 5 =  ");
    		marks[4] = keyboard.nextDouble();
    		System.out.println("enter result 6 =  ");
    		marks[5] = keyboard.nextDouble();
    	    System.out.println("enter result 7 =  ");
    		marks[6] = keyboard.nextDouble();
    		
    		System.out.println("result 1 =" + marks[0]);
    		System.out.println("result 2 =" + marks[1]);
    		System.out.println("result 3 =" + marks[2]);
    		System.out.println("result 4 =" + marks[3]);
    		System.out.println("result 5 =" + marks[4]);
    		System.out.println("result 6 =" + marks[5]);
    		System.out.println("result 7 =" + marks[6]);
    		
    		
       				}

    this is StudentMarks

    Java Code:
    class StudentMarks {
    	public static void main(String[] arg) {
    		
    	}
    	
    	// Create the array 
    	public StudentMarks(int i){
    
    	double [] marks = new double [i];
    
    }

  16. #16
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    StudentMarks marks= new StudentMarks(7);

    try to change that to new StudentMarks(7);

    I don't have compliler on this computer, but I think it may work like that.

  17. #17
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    hi ian,

    it gives a different error,

    cannot find symbol
    symbol : variable marks

    cheers

  18. #18
    ianjedi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    I'll post later when I go home as this computer only has a web browser.

    someone else may have beat me to it in that time :)
    cheers

    Ian J

  19. #19
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    You should use
    Java Code:
    marks.marks[0] = blabla
    instead of
    Java Code:
    marks[0] = blabla
    And this:
    Java Code:
    class StudentMarks {
    	public static void main(String[] arg) {
    		
    	}
    	
    	// Create the array 
    	public StudentMarks(int i){
    
    	double [] marks = new double [i];
    
    }
    should be
    Java Code:
    class StudentMarks {
    
    	public double[] marks;
    	// Create the array 
    	public StudentMarks(int i){
    
    	marks = new double[i];
    }
    
    }
    I die a little on the inside...
    Every time I get shot.

  20. #20
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default

    hi supamagier,

    i get an illegal start of expression error when i compile

    class StudentMarks {


    // Create the array
    public StudentMarks(int i){

    public double [] marks = new double [i];
    }

    }

Page 1 of 2 12 LastLast

Similar Threads

  1. writing an array class
    By wardd85 in forum New To Java
    Replies: 5
    Last Post: 07-16-2008, 11:59 PM
  2. Writing a countdown array to a file.
    By kewlgeye in forum New To Java
    Replies: 6
    Last Post: 05-25-2008, 07:09 AM
  3. replacing array values
    By Jononomous in forum New To Java
    Replies: 1
    Last Post: 05-22-2008, 04:27 PM
  4. Replies: 4
    Last Post: 04-15-2008, 02:04 PM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 11:41 AM

Posting Permissions

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