Welcome to Dream.In.Code
Become a Java Expert!

Join 150,402 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 957 people online right now. Registration is fast and FREE... Join Now!




Vector question

 
Reply to this topicStart new topic

Vector question, How to use vectors with methods

NassauBob
18 Mar, 2008 - 11:12 AM
Post #1

New D.I.C Head
*

Joined: 13 Mar, 2008
Posts: 6

Hi all. This is the BEST help site for Java!

I am working on a homework problem, and can use a little nudge.

I don't need any specific coding help, just want to know how do I make a method with a vector as its formal parameter.

Like an array is (int[] arrayName), an integer is (int number), etc.

How do i do it for a vector? And would I put for actual parameter just the vector name ex. findNumber(vectorName); ?

Thanks for always being here!

Shane
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Vector Question
18 Mar, 2008 - 11:38 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
You can do returnType method(Vector vec) but that will give you a warning. You can instead do
returnType method(Vector<DataType> vec) which specifies what the Vector will contain. Using the <DataType> is sort of like an array of said type.
User is offlineProfile CardPM
+Quote Post

NassauBob
RE: Vector Question
18 Mar, 2008 - 12:08 PM
Post #3

New D.I.C Head
*

Joined: 13 Mar, 2008
Posts: 6

Awesome.

Okay, here is my code (sans the aforementioned part above):

I am working with a vector of string vectors, everything (surprisingly!) is working good with the vectors, but I want to search for a value in the inner vectors, a name string.

CODE


//Vector of vectors

import java.util.*;
import java.io.*;

public class VectorStack
{
    public static void main(String[] args)
    {
        
        String inName;
        Scanner console = new Scanner(System.in);
        
        Vector<String> emp1 = new Vector<String>(4);
        Vector<String> emp2 = new Vector<String>(4);
        Vector<String> emp3 = new Vector<String>(4);
        Vector<String> emp4 = new Vector<String>(4);
        Vector<String> emp5 = new Vector<String>(4);
        Vector<String> emp6 = new Vector<String>(4);
        Vector<String> emp7 = new Vector<String>(4);
        Vector<String> emp8 = new Vector<String>(4);
        Vector<Vector> empData = new Vector<Vector>(8);
            
        emp1.addElement("Shane Andrews");
        emp1.addElement("252-333-4567");
        emp1.addElement("252-333-4567");
        emp1.addElement("252-444-5678");
        
        emp2.addElement("Bob Cline");
        emp2.addElement("252-343-4567");
        emp2.addElement("252-343-4567");
        emp2.addElement("252-434-5678");
        
        emp3.addElement("Steve Carlton");
        emp3.addElement("252-333-4227");
        emp3.addElement("252-353-4447");
        emp3.addElement("252-447-5980");
        
        emp4.addElement("Jason Jameston");
        emp4.addElement("245-433-4567");
        emp4.addElement("245-433-4557");
        emp4.addElement("245-433-2348");    
        
        emp5.addElement("Justin Webb");
        emp5.addElement("152-333-4067");
        emp5.addElement("212-300-4167");
        emp5.addElement("252-414-5675");
        
        emp6.addElement("Rich Fisher");
        emp6.addElement("272-343-9561");
        emp6.addElement("272-343-4007");
        emp6.addElement("272-774-5678");
        
        emp7.addElement("Ronda Ervin");
        emp7.addElement("259-305-3127");
        emp7.addElement("259-353-4337");
        emp7.addElement("259-407-5080");
        
        emp8.addElement("Jimbo Reale");
        emp8.addElement("637-433-4567");
        emp8.addElement("637-020-4500");
        emp8.addElement("637-433-1148");
        
        empData.addElement(emp1);
        empData.addElement(emp2);
        empData.addElement(emp3);
        empData.addElement(emp4);
        empData.addElement(emp5);
        empData.addElement(emp6);
        empData.addElement(emp7);
        empData.addElement(emp8);
        
        System.out.print("Enter the name of the employee: ");
        inName = console.next();
        for(int x = 0; x < empData.size(); x++)
        {
            if(empData.elementAt(x).contains(inName)) //DOES NOT WORK WITH THIS VARIABLE...WHY NOT?
            {
                System.out.println("has");
                System.out.println(empData.elementAt(x));
            }    
            else
                System.out.println("has not");                    
        }
    }
}



Now, when I manually enter one of the names (e.g. Bob Cline) in the section

CODE


if(empData.elementAt(x).contains(inName))



the program runs fine. But if I leave it with the reference to the user entered value (inName) as above, the if statement never finds what I enter. The else statement always executes, which at least tells me that the code structure is good.

My original question was so that I can make this findName function be performed by a void method.

Thanks,

Shane
User is offlineProfile CardPM
+Quote Post

NassauBob
RE: Vector Question
19 Mar, 2008 - 12:20 PM
Post #4

New D.I.C Head
*

Joined: 13 Mar, 2008
Posts: 6

All,

Fixed my own problem (tada!) I was trying to input a string that had spaces, and the console.next() was taking the part before the space, but nothing after that.

So I just broke that field into two fields, last name and first name. After that, I just prompted for last name, and it works like a champ!

So...thanks to me!!!

I also was successful in using the method I wanted with the vector as a formal parameter - so thanks to GWatt for THAT.

Shane

This post has been edited by NassauBob: 19 Mar, 2008 - 12:22 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:15PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month