Question

IN JAVA Read the runners data file into arrays. ONE string array and ONE 2-dimension array....

IN JAVA Read the runners data file into arrays. ONE string array and ONE 2-dimension array. Each runner has 8 pieces of data, which includes name, and how much they ran for each of 7 days. Print out the raw data in columns. Below the raw data, print out the total miles ran for all of the runners.

THIS IS TXT FILE :

Keith
10
8
4
5
12
3
2
Anna
9
8
7
6
5
12
13
Derick
1
2
0
22
11
0
7
Susan
4
5
6
7
8
9
2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Create a input.txt file in D drive with following details

Keith
10
8
4
5
12
3
2
Anna
9
8
7
6
5
12
13
Derick
1
2
0
22
11
0
7
Susan
4
5
6
7
8
9
2

Here is the screenshot

Code:

import java.io.*; //to read data from file
public class Runner
{
// counter is used for type of data. eg runner name, day1, day2...day7
// i is used for runner. eg runner1, runner2....runner7
int counter=0, i=0;
String nameList[] = new String[4];//we have 4 runner data so kept array size =4, change if required.
int day1[] = new int[4];
int day2[] = new int[4];
int day3[] = new int[4];
int day4[] = new int[4];
int day5[] = new int[4];
int day6[] = new int[4];
int day7[] = new int[4];
int total[] = new int[4];
String line;

void readFromFile()
{
try
{
// BufferedReader object to read data from file
BufferedReader read = new BufferedReader(new FileReader("d:\\input.txt"));

while ((line = read.readLine()) != null) //read data line by line
{
if(counter%8==0) //counter will keep track about type of data
nameList[i]=line;
else if(counter%8==1)
{
day1[i] = Integer.parseInt(line);//convert string to number
total[i] = total[i] + day1[i];//to count total for each runner
}
else if(counter%8==2)
{
day2[i] = Integer.parseInt(line);
total[i] = total[i] + day2[i];
}
else if(counter%8==3)
{
day3[i] = Integer.parseInt(line);
total[i] = total[i] + day3[i];
}
else if(counter%8==4)
{
day4[i] = Integer.parseInt(line);
total[i] = total[i] + day4[i];
}
else if(counter%8==5)
{
day5[i] = Integer.parseInt(line);
total[i] = total[i] + day5[i];
}
else if(counter%8==6)
{
day6[i] = Integer.parseInt(line);
total[i] = total[i] + day6[i];
}
else if(counter%8==7)
{
day7[i] = Integer.parseInt(line);
total[i] = total[i] + day7[i];
//increase i as we have collected data for ith runner, now need to collect data for (i+1)th runner
i++;
}

counter++;
}
read.close(); //close input file

}
catch(FileNotFoundException e) //exception for file not found
{
System.out.println("File not found.");
}
catch(Exception ex) //other exceptions like permission denied
{
System.out.println(ex.toString());
}
}

void showData()
{
System.out.print("\n");
for(int x=0; x<i; x++) //i is having count of total no or runner
{
System.out.print(nameList[x] + " "); //print name
}
System.out.print("\n");
for(int x=0; x<i; x++)
{
if(day1[x]>=10) //if number is more than 2 digit than just print number
System.out.print(day1[x] + " ");
else //if number is 1 digit than print number with a space
System.out.print(" " + day1[x] + " ");
}
System.out.print("\n");
for(int x=0; x<i; x++)
{
if(day2[x]>=10)
System.out.print(day2[x] + " ");
else
System.out.print(" " + day2[x] + " ");
}
System.out.print("\n");
for(int x=0; x<i; x++)
{
if(day3[x]>=10)
System.out.print(day3[x] + " ");
else
System.out.print(" " + day3[x] + " ");
}
System.out.print("\n");
for(int x=0; x<i; x++)
{
if(day4[x]>=10)
System.out.print(day4[x] + " ");
else
System.out.print(" " + day4[x] + " ");
}
System.out.print("\n");
for(int x=0; x<i; x++)
{
if(day5[x]>=10)
System.out.print(day5[x] + " ");
else
System.out.print(" " + day5[x] + " ");
}
System.out.print("\n");
for(int x=0; x<i; x++)
{
if(day6[x]>=10)
System.out.print(day6[x] + " ");
else
System.out.print(" " + day6[x] + " ");
}
System.out.print("\n");
for(int x=0; x<i; x++)
{
if(day7[x]>=10)
System.out.print(day7[x] + " ");
else
System.out.print(" " + day7[x] + " ");
}
System.out.print("\n---------------------------------------------");
System.out.print("\n");
for(int x=0; x<i; x++)//printing total
System.out.print(total[x] + " ");
System.out.print("\n---------------------------------------------");
System.out.print("\n\n");
}

public static void main(String arga[]) //main method to control the program
{
Runner run = new Runner(); //crating object of class. it will call constructor
run.readFromFile(); //call function to read data from file
run.showData(); //call function to print data
}
}

