Renuz
Renuz

Reputation: 1657

Is there an Easy way to enter a list of items into an Array, say 50-100?

I am trying to create an array with about 100 names. I want the names to be the ones I have selected not randomly generated ones. So each position of the Array will hold a string of a name. Is there an easier way to do this than Array[1] = "name; Array[2] = "name"; ? The way I was thinking was to use a for loop to get the information from a data file or a text file but I have no idea how to do that. What is the easiest way to accomplish this task?

Upvotes: 1

Views: 206

Answers (7)

Paul Vargas
Paul Vargas

Reputation: 42030

If you have a file with one name by line:

public static String[] getArrayStringRandom() {
    try {
        List<String> list = new LinkedList<String>();
        FileReader fileReader = new FileReader("file.txt");
        BufferedReader in = new BufferedReader(fileReader);
        for (String name; (name = in.readLine()) != null;) {
            list.add(name);
        }
        in.close();
        fileReader.close();
        // Collections.shuffle(list); // Random permutes
        return list.toArray(new String[0]);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Upvotes: 5

apnorton
apnorton

Reputation: 2440

If this is just for testing a homework assignment, you can use the < command-line tool to pass the file and a standard scanner. Example

import java.util.Scanner;

public class Example {
  public static void main(String[] args){
    Scanner cin = new Scanner(System.in);
    String[] stringArray = new String[100];

    for(int i = 0; i < 100; i++)
      stringArray[i] = Scanner.nextLine();
  }
}

Now if you have a file (filename.txt) that contains the name on each line, call the program from the cprompt with: java Example < filename.txt

This is good for testing...

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236034

The simplest way to initialize an array would be:

String[] array = { "a", "b", "c", "d", "e" };

... But if you're planning to read the names from a file, you'll have to store it one by one:

List<String> list = new ArrayList<String>();
// ... read each name from the file
list.add(name);
// finally, convert the list to a String[]
String[] array = list.toArray(new String[list.size()]);

Upvotes: 0

Robin
Robin

Reputation: 36611

If you need to fill the array with hardcoded values, you can just use

String stringArray = new String[]{ "Name1", "Name2", ... };

If you want to retrieve it from a file as you suggest, you can use a loop similar to

String stringArray = new String[100];
for( int i = 0; i < stringArray.length; i++ ){
   stringArray[i]=retrieveNextValueFromFile();
}

Upvotes: 0

random
random

Reputation: 8608

Well, if you already have all the names listed out in a text file then then parsing a txt file and populating the array is an efficient choice.

Here is everything you will need to accomplish it.

If however you do not already have all the names listed out digitally (aka if you have them written down on a sheet of paper) and you know they will not change then just manually add them in the code.

A draw back of doing this however is if you ever need to update the list of names it is much easier to deal with a txt file rather than hard code.

Upvotes: 0

Travis J
Travis J

Reputation: 82297

Perhaps like this. Reading it from a text file could work too but would require more effort although it would be more dynamic.

String[] myArray = {"Jon","Mike","Dave","Ed","Ted","Shirly","Jane","Robert"};//etc

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726699

You can use an array initializer:

An array initializer may be specified in a declaration, or as part of an array creation expression, to create an array and provide some initial values. It is written as a comma-separated list of expressions, enclosed by braces { and }.

Array = new String[] {"name1", "name2", ..., "name100"};

Upvotes: 2

Related Questions