1

I have a text file that contains data like this:

HW1  HW2  HW3  HW4  HW5
97   64   75   100  21   John
19   68   89   49   97   Kim
28   83   48   44   91   Kathy
69   66   78   87   85   Steve
99   94   93   96   91   Stacy
35   75   65   55   45   Faith

I tried to read the size with BufferedReader, so that I can convert them to several arrays (i.e. HW1arr, HW2arr, etc...). How can I find the array size like this 2D file?

public static void main(String[] args) {

    Scanner reader = null;
    try {
        File f = new File("inputHW7.txt");
         reader = new Scanner(f);
    } catch(FileNotFoundException e) {
    }


    double[][] size2d = null;
    int count = 0;
    int rowCount =0;
    int colCount = 0;
    int readChar = 0;
    boolean empty = true;

    rowCount = reader.nextInt();
    for(int i=0 ; i<rowCount ; i++){
        colCount = reader.nextInt();
        size2d[i] = new double[colCount];
        System.out.print("debugs column = "+size2d[i]);
        for(int j=0 ; j<colCount ; j++){
            size2d[i][j] = reader.nextDouble();
            System.out.println("debug rows = "+ size2d[i][j]);
        }
    }
}
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Mark Yo
  • 49
  • 6
  • Are there specific questions that you have? I am not entirely sure what you are asking. – Dmich May 05 '18 at 20:51
  • Show what you have tried. I suggest you read [this SO Post](https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa). – DevilsHnd - 退職した May 05 '18 at 20:56
  • @DevilsHnd, thanks. I did check that site. However, it only gives row length. However, I need row and column, I manage to find the row length. I still need column. – Mark Yo May 05 '18 at 22:41
  • @Dmich My goal is to turn these 2D data to 1D array. so my first step is to find the size of row and column, and I stuck at this. And then I want to convert the column data to 1D array, but it contents string and int. How do I convert them? – Mark Yo May 05 '18 at 22:44
  • @MarkYo - Do you want the header text included within each array as the first element? This would then mean you would want the data Types for each Array to be Object or String. Or is it you just want the raw columnar integer values in your Arrays? This then would mean you would want the data types for each Array to be one of the integer types. Perhaps not, perhaps you want all the Arrays to be of String Data Type. Which is it? What about the names column? Is that to be ignored? You need to really think about your question so that it doesn't generate more questions. We can't read your mind. – DevilsHnd - 退職した May 05 '18 at 22:59
  • @DevilsHnd Please forgive my poor explanation because this is my first java class. No java exp at all. My homework requires to extract the 2D data from file, and then transfers each vertical(column) data to single array, but it includes title and grades. that's why I don't know how to array them. – Mark Yo May 05 '18 at 23:13

2 Answers2

0
  • Scanner provides APIs to check whether there is another item in the input stream (even the type specific ones)
  • hasNext* family of APIs and next* family of APIs are what you need.
  • Use it with a loop.
  • Make use of ArrayList so that you don't need to know size before hand.

  • if number of columns is fixed, You can read entire line, and split by space. Length of the resulting array will be your column size. Row size however can't be obtained beforehand. Hence List collection should be preferred.

https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Scanner.html

Arun Karunagath
  • 1,593
  • 10
  • 24
0

Here is a concept. I won't do your homework for you.

  1. Fill an ArrayList of String type with all lines of the file (excluding blank lines if any). Be sure to close file reading when done with the file. List<String> list = new ArrayList<>();. You will need to utilize the List.add() method to add each required file data line to the list.
  2. The ArrayList size (list.size()) now tells you how long (the length) each Array you want to create is to be. Declare each of your column Arrays (ie: hw1Array[] to hw5Array[]) as double data type. Looking at your code, this is your intention anyways. Be sure to initialize the length of each array as well right away: double[] hw1Array = new double[list.size()];.
  3. Since the names column is to be ignored we won't worry about it.
  4. Now, create a for loop so as to iterate through the List you created by reading the data file. Use i as the iteration variable. for (int i = 0; i < list.size(); i++) { }.
  5. Within the for loop declare a string Array and utilize the String.split() method against each List element: String[] lineValues = list.get(i).split("\\s+");. The "\\s+" is a Regular Expression within the split() method which means to split the string on a delimiter containing any number of white-spaces.
  6. Now it's just a matter of filling your 5 columnar arrays. Again within your for loop fill each columnar array with data from the lineValues[] String Array created in the previous code line and doing the conversion to double data type in the process using the Double.parseDouble() method, for example: if (lineValue.length >= 1) { hw1Array[i] = Double.parseDouble(lineValue[0]); } if (lineValue.length >= 2) { hw2Array[i] = Double.parseDouble(lineValue[1]); } if (lineValue.length >= 3) { hw3Array[i] = Double.parseDouble(lineValue[2]); } if (lineValue.length >= 4) { hw4Array[i] = Double.parseDouble(lineValue[3]); } if (lineValue.length >= 5) { hw5Array[i] = Double.parseDouble(lineValue[4]); }

    We use a if condition to ensure there is actually data to parse to double otherwise we end up with an NumberFormatException.

You arrays are now created.

To quickly display your Arrays:

System.out.println(Arrays.toString(hw1Array));
System.out.println(Arrays.toString(hw2Array));
System.out.println(Arrays.toString(hw3Array));
System.out.println(Arrays.toString(hw4Array));
System.out.println(Arrays.toString(hw5Array));
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22