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

Thread: user input strings to vectors

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

    Default user input strings to vectors

    Pretty much I have to do this.

    I have this.
    import java.util.*;
    public class Prog1
    {
     
    public static void main(String args[])
    {
     
    Vector vec = new Vector(); ]

    After that I don't know how to get a user input to be a string and stored into a vector. I know the user input command is "console.readLine." How would i use this command to store the string value as a vector? I know once I get this command to store the values I can use a for loop to allow it to let me store 10 string items.
    Last edited by vudoo; February 6th, 2011 at 05:46 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: user input strings to vectors

    Here's a neat and simple guide I just found:
    How-To-Use Scanner
    For the above, what you're after would be towards the bottom of the page mostly.
    Additionally, It wouldn't hurt to read the API for scanner.
    Scanner (Java 2 Platform SE 5.0)
    Last edited by newbie; February 4th, 2011 at 06:23 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: user input strings to vectors

    would scanner be the same thing as a vector?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: user input strings to vectors

    Scanner (Java Platform SE 6)

    Vector (Java Platform SE 6)

    No, a Vector is a generic type of thing.
    Vector<Object> v = new Vector<Object>();

    Vector<Integer> v2 = new Vector<Integer>();

    A Vector is kinda like an array but it can be extended until it runs out of room...though don't take it that far....unlike an array which has a fixed size.

    A Vector has some method called

    for

    Vector<T> ;

    public T get(int index)
    {
    return the data at the index...or returns null if invalid index range)
    }

    public void add(T data, int index)
    {
    adds data at index or doesn't if you give it a bad index
    // Note...you can add data at index size but not more than size or less than 0.
    }

    public void add(T data)
    {
    // adds data at last index in Vector
    }

    public void remove(int index)
    {
    // removes data at index..or doesn't if it's invalid.
    }

    public int size()
    {
    returns the size of Vector
    }

    Look up Collection on Java API for more details.

    A Scanner is used mainly for input...either from a File or from a console.

    a classic example is

    static Scanner console = new Scanner(System.in);

    the variable console is a Scanner object that reads in user input from keyboard

    Scanner reader = new Scanner("AFile.txt");

    would be an example of a Scanner that read from a file.

    Here's how you might deal with Vector problem.

    1.) Make a Scanner with System.in as parameter.
    2.) Make a Vector<Type> object
    3.) Make a for loop that goes from 0 to less than 10.
    4.) For each iteration...println telling the user to input the value
    5.) Set the value read by the Scanner object to a variable called temp.
    {For a String..for instance

    String temp = console.nextLine();
    }
    6.) VectorObject.add(temp);
    Last edited by javapenguin; February 4th, 2011 at 07:18 PM.

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

    Default Re: user input strings to vectors

    import java.util.*;
    public class Prog1
    {
     
    public static void main(String args[])
    {
     
    Vector<String> vec = new Vector<String>();
    Scanner scanner = new Scanner(System.in);
     
    System.out.println("Please insert 10");
    for (int ir = 0; i <= 10; i++)
    {
    String temp = console.nextLine()
    }

    would this be correct? I don't have an IDE available to me at the moment.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: user input strings to vectors

    Quote Originally Posted by vudoo View Post
    import java.util.*;
    public class Prog1
    {
     
    public static void main(String args[])
    {
     
    Vector<String> vec = new Vector<String>();
    Scanner scanner = new Scanner(System.in);
     
    System.out.println("Please insert 10");
    for (int ir = 0; i <= 10; i++)
    {
    String temp = console.nextLine()
    }

    would this be correct? I don't have an IDE available to me at the moment.
    Yes, but get rid of the = in the for loop. It's less than 10.

    Also, after that...tell it

    vec.add(temp);

    inside the for loop and that should do it.

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

    Smile Re: user input strings to vectors

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

    and this will execute 11 times not 10.

    Please change it to <10.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

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

    Default Re: user input strings to vectors

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

    This my new code and I keep getting this error while compiling I am not sure why. I am using Netbeans 6.9.1.

    C:\Users\Vince\Documents\NetBeansProjects\hw100\src\Main.java:18: illegal start of expression
    System.out.println(vec.toString());)
    1 error
    C:\Users\Vince\Documents\NetBeansProjects\hw100\nbproject\build-impl.xml:528: The following error occurred while executing this line:
    C:\Users\Vince\Documents\NetBeansProjects\hw100\nbproject\build-impl.xml:261: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 0 seconds)

  9. #9
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: user input strings to vectors

    String temp = console.nextLine();
    Is wrong because your Scanner object is called scanner not console.

    Secondly, Remove the last bracket from
    System.out.println(vec.toString()); ).
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: user input strings to vectors

    TY for the help Newbie, I had the same problem as Vudoo.

Similar Threads

  1. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 10:00 PM
  2. Allowing user to input variable.
    By That1guy in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 11:30 PM
  3. Help with toString and accesing user input
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 07:58 PM
  4. Problems with Input Strings
    By Bekuraryou1228 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 4th, 2010, 04:34 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM