Results 1 to 5 of 5
  1. #1
    reddens84 is offline Member
    Join Date
    Jul 2014
    Posts
    12
    Rep Power
    0

    Default 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Help with Vectors

    First, Vector is a legacy class. Modern code should mostly use ArrayList instead.

    Is there any way to move from inputting integers to printing them, without entering a non-integer (for example, hitting enter)?
    Generally, not when you're using a Scanner for the input.

    You can, however, print the numbers inside the while loop by assigning the nextInt() to a variable and both printing it and adding it to the Collection.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    reddens84 is offline Member
    Join Date
    Jul 2014
    Posts
    12
    Rep Power
    0

    Default Re: Help with Vectors

    How will that end the user's ability to keep entering additional numbers?

    Maybe I don't understand your explanation, but what I want to happen is this - someone can enter as many integers as they like, but then I need a way to let the program know when they've entered all of the values that they want to (again, preferably without having to enter a non-integer character or what have you).

    The best workaround I can think of is to ask the user beforehand how many numbers they'd like to use, and enter that number as the size of an array... But like I said, I'm playing with vectors so I'd like to stick with that. Am I just S.O.L.?
    Last edited by reddens84; 10-01-2014 at 11:54 AM.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    10

    Default Re: Help with Vectors

    Accepting purely numeric input is easily achievable but does throw up a couple of potential issues. As you are not allowing the user to use a non-numeric key, you are relying on the application to know when the user has finished entering a number. Just off the top, there are two basic ways:

    1) Assume each value is a single digit.
    This does limit yourself to the values 0 to 9, both for the number of values and the values themselves.

    2) Use a timer and listener to detect when the user has finished typing.
    If this is set too fast the user might not finish typing before the timer elapses. Too slow, and the user will be waiting before entering a new number. This also adds unnecessary complexity to the application.

    Either way, these ideas are not particularly elegant but provide possible solutions based on the requirements.

    Regards.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Help with Vectors

    Too tedious; read an entire line and try to convert it to an int (see the Integer class); if it fails, stop reading more lines, otherwise continue reading more ints.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Vectors
    By Zelaine in forum New To Java
    Replies: 4
    Last Post: 09-10-2013, 03:39 PM
  2. Need Help with Vectors
    By MadMikeUCF in forum New To Java
    Replies: 1
    Last Post: 04-13-2012, 01:07 AM
  3. Need Help Regarding Vectors
    By xHoly in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 07:56 AM
  4. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 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
  •