Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: New to vectors passing parameters

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question New to vectors passing parameters

    I am implementing a searching method for my first program using Vectors

    But it does not work not sure why??



    The program runs but the search is unsuccessful.

    Thank you for time and interest.
     
     
    import java.util.*;
    public class test {
     
     
    	public static void main(String[] args) {
    		Customer dan = new Customer("dan rogers","13 apple road",8787);
    		Customer carl = new Customer("carl","17 peach road",89);
    		Customer michael = new Customer("michael ford","345 apple road");
     
    		Vector<Customer> cu = new Vector<Customer>();
    		cu.add(dan);
    		cu.add(carl);
    		cu.add(michael);
     
     
    		Scanner sc = new Scanner(System.in);
    		String c = sc.nextLine();
     
     
     
    		searchVector(cu,c);
     
     
     
     
     
     
     
    	}//end main
     
    	// Search Method
     
    	public static void searchVector(Vector<Customer> v, String c ){
    		int i = v.indexOf(c);
    		if(i >=0){
    			System.out.println("Found "+ i);
    			System.out.println();
    			Customer temp  = v.get(i);
    			System.out.printf("%s\t %s\t %s\t",temp.getName(),temp.getTelephone(),temp.getAddress());
    			System.out.println();
    			}
    		else {
    			System.out.println( c +" Does not exist");
    		}
     
    	} Search Method
     
     
    }// end class


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: New to vectors passing parameters

    indexOf uses the equals method of an object...Customer and String are different objects, and if you do not define the equals method for Customer (no code to show either way) you will not find the object in question. What value of Customer is the String supposed to search anyway? A better method would be to loop through the vector, comparing the search string and the value you wish to search. An even better method would be to use a Map to key each customer based upon the values you wish to search with.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Post Re: New to vectors passing parameters

    Quote Originally Posted by osmaavenro View Post
    I am implementing a searching method for my first program using Vectors

    But it does not work not sure why??



    The program runs but the search is unsuccessful.

    Thank you for time and interest.
     
     
    import java.util.*;
    public class test {
     
     
    	public static void main(String[] args) {
    		Customer dan = new Customer("dan rogers","13 apple road",8787);
    		Customer carl = new Customer("carl","17 peach road",89);
    		Customer michael = new Customer("michael ford","345 apple road");
     
    		Vector<Customer> cu = new Vector<Customer>();
    		cu.add(dan);
    		cu.add(carl);
    		cu.add(michael);
     
     
    		Scanner sc = new Scanner(System.in);
    		String c = sc.nextLine();
     
     
     
    		searchVector(cu,c);
     
     
     
     
     
     
     
    	}//end main
     
    	// Search Method
     
    	public static void searchVector(Vector<Customer> v, String c ){
    		int i = v.indexOf(c);
    		if(i >=0){
    			System.out.println("Found "+ i);
    			System.out.println();
    			Customer temp  = v.get(i);
    			System.out.printf("%s\t %s\t %s\t",temp.getName(),temp.getTelephone(),temp.getAddress());
    			System.out.println();
    			}
    		else {
    			System.out.println( c +" Does not exist");
    		}
     
    	} Search Method
     
     
    }// end class
    Your parameters for searchVector are supposed to be v and c.

    You have cu and c.

    int i = v.indexOf(c);

    if c isn't in there, what value will i be?

    Also, maybe Scanner should be static, I'm not positive on that one.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to vectors passing parameters

    See cross posting at Vector Problem - Java Forums

Similar Threads

  1. Question regarding bit vectors...
    By ADizzle491 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 21st, 2010, 08:50 PM
  2. Vectors - accessing an unknown amount of objects
    By fox in forum Loops & Control Statements
    Replies: 1
    Last Post: May 7th, 2010, 03:54 PM
  3. Vectors
    By mgutierrez19 in forum Collections and Generics
    Replies: 4
    Last Post: March 3rd, 2010, 11:46 AM
  4. Passing in more than one flag
    By anon181 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 2nd, 2010, 06:37 AM
  5. Replies: 3
    Last Post: November 15th, 2008, 07:17 AM