Output

Code Screenshot

Add a comment
Know the answer?
Add Answer to:
IN JAVA Read the runners data file into arrays. ONE string array and ONE 2-dimension array....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assume that an array A is given to you in a txt file. You should read...

    Assume that an array A is given to you in a txt file. You should read it. If you think this is a time-dependent data, print the number of local minimums in the array to a txt file. Local minimum means the element which is less than the previous element and the following element. Please do not use any additional library. For example, if A=[3, 2, 9, 8, 7, 8, 6], the code should print 2 and 7. (Local minimums...

  • Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays...

    Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays for the wind speed and for the temperature. You will then use the stored data to calculate and output the average wind speed and the average temperature. Create a constant with a value of 30 and use that to declare and loop through your arrays and as a divisor to produce your averages. Here is the data file (below). 1. 14 25 2. 12...

  • IN PYTHON PLEASE...... Process bowlers and their 3 bowling scores. Use my data file below: bowlers2.txt...

    IN PYTHON PLEASE...... Process bowlers and their 3 bowling scores. Use my data file below: bowlers2.txt ( Showed below) And you can also use a while loop to read your file if you prefer. How to average down an array, which you don’t have to do. In this case that would be the game 1 average for all bowlers for example. Find the low average and high average. Start with read the data into arrays/lists and printed out the arrays/lists...

  • From: Java Programming: From Problem Analysis to Program Design, 5th ed. Ch. 9, page 636, Problem...

    From: Java Programming: From Problem Analysis to Program Design, 5th ed. Ch. 9, page 636, Problem Exercise 12: Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week they run certain miles and write them into a notebook. At the end of the week, they would like to know the number of miles run each day, the total miles for the week, and average miles run each day. Write a program to help...

  • can someone please comment through this code to explain me specifically how the variables and arrays...

    can someone please comment through this code to explain me specifically how the variables and arrays are working? I am just learning arrays code is below assignment C++ Programming from Problem Analysis to Program Design by D. S. Malik, 8th ed. Programming Exercise 12 on page 607 Lab9_data.txt Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week, they run a certain number of miles and write them into a notebook. At the...

  • For your second program, please read the data from the input file directly into an array....

    For your second program, please read the data from the input file directly into an array. (You may safely dimension your array to size 300.) Then close the input file. All subsequent processing will be done on the array. Your program should have two functions besides main(). The first function will print out the contents of the array in forward order, 10 numbers per line, each number right justified in a 5 byte field. The second function will print out...

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • You are required to open a file for input and read the data in that file...

    You are required to open a file for input and read the data in that file into an array (named data). Use the variable size to count how many elements in the file. Create the file yourself in notepad and call it data.txt so that it has the following numbers in order: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 Please help with this. In C please

  • DESCRIPTION Complete the program using Java to read in values from a text file. The values...

    DESCRIPTION Complete the program using Java to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows)...

  • Playing With Arrays The following problems are to give you practice with programming with arrays. Write...

    Playing With Arrays The following problems are to give you practice with programming with arrays. Write a program to: 1.Ask the user for numbers and place them in an array sequencially. 2.Next, print out the values in the array in the order they are stored. 3.Search the array for the postion containing the smallest value. 4.Print out the position where the smallest value was found. 5.Swap the smallest value with the value in the first postition of the array. 6.Print...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT