1 answer

I am told to create a fillArray method that will read integers from the file "data.txt"...

Question:

I am told to create a fillArray method that will read integers from the file "data.txt" and store them in the array. I'm told to assume that the file has no more than 100 items in it.

This is what I have so far:

public void fillArray()

{

int curVal;

  

Scanner input = null;

try

{

input = new Scanner(new File("data.txt"));

// set the current number of items in the array to zero

while (input.hasNextInt())

{

curVal = input.nextInt();

// add code to store curVal into the array and update other information as needed

}

input.close();

}

catch (FileNotFoundException e)

{

System.out.println("Could not find data.txt file");

System.exit(1);

}

}

I am unsure what I am missing, if anyone can help, I would be very grateful. Thank you.


Answers

thanks for the question,

You need to create an array of size 100. and then keep adding the numbers you read from the file in the array at the location given by the variable index. What you have so far, is to read all the numbers in the file but the code to store the numbers in the array is missing.

Since you have not shared the complete problem statement, I am assuming there is an int[] array by the name numbers and we want to populate the data from the file into this int[] array.

I have updated your class to do that. Rest everything is all good and sweet : )

If you have any problem, please do comment, no need to post another question for this. Will help you

thanks .

================================================================================

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;  public class SomeClassName {       // assuming you have an array numbers[] of size 100 declared      private int[] numbers = new int[100];      public void fillArray() {          int curVal;         Scanner input = null;         try {             input = new Scanner(new File("data.txt"));             // set the current number of items in the array to zero             int index = 0; // the position in the array where we are going to store the current number read             while (input.hasNextInt()) {                 curVal = input.nextInt();                 // add code to store curVal into the array and update other information as needed                 numbers[index] = curVal;                 index += 1; // increment the index by 1 so that we can add the next number in the next index             }              input.close();         } catch (FileNotFoundException e) {             System.out.println("Could not find data.txt file");             System.exit(1);         }     }      public static void main(String[] args) {      } }

======================================================================

.

Similar Solved Questions

1 answer
Amortization 32. You are considering to buy a car. The value of the car is $21,000...
Amortization 32. You are considering to buy a car. The value of the car is $21,000 and a 6-year auto loan rate based on your FICO score is 5.0%. If you decided to buy the car with a zero money down, what would be your monthly payment for your auto loan? a. $338 33. From Question 32, what is the inte...
1 answer
Medlock Company has two divisions, Wheel and Chassis. The Wheel Dvision manufactures a wheel assembly that...
Medlock Company has two divisions, Wheel and Chassis. The Wheel Dvision manufactures a wheel assembly that the Chassis Division uses. The variable cost to produce this assembly is $7.00 per unit; full cost is $8.00. The component sells on the open market for $15.00 What will the transfer price be if...
1 answer
To make a 50% solution of carbon tetrachloride in benzene, how much carbon tetrachloride should be added to #100*mL# of benzene?
To make a 50% solution of carbon tetrachloride in benzene, how much carbon tetrachloride should be added to #100*mL# of benzene?...
1 answer
Please show all the steps so I can learn. Thank you! 15. Find the electrical power...
Please show all the steps so I can learn. Thank you! 15. Find the electrical power dissipation in the 12 Ω resistor. 4Ω 452 (a) 33 W (b) 3.64W (c) 0.90 w (d) 2.33 W 28 V T 8Ω 6Ω 6Ω 12Ω...
1 answer
How do you solve #9sin^2xtanx=16tanx#?
How do you solve #9sin^2xtanx=16tanx#?...
2 answers
1) All of the following are purposes of internal control except to: A) Promote operational inefficiency....
1) All of the following are purposes of internal control except to: A) Promote operational inefficiency. B) Safeguard assets. C) Ensure accurate and reliable accounting records. D) Prevent and detect error and fraud. 2) An effective accounting information system: A) allows employees flexibility in s...
1 answer
How many #cm^2# are there in #4.21*"inch"^2#?
How many #cm^2# are there in #4.21*"inch"^2#?...
1 answer
What is the relationship between solubility and precipitation?
What is the relationship between solubility and precipitation?...
1 answer
Enter your answer in the box provided. Calculate the pH of a 1.0 x 10-5 M...
Enter your answer in the box provided. Calculate the pH of a 1.0 x 10-5 M solution of HNO2. pH= )...
1 answer
At 150 K the reaction below has K. = 4.8*10-12 A + 2B + C +...
At 150 K the reaction below has K. = 4.8*10-12 A + 2B + C + 2D You start the reaction with [A] = 6.6 M, [B] = 1.7 M, [C] = 0.0 M and [D] = 0.0 M a. If you let the reaction come to equilibrium what will [D] be? (8 points) b. What is K. for the following reaction at 150 K. (6 points) 3C + 6D 3A + 6B...
1 answer
Constants Part A The dipole moment of the water molecule (H20) is 6.17 x 1030 C-m....
Constants Part A The dipole moment of the water molecule (H20) is 6.17 x 1030 C-m. Consider a water molecule located at the origin whose dipole moment p points in the +x-direction. A chlorine ion (Cl, of charge -1.60 x 10-19 C, is located at z 3.00 x 10 9 m Assume that z is much larger than the sepa...
1 answer
The following information is related to Alpha Company: Estimated manufacturing overhead costs for the year Actual...
The following information is related to Alpha Company: Estimated manufacturing overhead costs for the year Actual manufacturing overhead costs for the year Estimated direct labor hours for the year Actual direct labor hours for the year $254,000 $202,000 50,000 35,000 Alpha uses normal costing and a...
1 answer
Can you help me with this question? 7. On Day 4, you will obtain your sequence....
Can you help me with this question? 7. On Day 4, you will obtain your sequence. An old technology would run chain-terminated products on a polyacrylamide gel to identify the fragments. Let's say you ran 4 reactions. Each included all of the dNTPs and one of the four ddNTPs. On the figure belo...