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 2 of 2

Thread: Vector Loop Termination

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Vector Loop Termination

    Hi all,

    I have the following code that stores x5 Floats into a Vector. Can anybody advise me on how to create a loop that will terminate if a negative number is entered?

    import java.util.*;
     
    public class acceptFloats
    {
    	public static void main(String args[])
    	{
    		Vector<Float> vec = new Vector<Float>();
    		Scanner scanner = new Scanner(System.in);
     
    		System.out.println("Please insert a float");
    		for (int i = 0; i <10; i++)
     
    		{
    			Float temp = scanner.nextFloat();
    			vec.add(temp);
    		}
     
    		System.out.println(vec.toString());
    	}
    }

    Much appreciated


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Thumbs up Re: Vector Loop Termination

    for (int i = 0; i <10; i++)

    {
    Float temp = scanner.nextFloat();
    vec.add(temp);
    }
    if you want that when user enter any number loop will terminate without adding that number into vector.
    then check inside the loop if the number is negative break;

    for (int i = 0; i <10; i++)
    {
    Float temp = scanner.nextFloat();

    if(temp<0)
    break;
    vec.add(temp);
    }

    and if you want that number entered negative should be the last number entered in the vector.

    then check the condition like:

    intitalize
    temp =0

    for (int i = 0; i <10 && temp>=0; i++)
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. Replies: 2
    Last Post: December 22nd, 2010, 08:21 AM
  2. Unit Vector
    By mingming8888 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 14th, 2010, 02:53 PM
  3. [SOLVED] 2D Vector
    By nasi in forum Collections and Generics
    Replies: 2
    Last Post: May 6th, 2010, 01:42 AM
  4. String Vector
    By nasi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 3rd, 2010, 11:45 PM
  5. vector
    By sriraj.kundan in forum Java Theory & Questions
    Replies: 8
    Last Post: August 12th, 2009, 10:17 AM