Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Vector

  1. #1
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default Vector

    Java Code:
    import java.util.*;
    public class Search {
        
        public static void main(String[] args) {
        	
        	Scanner scanner = new Scanner(System.in);
        	Vector<String> string = new Vector<String>();
        	
    }
    Why is this code not working, i mean it always show Element not found. Please help , thanks
    Last edited by sanox; 09-01-2009 at 09:24 AM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Do not use == to compare Strings. Use Strings "equals(String)" method.

    == compares the reference values, not the String values, "equals(String)" compares the String values, which is what you want.

  3. #3
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Thanks for the help. I have once more problem here

    Java Code:
    import java.util.*;
    public class Sort {
        
        public static void main(String[] args) {
        	
        	Scanner scanner = new Scanner(System.in);
        	String temp;
        	int counter, index;
        	
        	Vector<String> string = new Vector<String>();
        	System.out.println("Please enter five names");
        	System.out.print("First name: ");
        	String name1 = scanner.next();
        	System.out.print("Second name: ");
        	String name2 = scanner.next();
        	System.out.print("Third name: ");
        	String name3 = scanner.next();
        	System.out.print("Fourth name: ");
        	String name4 = scanner.next();
        	System.out.print("Fifth name: ");
        	String name5 = scanner.next();
        	
        	string.addElement(name1);
        	string.addElement(name2);
        	string.addElement(name3);
        	string.addElement(name4);
        	string.addElement(name5);
        	
        	for (counter = 0; counter < string.size()-1; counter++){
        		for (index = 0; index < string.size()-1-counter; index++)
        			if (string.elementAt(index).compareTo(string.elementAt(index + 1)) > 0){
        				temp = string.elementAt(index);
        				string.insertElementAt(string.elementAt(index+1),index);
        				string.insertElementAt(temp, index + 1);
        			}
        	}
        	for (int i = 0; i < string.size(); i++){
        		System.out.printf("The name number %d is %s\n",i+1, string.elementAt(i));
        	}
        	
        }
    }
    Why is it not working?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Describe, precisely, "not working".

  5. #5
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    What i am trying to do here is sorting the 5 names according to the ascending order. But here, the program crashed and it didnt work. But when i entered a,b,c,d,e as name 1,2,3,4,5, it worked. Please help. Because when i entered john,maria,marie,jane, james, it didnt.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    "Not working", how? What, exactly do you mean by not working? Are you getting exceptions, is it simply not producing anything. It is producing something other than what you expected, and exactly how does it differ?

    "Crashed" how? What is the exact exception you're getting? Be exact when you post as none of us have the desire to simulate both the compiler and the virtual machine in our heads as we're reading your code to try and figure out what might be wrong. Exceptions and compiler messages tell you exactly what is wrong.

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    796
    Rep Power
    6

    Default

    Hi,
    U are having sort method in Collections.no need to do this much complicated logic and ur logic is having major flaw .

    U cant directly manipulate the vector like this and no need of 2 forloops. other than the below solution what I have given u can do other thing using toArray() method u convert to array and do sort.

    Gothru this code:

    Java Code:
    import java.util.*;
    public class Test {
        
        public static void main(String[] args) {
        	
        	Scanner scanner = new Scanner(System.in);
        	String temp;
        	int counter, index;
        	
        	Vector<String> string = new Vector<String>();
        	System.out.println("Please enter five names");
        	System.out.print("First name: ");
        	String name1 = scanner.next();
        	System.out.print("Second name: ");
        	String name2 = scanner.next();
        	System.out.print("Third name: ");
        	String name3 = scanner.next();
        	System.out.print("Fourth name: ");
        	String name4 = scanner.next();
        	System.out.print("Fifth name: ");
        	String name5 = scanner.next();
        	
        	string.addElement(name1);
        	string.addElement(name2);
        	string.addElement(name3);
        	string.addElement(name4);
        	string.addElement(name5);
    
        	[B]Collections.sort(string);
    		
    		for(int i = 0; i < string.size() ; i++)
    			System.out.println(string.elementAt(i));
        		[/B]    	
        }
    }
    Ramya:cool:

  8. #8
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Here is the error
    Please enter five names
    First name: adc
    Second name: ad
    Third name: a
    Fourth name: adc
    Fifth name: ae
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2760)
    at java.util.Arrays.copyOf(Arrays.java:2734)
    at java.util.Vector.ensureCapacityHelper(Vector.java: 226)
    at java.util.Vector.insertElementAt(Vector.java:554)
    at T9Q2.main(T9Q2.java:42)

    Process completed.

    Here is what when it works
    Please enter five names
    First name: abc
    Second name: def
    Third name: ghi
    Fourth name: jkl
    Fifth name: mno
    The name number 1 is abc
    The name number 2 is def
    The name number 3 is ghi
    The name number 4 is jkl
    The name number 5 is mno

    Process completed.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Okay, is the point of this exercise for you to sort the Strings using the backing char arrays, or simply by using Strings? If the second, then read the API docs for String and the compareTo method.

    If it is to simply sort the Vector, regardless of how, then use the Collections.sort method (see the API docs), but, you are probably not allowed to use that Class at this point in your course.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    And which code are you using now? Same as you posted above?
    Which sorting method are you trying to use?
    Are you allowed to use Collections.sort?

  11. #11
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    No, i dont think i am allowed to use collections.sort. I am still new and this is introduction to vector topic.

    The question wants me to create a bubble sort that can be used to sort a string Vector object.

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    So then did you read a tutorial on bubble sort to find out how bubble sort works?
    Write down the algorithm first and then it should a simple matter to convert that to code.

  13. #13
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    I have written the algorithm above in my second post.

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by sanox View Post
    I have written the algorithm above in my second post.
    No you did not. You posted the code that is not working.
    You need to understand bubble sort first, then write down the steps to be followed to do it(the algorithm). You then simply convert those steps to code.

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, for one, for a bubble sort you need to use the get and set methods of Vector, not insertAt. Also, that code from your second post is not a bubble sort, it might work for one using the proper methods, but with insertAt, without any remove, definately not.

  16. #16
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    796
    Rep Power
    6

    Default

    Just use toArray() method and convert the vector to array and do sort.

    Don't try to manipulate vector like this.
    Ramya:cool:

  17. #17
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Just use toArray() method and convert the vector to array and do sort.

    Don't try to manipulate vector like this.
    How is using Arrays.sort() any different from using Collections.sort()?

    The course objective is to perform a Bubble Sort (to learn, assumedly, loops, or maybe just algorithm translation).

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Just use toArray() method and convert the vector to array and do sort.

    Don't try to manipulate vector like this.
    If they can't use Collections.sort then most likely they can't use Arrays.sort either.

    Edit:Masi is growing some super fast typing fingers.

  19. #19
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Oh my god, i am confused. I am still new to vector. I dont know much.
    Here is what my algorithm look like.
    h**p://img90.imageshack.us/img90/4928/buble.png

    I hope this helps

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    That is code not an algorithm. You need to understand the difference, it may actually be asked of you in an exam.
    Your code was probably ripped from a C/C++ context because it passes the length of the array to the function. Java doesn't need that since you can get the length of the array from the array itself.

    So modify the method, or create a new one (overload) to take a Vector of Strings instead of the array.

    Also understand that bubble sort is very inefficient.

Page 1 of 2 12 LastLast

Similar Threads

  1. Vector create
    By Warren in forum New To Java
    Replies: 4
    Last Post: 03-02-2010, 02:42 AM
  2. Vector
    By MuslimCoder in forum Advanced Java
    Replies: 4
    Last Post: 08-06-2009, 03:44 PM
  3. Replies: 1
    Last Post: 04-06-2009, 06:45 AM
  4. vector - get
    By natdizzle in forum New To Java
    Replies: 2
    Last Post: 01-02-2009, 04:09 AM
  5. Vector help
    By king_arthur in forum New To Java
    Replies: 3
    Last Post: 01-22-2008, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •