Question
Hello. I need help understanding how to create a 2D array based upon a text file. I'm reading in from a text file full of
Hello. I need help understanding how to create a 2D array based upon a text file. I'm reading in from a text file full of ints. The text file is called TempInFile. I'm trying to understand how to create and initialize a 2D array based upon that text file. I know how to create an array by telling it specifically how large it is ( double[][] temps = new double[2][12];). But I cant grasp how to use the text file to distinguish what size the array should be. I'll include a snip from my homework directions. There's more to it than what I'm showing that relates to a different class within my program, but right now I'm just trying to create the array within my Driver/main so I can attempt to manipulate the data to show some averages(but that's a different step).
My current driver code is as follows:
package project5;
import java.io.FileReader; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
Scanner inFile = new Scanner(new FileReader("TempInFile"));
PrintWriter outFile = new PrintWriter("outFile.txt");
int r = 2;
int c = 0;
int [][] temps = new int [r][c];
}
}
----------------------------------------------------//end--------------------------------------------------------------
/*was attempting to maniuplate 'c' somehow, but have gotten too frustrated. Keeping r and c isn't necessary, its just a concept I was toying with.*/
I manually set the row of my array since the text file is only 2 lines because I didn't see a point writing excess code when I know exactly how short it is. On the other hand I know how many columns are needed as well, but I cant figure out an effective way set the size without manually setting it. I don't want to assume I can manually set every array size every time. Thank you!
Write a java program that uses a two-dimensional array to store and process the highest and lowest temperatures for each month of the year. The input should be read from a text file using the following data 30 40 45 60 70 90 89 95 79 90 70 40 -10 -8 20 30 50 75 85 79 50 80 30 12 In this table rows represent temperatures and columns represent months. Row zero represents the highest temps while row 1 represents the lowest temps. For example, 30 represents the highest temperature in the month of January and -10 represents the lowest temperature in the month of JanuaryStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started