Results 1 to 20 of 28
- 10-31-2008, 03:02 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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(); } }
- 10-31-2008, 03:11 PM #2
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
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.
- 10-31-2008, 03:18 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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.
- 10-31-2008, 03:22 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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.
- 10-31-2008, 03:29 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
can you give us a quick example???? cheers
- 10-31-2008, 03:38 PM #6
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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/getterLast edited by ianjedi; 10-31-2008 at 03:51 PM. Reason: made a boob :)
- 10-31-2008, 03:48 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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);
- 10-31-2008, 03:55 PM #8
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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
- 10-31-2008, 04:03 PM #9
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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-31-2008, 04:09 PM #10
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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.
- 10-31-2008, 04:15 PM #11
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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];
}
- 10-31-2008, 04:20 PM #12
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
sorry the void should not be there I made a mistake which I edit.
- 10-31-2008, 04:24 PM #13
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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
- 10-31-2008, 04:29 PM #14
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
can you post your code thanks.
- 10-31-2008, 04:35 PM #15
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
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]; }
- 10-31-2008, 04:38 PM #16
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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.
- 10-31-2008, 04:42 PM #17
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
hi ian,
it gives a different error,
cannot find symbol
symbol : variable marks
cheers
- 10-31-2008, 04:53 PM #18
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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
- 10-31-2008, 05:05 PM #19
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
You should use
instead ofJava Code:marks.marks[0] = blabla
And this:Java Code:marks[0] = blabla
should beJava Code:class StudentMarks { public static void main(String[] arg) { } // Create the array public StudentMarks(int i){ double [] marks = new double [i]; }
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.
- 10-31-2008, 05:12 PM #20
Member
- Join Date
- Oct 2008
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
writing an array class
By wardd85 in forum New To JavaReplies: 5Last Post: 07-16-2008, 10:59 PM -
Writing a countdown array to a file.
By kewlgeye in forum New To JavaReplies: 6Last Post: 05-25-2008, 06:09 AM -
replacing array values
By Jononomous in forum New To JavaReplies: 1Last Post: 05-22-2008, 03:27 PM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks