Tuesday, September 30, 2014

Help with Vectors




I'm playing with vectors for the first time... What I'm trying to do is to allow a user to input one or more integers and store them in a vector for manipulation later on in the program... Here's the portion of the program I'm working with:






Java Code:






package com.itse2317;
import java.util.*;

public class VectorTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

Vector<Integer> v = new Vector<>(0,2);

System.out.print("Enter numbers for factorial: ");

while(input.hasNextInt())
{
v.addElement(input.nextInt());
}

System.out.println("Elements in vector: ");

for(int i : v)
{
System.out.print(i + " ");
}
System.out.println();

}
}




My question is this: Is there any way to move from inputting integers to printing them, without entering a non-integer (for example, hitting enter)?

I looked at the API for the Vector class, and either I'm not thinking about the problem the right way to be able to find an answer, or it's just not there.




Any help would be appreciated.







No comments:

Post a Comment