0

I have a text file with n amount of athletes names, each name on a new line, and the number of athletes is written as an int on the first line of the text file. The code i have written so far reads the text file, and using scanner and creates an array with all the data (Athletes names). What I wondering is, is it possible to use a method to create a separate array for each athlete (I will later fill these with data on scores/ performance). I cannot initialize these before the text file is scanned as the number of athletes will not yet be known.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class TextScanner {

  private static void readFile(String fileName) {
    try {
      List<String> distance = new ArrayList<String>();
      File file = new File(fileName);
      Scanner scanner = new Scanner(file);
      while (scanner.hasNext()) {
          distance.add(scanner.next());
      }
      scanner.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }

 public static void main(String[] args) {

    readFile(args[0]);
  }
}

Very new to Java, so I apologise. I have looked around and cant seem to find any information. Thanks

6
  • may this help you ; stackoverflow.com/questions/12777274/dynamic-arraylist-in-java Commented Mar 12, 2015 at 7:01
  • You can create a new type/class to represent your Athlete info and add to list or a Map... so later you can again set properties of those objects as required.. Commented Mar 12, 2015 at 7:06
  • then you can use HashMap<String,List> and arraylist is dynamic. Commented Mar 12, 2015 at 7:07
  • @newuserua_ext Thanks, yeh i saw that, but i think most of the answers misunderstood his question, and were focusing on adding elements to an existing array. Commented Mar 12, 2015 at 7:16
  • No Most of users doesn't misunderstood , they are trying to tell you the Correct way of doing it ! Everytime the way which solves your problem is not the correct and only way! Commented Mar 12, 2015 at 7:18

5 Answers 5

4

Yes it is possible , but Array has certain limitation as it can not expand and collapse dynamically

Try this way

Create a Class Athelete having attributes you want to keep for athelete

Say

Class Athelete{

private String name;
private Integer age;
private String skill;

// then Getters and Setters for each of these
}

and create a Map

Map<String,Athelete> // having `name` as Key and `athelete object` as value.

Now Read from file and create Athelete Object and add it to Map.

Person p=new Person();
p.setName=nameFromFile();
// also set further attributes
map.put(p.getName,p);
Sign up to request clarification or add additional context in comments.

1 Comment

@XavierDSouza Should have posted it as an answer
2

Since Java is object oriented language think objects and in your case its seems to be Athlete, Score and Performance

public class Athlete {
    String name;
    List<Score> scores = new ArrayList<Score>();
    List<Performance> performances = new ArrayList<Performance>();
}

public class Score {
    String score
}

public class Performance {
    int performnce
}

So List<String> distance = new ArrayList<String>(); this will become List<Athlete> Athletes = new ArrayList<Athlete>();

For each record in the file create Athlete object and store the name in name instance variable. As you get the scores and performances for each particular Athlete updte the Athlete objects already created above.

Update

As other commentator have pointed out using a Map rather than Listwould be suitable. for eg Map<String, Athlete> athletes = new HashMap<String, Athlete>(); This way updating later on receiveing the score s and performances becomes easier. Also, scores and performances are List as an Athlete could have multiple score and performnces.

Comments

2

You can create class Athlete:

class Athlete{
   String name;
   int score;
   Athlete(String name){
      this.name = name;
   }
   int getScore(){return score;}
   void setScore(int score){ this.score = score}
}

and insert instances of Athlete into array or List:

List<Athlete> distance = new ArrayList<>();
distance.add(new Athlete('Some name'));

UPD: if you use Java7 consider java.nio.file.Files instead Scanner for read small files. I think readAllLines method will useful for you.

2 Comments

Thank you! Just to clarify, if i were to run a for loop, or something similar, i could continually create new objects for each athlete using the class? Appreciate your help
@user3636636 Create new instance in loop it usual OOP (and java as OOP language) pattern. You can find it in either book about java.
0

you can first add all the data (Athletes names) to a list and later call list.toArray(new Athlete[list.size()]); this will get you your required Array

Comments

0

Sure, you can create an array for each record from the file. The method signature would look like e.g.

String[] readAthleteData() {...}

However, using collections like ArrayList is usually more convenient than arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.