Results 1 to 3 of 3
Thread: Vector
- 05-11-2010, 12:43 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Vector
Hi
I wrote the following program as an example of vector
When I run the program and enter 'stop', it doesn't stop
I've tried many times to figure out the problem but I couldn'tJava Code:import java.util.Vector; import java.util.Scanner; public class TryVector { public static void main(String[] args){ Scanner scan = new Scanner(System.in); Vector<String> v = new Vector<String>() ; String name=""; while(name != "stop"){ v.add(name); System.out.println("Add name"); name = scan.next(); } //print for(int i=0; i<v.size(); i++){ v.get(i); } } }
- 05-11-2010, 12:45 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Object comparison should always be done with the equals() method, the == operator is used for primitive types, and memory locations of object instances.
Swap out while(name != "stop) with while(!name.equals("stop")) and you'll be fine.
And one more thing:
This doesn't acomplish anything, you just get the value from the vector, but you don't store it anywhere, or do anything with it. Maybe you wanted a System.out.println() there?Java Code:for(int i = 0; i < v.size(); i++) v.get(i);
Last edited by m00nchile; 05-11-2010 at 12:48 AM.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-11-2010, 07:23 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
The OP asked a similar question here. Strange he forgot how to do it right ...
kind regards,
Jos
Similar Threads
-
Vector
By Anandiscool in forum New To JavaReplies: 4Last Post: 03-28-2010, 08:10 PM -
Vector<vector> loop thru
By ocean in forum New To JavaReplies: 11Last Post: 11-21-2009, 02:17 PM -
Vector
By MuslimCoder in forum Advanced JavaReplies: 4Last Post: 08-06-2009, 03:44 PM -
Vector<Point2D> list = new Vector<Point2D>();Is it possible for 15,000 Point2D obj???
By Mazharul in forum Java 2DReplies: 1Last Post: 04-06-2009, 06:45 AM -
Vector help
By king_arthur in forum New To JavaReplies: 3Last Post: 01-22-2008, 07:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks