473,416 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

What is wrong with my java codes?

108 100+
I just happened to read abt vectors in java..For some practice,I implemented this small script where I Store objects in a vector..Then,I want to invoke the methods of the corresponding objects.My problem is that in this line

System.out.println(vc.elementAt(i).test3());

the test3() method of my object is being underlined..I know I am retrieving an abject from vc.elementAt(i),then why is the method test3() being underlined??


Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class MITlab2 {
  3.  
  4.     /**
  5.      * @param args
  6.      */
  7.     public static void main(String[] args) {
  8.         // TODO Auto-generated method stub
  9.          test_vector1();
  10.     }
  11.  
  12.  
  13.     public static void test_vector1(){
  14.         test_class vm=new test_class();
  15.         Vector vc=new Vector();
  16.         vc.add(vm);vc.add(vm);vc.add(vm);
  17.         System.out.println(vc.size());
  18.         for (int i=0;i<vc.size();i++){
  19.             System.out.println(vc.elementAt(i).test3());
  20.         }
  21.  
  22.     }
  23.  
  24. }
  25. class test_class{
  26.     public String vim="testing";
  27.     public boolean test3(){
  28.         return true;
  29.     }
  30. }
  31.  
May 28 '10 #1

✓ answered by Dheeraj Joshi

You code in line 19 should be changed.

Expand|Select|Wrap|Line Numbers
  1. ((test_class) vc.elementAt(i)).test3()
  2.  
This is because test3 function is the member of test_class and you are using it in MITLab2 class.

Regards
Dheeraj Joshi

4 1668
Dheeraj Joshi
1,123 Expert 1GB
You code in line 19 should be changed.

Expand|Select|Wrap|Line Numbers
  1. ((test_class) vc.elementAt(i)).test3()
  2.  
This is because test3 function is the member of test_class and you are using it in MITLab2 class.

Regards
Dheeraj Joshi
May 28 '10 #2
There The return type is Object so u need to do implicit conversion buddy
May 28 '10 #3
phpuser123
108 100+
Thanks dheerajjoshim and konapalligopi,The code worked...
But I hava a slight problem understanding this...

I used vm.test3() and it worked fine but then why didn't it work on vc.elementAt(i).test3()??

Why does it work with vm.test3() when vm also is an object....Why i don't have to do this conversion??A bit confused here...
May 31 '10 #4
jkmyoung
2,057 Expert 2GB
You've explicitly declared vm as a test_class object.
vc.elementAt(i) is just an object, needs to be explicitly cast.

You could always set your vector to only hold test_class objects (assuming you're using java 1.5 or higher.

eg Vector <test_class> vc=new Vector<test_class>();
May 31 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Niklas | last post by:
H What is Java Webstart counterpart in .NET Regard /Niklas
3
by: Skyhorse | last post by:
What are the following codes doing? What are the meaning of & 0xffe00000, & 3, 0xf, 0x3? Is there any difference between & 3 and & 0x3? inline BOOL CheckAudioHeader(DWORD dwHeader) { if (...
1
by: cheng | last post by:
can anyone give me java codes for dropdown menus for website.....
1
by: JagPeram | last post by:
I need to migrate data from a ms excel sheet to db. what has to be done? this entirely new application for me. help me with this.
1
by: Fahad Faisal | last post by:
what is java
1
by: fAnSKyer/C# newbie | last post by:
private void startupServer() { try { serverscoket = new ServerSocket(Listener.PORTNUMBER); SERVER_ON = true; } catch (IOException e1) { e1.printStackTrace(); } } public void run() {
3
by: Rajesh V | last post by:
Why Java codes take more run time than C/C++ ?
20
by: mc | last post by:
I may be opening a can of worms and don't want to start a religious war, but... What features of Java do Java programmers miss when working in C#? Other than, of course, great portability. C#...
5
xploreraj
by: xploreraj | last post by:
Hi there, would you please explain me in detail on how to use the command prompt for writing, compiling and running java programs (JSE). Thanks.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.