I don't know what an int[] tab is but if you want to do those other things then this is one way you can accomplish that. Read all the comments in code which is quite extensive and makes the code look a lot larger than it really is. You can delete the comments afterwords if you like:
This code uses a helper method named parseDataLine() which ultimately returns an int[] Array which the example code places into a List<int[]>. I don't know what you are using as a file reader but i have used a String[] Array to simulate the reading of a file. How you want to incorporate this into your existing code is up to you:
// List to hold created int[] Arrays from File Data Lines.
List<int[]> listOfArrays = new ArrayList<>();
// A String[] Array simulating Lines of a Data file:
String[] fileLines = {
"1 2 3 4", // Space Delimiter
"2 4 6 8 10", // Tab Delimiter
"10 20 30 40", // Space Delimiter
"1, 3, 1, 2, 3, 4", // Comma Delimiter
"4;3;2;2;1", // Semicolon Delimiter
"12:13:14:15:16", // Colon Delimiter
"1~3~5~7" // Tilde Delimiter
};
// Iterate through each file line...
for (int ln = 0; ln < fileLines.length; ln++) {
String line = fileLines[ln].trim(); // Remove leading/trailing whitespaces.
int[] array = parseDataLine(line, (ln + 1)); // Parse the file line.
// Add the int[] array to List
listOfArrays.add(array); // Add returned array to list.
}
// Display all the collected Integer Arrays from File:
System.out.println("All the collected Integer Arrays from File:");
for (int[] array : listOfArrays) {
System.out.println(Arrays.toString(array));
}
/**
* Helper method to:<pre>
*
* - Determine the delimiter used in each file line;
* - Convert the File Line to am int[] array;
* - Determine the Lowest elemental value within the Array;
* - Determine the Highest elemental value within the Array;
* - Display pertinent information into the Console Window.</pre>
*
* @param fileLine (String) The actual file data line as it was read.
*
* @param lineNum (int) The current file line number. This line number is to
* be acquired by whatever means necessary.
*
* @return (int[] Array) The generated int[] array from the file data line.
*/
public int[] parseDataLine(String fileLine, int lineNum) {
String line = fileLine;
/* Determine the possible delimiter used within the file
data line. This is done by deleting all numerical data
from the line and from what remains, the first character
should be the delimiter used. This ONLY works for numerical
file data lines. */
String unknownDelim = line.replaceAll("\\d+", "").substring(0, 1);
// Process with the detected delimiter...
/* Determining between a Whitespace delimiter and a Tab
delimiter requires a little more attention. If a Space
delimiter is detected, we check to see if the file line
contains spacing that is at least two spaces. If it does
then we wil consider the line as TAB Delimited. */
String example; // Will hold an example of the Delimiter used for display.
// Is the line Tab Delimited?
if (unknownDelim.equals(" ") && line.contains(" ")) {
// It most likely is...
example = "\" \""; // Provide a Tab Delimiter example;
/* Replace all Tabs or multi-spacing with a single
whitespace for processing. */
line = line.replaceAll("\\s+", " ");
}
else {
/* No, it's either a single space delimiter or
some other character delimiter. Provide an
example of the delimiter. */
example = "\"" + unknownDelim + "\"";
}
/* Split the file data line into a String[] Array based
on the delimiter we detected. Note that we use "\\s*"
on either side of the detected delimiter. This allows
us to split the line on ANY spacing/delimiter combination,
for example: ";", "; ", " ;", " ; ". This ensures
for proper splitting and our array elements will not
contain any whitespaces and therefore eliminates the
need for elemental trimming. */
String[] lineArray = line.split("\\s*" + unknownDelim + "\\s*");
/* Verify that the delimiter we have assumed is in fact
the proper delimiter. We do this by counting the number
array elements in relation to the number of delimiters
used within the created String[] array. The number of
delimiters should always be 1 Less than the total number
of array elements. To do this we get the number of elements
and subtract 1 (lineArray.length - 1) then we get the
number of delimiters used by removing everything from the
the file line except out delimiter and count what remains:
(line.replaceAll("[^" + unknownDelim + "]", "").length())
If the two values are equal then the delimiter is valid.
If the two values are NOT equal then the delimiter is NOT
valid and we return a null array (as coded below). */
if ((lineArray.length - 1) != line.replaceAll("[^" + unknownDelim + "]", "").length()) {
return null;
}
// Create an int[] array from the String[] array elements:
// Declare and initialize an int[] array:
int[] array = new int[lineArray.length];
/* Convert each String numerical element from the String[] Array
to an Integers element and apply to the int[] Array. */
for (int i = 0; i < lineArray.length; i++) {
array[i] = Integer.parseInt(lineArray[i]);
}
// Determine Minimum and Maximum values in the new int[] array:
int lineMinValue = Integer.MAX_VALUE; // An obscure high value.
int lineMaxValue = Integer.MIN_VALUE; // An obscure low value.
// Iterate the the new int[] Array and determine lowest an highest values.
for (int v : array) {
if (v < lineMinValue) { lineMinValue = v; }
if (v > lineMaxValue) { lineMaxValue = v; }
}
// Display the overall results within the Console Window:
System.out.printf("%-30s %-4s%n", ("File Line #" + lineNum + " Data:"), fileLine);
System.out.printf("%-30s %-4s%n", ("Delimiter Detected:"), example);
System.out.printf("%-30s %-4s%n", ("Converted To int[] Array:"), Arrays.toString(array));
System.out.printf("%-30s %-4s%n", ("Minimum Array Value is:"), lineMinValue);
System.out.printf("%-30s %-4s%n", ("Maximum Array Value is:"), lineMaxValue);
System.out.println();
// Return the newly created int[] Array
return array;
}
If properly run the console window should display:
File Line #1 Data: 1 2 3 4
Delimiter Detected: " "
Converted To int[] Array: [1, 2, 3, 4]
Minimum Array Value is: 1
Maximum Array Value is: 4
File Line #2 Data: 2 4 6 8 10
Delimiter Detected: " "
Converted To int[] Array: [2, 4, 6, 8, 10]
Minimum Array Value is: 2
Maximum Array Value is: 10
File Line #3 Data: 10 20 30 40
Delimiter Detected: " "
Converted To int[] Array: [10, 20, 30, 40]
Minimum Array Value is: 10
Maximum Array Value is: 40
File Line #4 Data: 1, 3, 1, 2, 3, 4
Delimiter Detected: ","
Converted To int[] Array: [1, 3, 1, 2, 3, 4]
Minimum Array Value is: 1
Maximum Array Value is: 4
File Line #5 Data: 4;3;2;2;1
Delimiter Detected: ";"
Converted To int[] Array: [4, 3, 2, 2, 1]
Minimum Array Value is: 1
Maximum Array Value is: 4
File Line #6 Data: 12:13:14:15:16
Delimiter Detected: ":"
Converted To int[] Array: [12, 13, 14, 15, 16]
Minimum Array Value is: 12
Maximum Array Value is: 16
File Line #7 Data: 1~ 3~5 ~7
Delimiter Detected: "~"
Converted To int[] Array: [1, 3, 5, 7]
Minimum Array Value is: 1
Maximum Array Value is: 7
All the collected Integer Arrays from File:
[1, 2, 3, 4]
[2, 4, 6, 8, 10]
[10, 20, 30, 40]
[1, 3, 1, 2, 3, 4]
[4, 3, 2, 2, 1]
[12, 13, 14, 15, 16]
[1, 3, 5, 7]