INITIALIZING A VARIABLE

INITIALIZING A VARIABLE

Page 1 of 1

6 Replies - 1020 Views - Last Post: 12 April 2007 - 09:30 PM Rate Topic: -----

#1 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

INITIALIZING A VARIABLE

Posted 12 April 2007 - 08:00 PM

Initializing a variable.

How would I initialize these two variables?

int age[];

int weight[];

according to my error code below?

C:\cs151\People.java:87: variable age might not have been initialized
for(int i = 0; age[i]!= -2; i++)
^
C:\cs151\People.java:109: variable weight might not have been initialized
for(int i = 0; weight[i]!= -1;i++){
^
2 errors

Tool completed with exit code 1

Is This A Good Question/Topic? 0
  • +

Replies To: INITIALIZING A VARIABLE

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: INITIALIZING A VARIABLE

Posted 12 April 2007 - 08:14 PM

Check out the following link for info on initializing arrays.

http://java.about.co.../l/aa_array.htm
http://learning.nort...v1_arrays_t.htm
Was This Post Helpful? 0
  • +
  • -

#3 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: INITIALIZING A VARIABLE

Posted 12 April 2007 - 08:18 PM

Okay well it doesn't work when I put in

int age[i];

int weight[i];

nor does it work if i do this:

int age[] = 0;
int weight[] = 0;

or this:

int age[1];

int age[1];

what am I doing wrong? did you see the error code?


View Postjayman9, on 12 Apr, 2007 - 08:14 PM, said:

Check out the following link for info on initializing arrays.

http://java.about.co.../l/aa_array.htm

Was This Post Helpful? 0
  • +
  • -

#4 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: INITIALIZING A VARIABLE

Posted 12 April 2007 - 08:27 PM

I also tried this:

int[] age;

age = new int[i]!;

int[] weight;
weight = new int[i]!;


and I get an error of this:

C:\cs151\People.java:40: ';' expected
age = new int[i]!;
^
C:\cs151\People.java:44: ';' expected
weight = new int[i]! = -2;
^
2 errors

Tool completed with exit code 1






View Postxosunkist, on 12 Apr, 2007 - 08:18 PM, said:

Okay well it doesn't work when I put in

int age[i];

int weight[i];

nor does it work if i do this:

int age[] = 0;
int weight[] = 0;

or this:

int age[1];

int age[1];

what am I doing wrong? did you see the error code?


View Postjayman9, on 12 Apr, 2007 - 08:14 PM, said:

Check out the following link for info on initializing arrays.

http://java.about.co.../l/aa_array.htm

Was This Post Helpful? 0
  • +
  • -

#5 xosunkist  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 28-March 07

Re: INITIALIZING A VARIABLE

Posted 12 April 2007 - 08:39 PM

help anyone?

i read through the examples from the websites you posted. i don't think they helped me out too much. i don't understand.

View Postxosunkist, on 12 Apr, 2007 - 08:00 PM, said:

Initializing a variable.

How would I initialize these two variables?

int age[];

int weight[];

according to my error code below?

C:\cs151\People.java:87: variable age might not have been initialized
for(int i = 0; age[i]!= -2; i++)
^
C:\cs151\People.java:109: variable weight might not have been initialized
for(int i = 0; weight[i]!= -1;i++){
^
2 errors

Tool completed with exit code 1

Was This Post Helpful? 0
  • +
  • -

#6 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: INITIALIZING A VARIABLE

Posted 12 April 2007 - 09:22 PM

int age[] = new int[numberOfAges];

And array must have a defined length to begin with. Using this to initialize the array and then filling that array with your for loop should sork
Was This Post Helpful? 0
  • +
  • -

#7 vasdueva  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 03-April 07

Re: INITIALIZING A VARIABLE

Posted 12 April 2007 - 09:30 PM

int age[] = new int[numberOfAges];
for(int i = 0; i<numberOfAges; i++)
{
age[i] = age;
}

Something along those lines will initialize the array defined to be the length of 'numberOfAges' Then it will loop through and fill the array. As to whats in the for loop to fill the array... up to you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1