0

This is probably very basic stuff, but I am not too sure how I should ask questions because I am very new to this, so here goes.

I am practicing vectors and what we can do to them. I have prompted the user for the elements of the vectors (per my directions) among other things successfully. For my next step, I have to "print out the element at index i in each of the two vectors." I was given the methods which I am supposed to use, but the explanations I saw of them were very unclear. Here they are:

Object get (int which)

Object remove (int which)

set (int index, object element)

How would I get the system output to be the element at the index i?

package vectorusage;

import java.util.*;

public class VectorUsage {

public static void main(String[] args) {

    Vector a = new Vector ();
    Vector b = new Vector ();

    System.out.println (a);
    System.out.println (b);

    Scanner input = new Scanner(System.in);

    String first;
    System.out.print("Please enter 4 strings.");
    first = input.next();
    a.add (first);

    String second;
    second = input.next();
    a.add (second);

    String third;
    third = input.next();
    a.add (third);

    String fourth;
    fourth = input.next();
    a.add (fourth);

    String fifth;
    System.out.print("Please enter 4 more strings.");
    fifth = input.next();
    b.add (fifth);

    String sixth;
    sixth = input.next();
    b.add (sixth);

    String seventh;
    seventh = input.next();
    b.add (seventh);

    String eighth;
    eighth = input.next();
    b.add (eighth);

    System.out.println("Vector a is size " + (a.size()) + " and contains: " + (a));
    System.out.println("Vector b is size " + (b.size()) + " and contains: " + (b));

    int i;
    System.out.println("Please enter an integer.");
    i = input.nextInt();

    System.out.println("Element at index " + i + " in Vector a is: " + ;
Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
  • You shouldn't be using `vectors`. They are considered deprecated. Check [here](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) – sam Oct 20 '15 at 23:48

2 Answers2

0

Avoid using vectors, they are deprishiated. Use ArrayList instead. Using a for loop you can simplify your code like below,

(Please note, this code does not validate user input or do error handling)

import java.util.ArrayList;
import java.util.Scanner;

public class Test {

    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        System.out.println("Please enter 8 strings.");

        for(int i = 1; i <= 8; i++) {
            System.out.print("Please enter strings #" + i + ": ");
            numbers.add(input.nextInt());   
        }

        for(int j = 0; j < numbers.size(); j++) {

            System.out.println("Number at index " + j + " is " + numbers.get(j));
        }
    }
}
Achintha Gunasekara
  • 1,165
  • 1
  • 15
  • 29
0

I usually use a mix of while and for loop. The while loop is used to add the user input into the vector. The for loop prints out the elements of the vector using index i. I've set it to print all the elements of the vector but you can modify it by using if conditions. Here's my code, hope it helps!

import java.util.*;
import java.io.*;
public class VectorUsage {

public static void main(String[]args) {
    Scanner input=new Scanner(System.in);
    Vector a=new Vector();
    int count =0;
    while(count<4)
    {
        System.out.print("Enter a string: ");
        a.addElement(input.nextLine());
        count++;
    }
    for(int i=0;i<a.size();i++)
    {
        System.out.println(a.elementAt(i));
    }
    Vector b=new Vector();
    int count1=0;
    while(count1<4)
    {
        System.out.print("Enter a string: ");
        b.addElement(input.nextLine());
        count1++;
    }
    for(int i=0;i<b.size();i++)
    {
        System.out.println(b.elementAt(i));
    }

}

}