 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
janicethorne via JavaKB.c Guest
|
Posted: Thu Apr 20, 2006 4:12 pm Post subject: Methods and arrays |
|
|
I have an array that is fed numbers (each on separate lines) by an infile
(File.txt). In a separate class, I needed to create a method to print each
number on the same line and be separated by a space. Is this code the correct
way to reference each separate line of data and print them all together?
double[] data = new double[5];
double ave;
//print all numbers on a line with spaces between them
public static printIt(data) {
for (int index = 0; index < data.length; index++)
outFile.print(data[index]+ " "); }
--
"The man who does not read good books has no advantage over the man who
cannot read them."
Message posted via http://www.javakb.com |
|
| Back to top |
|
 |
Bjorn Abelli Guest
|
Posted: Thu Apr 20, 2006 5:12 pm Post subject: Re: Methods and arrays |
|
|
"janicethorne via JavaKB.com" wrote...
| Quote: | I have an array that is fed numbers (each on separate lines)
by an infile (File.txt). In a separate class, I needed to
create a method to print each number on the same line and be
separated by a space. Is this code the correct
way to reference each separate line of data and
print them all together?
|
Almost...
The method signature needs a return type (.e.g.'void'),
and the argument 'data' also needs a type, (e.g. 'double[]').
| Quote: | public static printIt(data) {
for (int index = 0; index < data.length; index++)
|
'outFile' must be declared as a 'static' variable in the class, otherwise
the method can't reach it.
| Quote: | outFile.print(data[index]+ " "); }
|
Apart from those little things, it should work...
// Bjorn A
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|