• Post Reply Bookmark Topic Watch Topic
  • New Topic

logic error  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for some reason im having trouble seeing what the problem here is. i have a class named Distance that has an array initialized to all -1's. The class has been tested and works correctly. now i have a class that extends JFrame that has a Distance object as a member variable. for some reason when i call its findDistance() method, im not getting the correct value returned. I added System.out.println()'s but still dont see what the problem is. here is some of the code and the output of the println()'s.

//value printed inside actionPerformed()
60
//parameters printed inside addDistance()
Portland
Salem
60
//parameters printed inside findDistance()
Portland
Salem
//value printed inside actionPerformed()
-1
maybe i should mention that the Distance class also has an array of String. the methods compare the parameters with the values in this array and uses indexOf() to index the 2 dimensional array of int. in my JFrame's constructor i have this code that initializes that array.
distance.addCity("Portland");
distance.addCity("Salem");
distance.addCity("Eugene");
distance.addCity("Roseburg");
distance.addCity("Medford");
[ January 24, 2004: Message edited by: Randall Twede ]
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the class i used to test the Distance class. it performed correctly
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
found the problem finally

should have been

forgot i needed to use equals() instead of ==
kind of threw me off that it worked when i tested it with the TestDistance class
PS: had to change the start value of the loop to avoid a NullPointerEception
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!