themaster389
themaster389

Reputation: 25

How to create three separate arrays and store ints in one, strings in the others?

I am trying to write a program to read the data in the file “Roster.txt” and store it locally in three separate arrays:

I want to print each array individually.

The Roster.txt file format is:

1 Nathan Clark DB
2 Austin Mancosky RB
4 Drew David QB
7 A.J. Day WR
9 Andrew Thompson QB
11 Nick Holcomb WR
13 Joel Oxton WR
17 Joshua Fischer K
18 Dylan Ulrich DB
22 Michael Garvey LB
23 Josh Neary DB
24 Jalen Clark RB
27 Alexander Villarreal DB
28 Tyler Jenkins RB
31 Peter Krien DB
32 Tevin Anderson RB
35 Zach Kirby P
37 Zach Pierce LB
38 Jacob Zabrowski TE
42 Zach Novotny TE
44 Zach Zillmer LB
51 Jacob Gerzsik LB
52 Eric Bitney LB
53 Aaron Benson LB
57 Zach Sedo LB
58 Alex Prendergast DL
59 Caleb Bryant OL
60 Dan Graf DL
61 Collin Quinn DL
62 Ellis Johnson OL
64 Dylan Bauer OL
69 Eric Graf DL
70 Eric Larson DL
71 Garrett Westerwelle OL
75 Langdon Scott OL
76 Nathan Carpenter OL
80 Samuel Allen WR
84 Dante Marski WR
88 Bryce Neidert TE
97 Sebastian Garcia DL

My current code:

import java.io.File;
import java.util.Scanner;
public class Exercise5 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the file name: ");

    String filename = sc.next();

    File myFile = new File(filename);

    int[] jerseyNumber = new int[39];

    String[] playerName;

    String[] playerPosition;

    int counter = 0;

    while(counter < 39)
    {
        counter++;
    }
 }


public static void printArray(int[] array)
{
for(int i = 0; i < array.length; i++)
{
    System.out.print(array[i] + " ");
}
System.out.println();
}
}

Any help would be appreciated.

Upvotes: 0

Views: 84

Answers (2)

hashmap
hashmap

Reputation: 400

As you mentioned that you want to use arrays. Here is how you can do it :

1)Read the file first and find out the number of lines in the file as it will tell you how many data points you have and you can use it to initialize the array. Although you could just make the size of array as 100 or any other number that you think will fit the data.

BufferedReader reader = null;
    int[] jerseyNumber;
    String[] name;
    String[] position;
    int counter = 0;
    try {
        reader = new BufferedReader(new FileReader("file path"));
        String line = reader.readLine();

        while (line != null) {
            counter++;
            line = reader.readLine();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        reader.close();
    }

2) Read the file again and use StringTokenizer to break the string into tokens and store the tokes appropriately in the array. like this,

jerseyNumber = new int[counter];
    name = new String[counter];
    position = new String[counter];

    try {
        reader = new BufferedReader(new FileReader("file path"));

        String line = reader.readLine();
        counter = 0;
        while (line != null) {
            StringTokenizer string = new StringTokenizer(line, " ");
            jerseyNumber[counter] = Integer.parseInt(string.nextToken());
            name[counter] = string.nextToken() + " " + string.nextToken();
            position[counter] = string.nextToken();
            counter++;
            line = reader.readLine();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        reader.close();
    }

3) Print them out:

    for (int number : jerseyNumber) {
        System.out.println(number);
    }

    for (String playerName : name) {
        System.out.println(playerName);
    }

    for (String playerPosition : position) {
        System.out.println(playerPosition);
    }

The solution here is tedious as you wanted to use array. The better way to do this is using list.

Upvotes: 1

Jameson
Jameson

Reputation: 6659

I mean yea, you can do that,

use:

List<Integer> playerNumbers = new ArrayList<>();
List<String> playerNames = new ArrayList<>();
List<String> playerPositions = new ArrayList<>();

Okay, but don't actually do that. You'd be better off (most likely) by making a class to represent each player:

public class Player {
    final Integer number;
    final String name;
    final String position;

    public Player(final Integer number,
                  final String name,
                  final String position) {
        this.number = number;
        this.name = name;
        this.position = position;
    }

    public String getName() {
        return name;
    }

    public Integer getNumber() {
        return number;
    }

    public String getPosition() {
        return position;
    }
}

And then having a collection of players, a "roster":

List<Player> roster = new ArrayList<Player>();

Then, when you are reading in the data line by line, make a new Player:

while (readStuff()) {
    final Integer number = getNumberFromInput();
    final String name = parseNameBlahBlah();
    final String position = obtainPositionBlah();
    roster.add(new Player(number, name, position));
}

From there you can construct lists if you really do need them:

List<String> playerNames =
    roster.stream().map(x -> x.getName()).collect(Collectors.asList());

for (final String playerName : playerNames) {
    System.out.println(playerName + " is on the roster!");
}

etc.

Next step would be to expand to change the String position to an enum, and also you can make a Roster class directly which has internal logic about how many of each position there are, checks that duplicates aren't added, etc., so instaed of the simple List<Player>, you'd have a Roster class:

Roster roster = new Roster();
roster.add(player);

Where roster.add() performs checks on player to make sure it makes sense to put them on the roster. For example, is there already a player with the number provided? Etc., etc.

Upvotes: 1

Related Questions