Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7780295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:51:04+00:00 2026-06-01T18:51:04+00:00

I am trying to display statistics from a simple text file using arrays in

  • 0

I am trying to display statistics from a simple text file using arrays in Java. I know what I am supposed to do, but I don’t really how how to code it. So can anybody show me a sample code on how to do it.

So let’s say the text file is called gameranking.txt, that contains the following information (This is a simple txt file to use as an example):

Game Event, 1st place, second place, third place, fourth place
World of Warcraft, John, Michael, Bill, Chris
Call of Duty, Michael, Chris, John, Bill
League of Legends, John, Chris, Bill, Michael. 

My goal is to display stats such as how many first places, second places.. each individual won in a table like the following

Placement     First place, second, third, fourth
John            2            0       1      0
Chris           0            2       0      1
etc...       

My thought:
First, I would read the gameranking.txt and stores it to “input”. Then I can use the while loop to read each line and store each line into a string called “line”, afterward, I would use the array method “split” to pull out each string and store them into individual array. Afterward, I would count which placement each individual won and display them into a neat table using printf.

My first problem is I don’t know how to create the arrays for this data. Do I first need to read through the file and see how many strings are in each row and column, then create the array table accordingly? Or can I store each string in an array as I read them?

The pseudocode that I have right now is the following.

  • Count how many rows are there and store it in row
  • Count how many column are there and store it in column
  • Create an array
  • String [] [] gameranking = new String [row] [column]
  • Next read the text file and store the info into the arrays

using:

while (input.hasNextLine) {
    String line = input.nextLine();
    while (line.hasNext()) {
        Use line.split to pull out each string
        first string = event and store it into the array
        second string = first place
        third string =......
  • Somewhere in the code, I need to count the placement….

Can somebody please show me how I should go about doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:51:06+00:00Added an answer on June 1, 2026 at 6:51 pm

    I am not going to write the full program, but I will try to tackle each question and give you a simple suggestion:

    Reading the initial file, you can get each line and store it in a string using a BufferedReader (or if you like, use a LineNumberReader)

    BufferedReader br = new BufferedReader(new FileReader(file));
    String strLine;
    while ((strLine = br.readLine()) != null)   {
    ......Do stuff....
    }
    

    At that point, in the while loop you will go through the string (since it comma delimited, you can use that to seperate each section). for each substring you can
    a) compare it with first, second, third, fourth to get placement.
    b) if its not any of those, then it could either be a game name or a user name

    You can figure that out by position or nth substring (ie if this is the 5th substring, its likely to be the first game name. since you have 4 players, the next game name will be the 10th substring, etc.). Do note, I ignored “Game event” as that’s not part of the pattern. You can use split to do this or a number of other options, rather than try to explain that I will give you a link to a tutorial I found:
    http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html

    As for tabulating results, Basically you can get an int array for each player which keeps track of their 1st, 2nd, 3rd, awards etc.

    int[] Bob = new int[4]; //where 0 denotes # of 1st awards, etc.
    int[] Jane = new int[4]; //where 0 denotes # of 1st awards, etc.
    

    Showing the table is a matter of organizing the data and using a JTable in a GUI:
    http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

    Alrighty…Here is what I wrote up, I am sure there is a cleaner and faster way, but this should give you an idea:

    String[] Contestants = {"Bob","Bill","Chris","John","Michael"};
    
    int[][] contPlace=new int[Contestants.length][4];
    String file = "test.txt";
    
    public FileParsing() throws Exception {
        Arrays.fill(contPlace[0], 0);
        Arrays.fill(contPlace[1], 0);
        Arrays.fill(contPlace[2], 0);
        Arrays.fill(contPlace[3], 0);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String strLine;
        while((strLine=br.readLine())!=null){
            String[] line = strLine.split(",");
            System.out.println(line[0]+"/"+line[1]+"/"+line[2]+"/"+line[3]+"/"+line[4]);
            if(line[0].equals("Game Event")){
                //line[1]==1st place;
                //line[2]==2nd place;
                //line[3]==3rd place;
            }else{//we know we are on a game line, so we can just pick the names
                for(int i=0;i<line.length;i++){
                    for(int j=0;j<Contestants.length;j++){
                        if(line[i].trim().equals(Contestants[j])){
                            System.out.println("j="+j+"i="+i+Contestants[j]);
                            contPlace[j][i-1]++; //i-1 because 1st substring is the game name
                        }
    
                    }
                }
            }
        }
        //Now how to get contestants out of the 2d array
        System.out.println("Placement  First Second Third Fourth");
        System.out.println(Contestants[0]+" "+contPlace[0][0]+" "+contPlace[0][1]+" "+contPlace[0][2]+" "+contPlace[0][3]);
        System.out.println(Contestants[1]+" "+contPlace[1][0]+" "+contPlace[1][1]+" "+contPlace[1][2]+" "+contPlace[1][3]);
        System.out.println(Contestants[2]+" "+contPlace[2][0]+" "+contPlace[2][1]+" "+contPlace[2][2]+" "+contPlace[2][3]);
        System.out.println(Contestants[3]+" "+contPlace[3][0]+" "+contPlace[3][1]+" "+contPlace[3][2]+" "+contPlace[3][3]);
        System.out.println(Contestants[4]+" "+contPlace[4][0]+" "+contPlace[4][1]+" "+contPlace[4][2]+" "+contPlace[4][3]);
    
    }
    

    If you need to populate the contestants array or keep track of the games, you will have to insert appropriate code. Also note, using this 2-d array method is probably not best if you want to do anything other than display them. You should be able to take my code, add a main, and see it run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to display text using a Quartz context, but no matter what
Ive been trying to display formatted content blocks from a xml file with no
I am trying to do some simple statistics on data I am pulling from
Im trying to display text but that isnt working. I create a dynamic text
I am trying display customized text behind my embedded Flash swf file on HTML.
I'm trying to display an UIPopoverController from the rect of a selected text in
Trying to display current time with PHP (using this ): $date = date('m/d/Y h:i:s
I am trying to display the contents of a .cpp file in php. I
I'm trying to display an svg graph with associated javascript in my application using
I'm trying to display the sum of a field in a text box in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.