Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

Arrays

Thread view: 
spudgun - 09 Jun 2005 19:55 GMT
I'm a real newbie to java but I'm stuck on arrays and would really
appreciate some help.

I've created two java classes test and collect within my results.jpx file.
The code for test is:

public class test {

 public String field1;
 public String int2;

 public test(String field1, int field2) {
   field1= "none";
   field2= "0";
  }

The code for collect is:

public class collect{
 public int capacity= 5;
 String[] arraycollect;

 public collect() {

   String[] arraycollect= new String[capacity];
 }

public void populatecollect() {
   int count = 0;
   while (count <arraycollect.length){
     if (count%2 == 0) {
         arrayCollect[count] = new test(("1234"+count),1+count);
     }
     else {
         vehicleCarPark[count] = new test(("123456"+count),1,);
     }
     count++;
   }

But all I'm getting when I compile is error#.354 incompatible types; found:
results.test, required: java.lang.string.

Can someone help because I can't see what I've done wrong and its driving
me nuts
Eric Sosman - 09 Jun 2005 20:22 GMT
> [...]
>     String[] arraycollect= new String[capacity];

   All right: arraycollect refers to an array that can
hold references to String objects.

> [...]
>           arrayCollect[count] = new test(("1234"+count),1+count);

   Woops!  There's no such thing as arrayCollect in your
program, so this won't compile.  But since you didn't get
the error message this error would have produced, I suspect
that what you've shown is not actually the code that's
giving you the trouble.  Can you understand why paraphrasing
your code is a terrible idea?  "Doctor, I've got this terrible
toothache."  "Where?"  "In my left elbow."  Makes for sort
of an unreliable diagnosis, don't you think?  So I'm just
going to assume that the upper-case C was actually lower-case,
and that you will learn NEVER to do this again.  Okay?  Now,
back to our originally scheduled program.

  Woops!  The thing you're trying to insert in the array
is not a reference to a String, it's a reference to a test.
The Java compiler complains because you're trying to put a
square peg into a round hole.  Either change arraycollect
to be an array of test references

    test[] arraycollect = new test[capacity];

or else derive some kind of string from the test object and
put a reference to that object in the array:

    test t = new test(("1234"+count), 1+count);
    arraycollect[count] = t.toString();
    // or perhaps
    arraycollect[count] = t.field1;

   Decide what you want: an array of Strings, or an array
of tests.

   (And learn how to use cut-and-paste when posting code
that perplexes you.  DON'T paraphrase, because you'll almost
certainly introduce brand-new errors that the kind folks on
Usenet will happily solve for you -- but, of course, your
real problem will not have been cured.)

Signature

Eric.Sosman@sun.com

spudgun - 09 Jun 2005 21:12 GMT
>> [...]
>>     String[] arraycollect= new String[capacity];
[quoted text clipped - 41 lines]
>Usenet will happily solve for you -- but, of course, your
>real problem will not have been cured.)

Eric, thanks for that. I'll never make the mistake of paraphrasing the code
don't worry!!!Those error messages have gone now but something else is goin
wrong :-(

I've created a new java file called testOutput with the following code:

public class testOutput{

public static void main(String args[]) {

 collect Test = new collect();

 Test.populatecollect();

 }
}

I'm getting an error message saying there is a
java.lang.nullPointerException relating to the while line in the
populatecollect method. I'm guessing that it's because the array isn't
intialising correctly, but I thought what I'd written to populate the array
is correct.

Cheers for your help
Eric Sosman - 09 Jun 2005 22:08 GMT
> Eric, thanks for that. I'll never make the mistake of paraphrasing the code
> don't worry!!!Those error messages have gone now but something else is goin
[quoted text clipped - 18 lines]
> intialising correctly, but I thought what I'd written to populate the array
> is correct.

   Glad to see you've learned the "copy and paste" lesson.
It turns out that there are more lessons to learn, and one
of them is the "don't withhold evidence" piece.  You say
you're having trouble in the populatecollect method -- well,
then, how about showing us the method?  No, wait: show us
the entire program, because even though the symptom shows up
in populatecollect, the cause may lie elsewhere.  (In fact,
I've got a sneaking suspicion about where "elsewhere" is, but
since it's based on the paraphrase and not on the real code,
I could well be wrong.)

   Now, don't post a 20,000-line program in its entirety;
nobody will read it and you'll get no help.  Here's what to
do if you're starting with more than about fifty lines:

   - Whittle away the irrelevant pieces, things that are
     important to your program but not to the problem.  How
     can you tell which bits are irrelevant?  Run the reduced
     program and see if the problem still occurs.  (It often
     happens that this step will reveal the problem and you'll
     have the pleasure of discovering the fix for yourself.)

   - When you've whittled it down as far as you can, make
     sure you're not relying on any other classes and packages
     not found in the standard Java run-time environment --
     if your program requires additional stuff that we out
     here in Usenet-land don't have available, we can't run
     your program and find the problem (which might reside in
     those other packages, you know).

   - Copy and paste the code into your message, verbatim.  If
     lines wrap unplesantly, either leave them messy or go
     back and change the original code and re-run it, and then
     copy and paste the revised code; do not hand-edit what
     you pasted.

   Don't feel bad: we were all newbies once.

Signature

Eric.Sosman@sun.com

spudgun - 09 Jun 2005 22:51 GMT
OK, deep breath here we go:

public class collect{
 public int Capacity = 5;
 test[] arraycollect;

 public collect() {

   test[] arraycollect= new test[Capacity];
   vehiclesInCarPark = 0;
 }

 public void populatecollect() {
   int count = 0;
   while (count <arraycollect.length){
     if (count%2 == 0) {
         arrayCollect[count] = new test(("1234"+count),1+count);
     }
     else {
         arraycollect[count] = new test(("123456"+count),1,);

     }
     count++;
   }

}

public class test{

   public String registrationNumber;
   public int Limit;

   public test() {
       // Initial Settings for registration and limit
       registrationNumber = "A123 BCD";
       Limit= 1;
   }

   public void setRegistrationNumber(String reg) {
       registrationNumber = reg;
   }

   public void setLimit(int occ) {
       Limit= occ;
   }

   public String getRegistrationNumber() {
       return registrationNumber;
   }

   public int getLimit() {
       return Limit;
   }

   public String toString() {
       return "Registration number " + getRegistrationNumber() + " with
limit " + getLimit();
   }
}

public class testOutput {

   public static void main(String[] args) {

 test car = new test();

car.setRegistrationNumber("Test 123");
 car.setLimit(4);
 car.getLimit();
 car.getRegistrationNumber();
 System.out.println(car.toString());

collect test2 = new collect();

Test.populatecollect();

 }
}

If you can solve this for me, it'll be a geat help
spudgun - 09 Jun 2005 22:59 GMT
sorry that last bit should have been

test2.populatecollect

Am working on two different versions at the moment and its just getting me
confused

>OK, deep breath here we go:
>
[quoted text clipped - 76 lines]
>
>If you can solve this for me, it'll be a geat help
Eric Sosman - 09 Jun 2005 23:25 GMT
> OK, deep breath here we go:

   spudgun, spudgun ... What ever are we going to do with
you?  When I tried to compile this code, Java told me

collect.java:18: illegal start of expression
         arraycollect[count] = new test(("123456"+count),1,);
                                                           ^
collect.java:18: ')' expected
         arraycollect[count] = new test(("123456"+count),1,);
                                                            ^
collect.java:25: '}' expected
^
3 errors

   Now, are you going to sit there with a straight face and
claim that you actually compiled and ran the code before
posting it?  Tut-tut, spudgun, tut-tut.

   But what the heck: I'm encouraging your bad behavior,
but it's been a long day and I'm too tired to shoulder the
whole burden of your education.  So I'll point out ONE error
in your code and tell you how to fix it -- but I'll warn you
up front that I have not examined the rest of it (because
it's clearly not "the" code) and will not help you with it
any further.  If a Java Master you wish to become, how to
ask effective questions you must learn.  Grump, grump.

> public class collect{
>   public int Capacity = 5;
[quoted text clipped - 3 lines]
>
>     test[] arraycollect= new test[Capacity];

   Here's the (or "a") problem: You've already got a class
member called `arraycollect', which you declared about five
lines ago.  Now you declare another `arraycollect' completely
independent of the class member: this one is just a local
variable in the constructor.  You declare this local variable,
create a new test[] array, put a reference to the array in
the local variable, and ride off into the sunset with the
happy feeling that comes from a job well done.

   ... except that you've done nothing at all to the class
member `arraycollect'; it's still a `null' reference.  The
newly-created array will become collectible garbage as soon
as the constructor returns and its local variables disappear.
So later on when you try to use the class member `arraycollect'
Java will be unhappy with you because that `arraycollect'
doesn't refer to anything.

   The fix: Get rid of the `test[]' at the beginning of the
line.  Then instead of declaring a local `arraycollect' you'll
be referring to the class member, as you probably intended.

Signature

Eric.Sosman@sun.com

blmblm@myrealbox.com - 11 Jun 2005 01:45 GMT
[ snip ]

>  If a Java Master you wish to become, how to
>ask effective questions you must learn.  Grump, grump.

Also:

If Java Master you wish to become, naming conventions followed by
other Java Masters you may wish to adopt.

Namely, a mixed-case name starting with a capital letter (e.g.,
Vector or JButton) is a class, and a mixed-case name starting with
a lower-case letter (e.g., length or size()) is a variable or method.

You seem to have adopted more or less the opposite convention
(variable named Capacity, class named test).  I doubt I'm the only
person who finds that it makes your code confusing to follow.

>> public class collect{
>>   public int Capacity = 5;
[quoted text clipped - 3 lines]
>>
>>     test[] arraycollect= new test[Capacity];

[ snip ]

| B. L. Massingill
| ObDisclaimer:  I don't speak for my employers; they return the favor.
"." - 10 Jun 2005 17:41 GMT
> I'm a real newbie to java but I'm stuck on arrays and would really
> appreciate some help.
>
> I've created two java classes test and collect within my results.jpx file.
> The code for test is:

    [Unimportant code snipped]

> But all I'm getting when I compile is error#.354 incompatible types; found:
> results.test, required: java.lang.string.

The compiler found the data type results.test but it was expecting a
String. Java is a strongly typed language. That means if you saying
Apple = Orange you will get an error like this. For example, if I have
the class:

class NotAString {
    String s;

    void setValue(String s) { this.s = s; }
    String getValue() { return s; }
}

then I attempt to do:

    String test = new NotAString();

I will get an error saying, found: NotAString, required: java.lang.String.

Have a look at the code and see if you are doing something like this, i.e.
assigning one data type to another data type. Another similar mistake
would be do do soemthing like:

    Integer n = new Integer(5);
    NotAString test;

    test.setValue(n);

The test.setValue() method expects a java.lang.String as input but I have
given it an Integer. The compiler will complain about this as well.

Signature

Send e-mail to: darrell dot grainger at utoronto dot ca



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.