Here's what I have so far. The compiler keeps spitting out errors
regardless of what I do to fix it, so what I'm posting is my most
recent attempt. I'm still not completely comfortable with the whole
objects and classes thing, so that'll probably show up quite nicely in
the code I'm posting here.
Obviously, I don't want anyone to do this assignment for me, and I
don't want any of you to assume that. More likely, I just need to be
kicked and yelled at a few times to force me to do things properly.
________________________________________________________
public class SearchStudents
{
public static void main(String[] args)
{
String name1;
double ID;
int status;
FillArray();
}
static void FillArray()
{
StudentInfo[] siarray = new StudentInfo[20];
for (int i = 0; i < 20; i++)
{
name1 = getName1(i);
ID = 100000 * Math.random();
status = 2 * Math.random();
siarray = new StudentInfo(name, ID, status);
}
}
static String getName1(int j)
{
String[] name = { //list of 20 names here };
return name[j];
}
}
public class StudentInfo
{
private String name;
private int ID;
private int status;
public StudentInfo(arrName, arrID, arrStatus)
{
name = arrName;
ID = arrID;
status = arrStatus
}
}
"Paul Fedorenko" wrote...
> Here's what I have so far. The compiler keeps spitting out errors
> regardless of what I do to fix it, so what I'm posting is my most
> recent attempt.
Well, looking at the code, those "fixes" seem most likely to be only
"guesses", and lack of some understanding of fundamental things like
"variable scope", "method signatures", etc.
I'll try to give you some hints:
> public class SearchStudents
> {
> public static void main(String[] args)
> {
What are you attempting to use these variables for?
And from where can you reach them?
> String name1;
> double ID;
[quoted text clipped - 5 lines]
> static void FillArray()
> {
Maybe the inner workings of "FillArray" will meet the requirements of your
assignment (?), but possibly the assignment will need to be expanded
somewhat in the future, so you can reach "siarray" from somewhere else as
well?
> StudentInfo[] siarray = new StudentInfo[20];
>
> for (int i = 0; i < 20; i++)
> {
Where did you define variable "name1"?
How can you reach it from here?
> name1 = getName1(i);
Where did you define variable "ID"?
How can you reach it from here?
> ID = 100000 * Math.random();
Where did you define variable "status"?
How can you reach it from here?
Can the result of the calculation be implicitly cast to that type?
> status = 2 * Math.random();
To what element of the array do you want to assign the StudentInfo?
Where did the variable "name" come from?
> siarray = new StudentInfo(name, ID, status);
> }
> }
>
> static String getName1(int j)
> {
As the array of names will be the same each time, maybe you'd look into the
chapter on "constants"?
> String[] name = { //list of 20 names here };
> return name[j];
[quoted text clipped - 3 lines]
> public class StudentInfo
> {
You will possibly need to add some methods to get these values as well...
> private String name;
> private int ID;
> private int status;
Maybe you should look into the chapter on "method signatures"?
> public StudentInfo(arrName, arrID, arrStatus)
> {
[quoted text clipped - 3 lines]
> }
> }
There are a lot more to be said about this, but I hope these hints can help
you come a bit further on your own.
// Bjorn A
Paul Fedorenko - 17 Mar 2005 05:45 GMT
> Well, looking at the code, those "fixes" seem most likely to be only
> "guesses", and lack of some understanding of fundamental things like
> "variable scope", "method signatures", etc.
Those are definitely among my problems, yes. Part of me wishes that I could
just define the variables straight-up at the beginning of the function that
uses them. It'd make life a whole lot easier, not having to worry about
things like private, static, public and protected. I think that's what's
throwing me the most. Well... That, and trying to get the array put
together in the first place.
> I'll try to give you some hints:
>
[quoted text clipped - 9 lines]
>> double ID;
>> int status;
They're supposed to be used by the functions in the class where they're
declared, and are meant to be passed into the array once they contain the
appropriate information. I've also tried declaring them in the FillArray()
function, where they're actually being used, but that only caused me more
problems. I figured they were better off where they are now, since I get
less errors that way...
No?
> There are a lot more to be said about this, but I hope these hints can
> help you come a bit further on your own.
I think I may have gotten the problems straightened out, except for the
following:
C:\Documents and Settings\paul\My
Documents\Programming\Java\Lesson7\SearchStudents.java:16: ';' expected
siarray = new StudentInfo[i](name1, ID, status);
I've got a semi-colon at the end of the line, and all the lines surrounding
the one in question end with a semi-colon too. As far as I can tell, the
syntax seems right otherwise.
Bjorn Abelli - 17 Mar 2005 07:47 GMT
"Paul Fedorenko" wrote...
> "Bjorn Abelli" wrote...
>>> public class SearchStudents
>>> {
[quoted text clipped - 16 lines]
>
> No?
No!
This is a question of "variable scope".
A variable only "exist" within a specific context.
As they were defined only in the method "main", they couldn't be reached
from within the method "FillArray".
The simple rule is to define variables where they are being used, in this
case *inside* FillArray, as that's the *only* place they're being used. If
they had been used in other methods as well, then you could have declared
them on a more "global" level, e.g. as "static" variables.
> C:\Documents and Settings\paul\My
> Documents\Programming\Java\Lesson7\SearchStudents.java:16: ';' expected
[quoted text clipped - 3 lines]
> surrounding the one in question end with a semi-colon too. As far as I
> can tell, the syntax seems right otherwise.
As I recall, there *was* a semicolon missing in the constructor of
StudentInfo...
// Bjorn A
Lāʻie Techie - 21 Mar 2005 07:29 GMT
> I think I may have gotten the problems straightened out, except for the
> following:
[quoted text clipped - 6 lines]
> surrounding the one in question end with a semi-colon too. As far as I
> can tell, the syntax seems right otherwise.
Since siarray is an array of StudentInfo references, you can only assign
an array to it. If you want to assign individual elements, you must tell
the index.
StudentInfo []siarray = new StudentInfo[20]; // creates an array
You reference each element of the array using square brackets:
siarray[0] = new StudentInfo(); // assigns a reference to element 0
StudentInfo si = siarray[1]; // retrieves the second element (index 1)
HTH,
La'ie Techie