I am very new to Java so please ignore if there are obvious mistakes. If my question seems redundant then please guide me towards the correct link. However, I have surfed enough in order to find the answer. Any changes in code will be greatly appreciated.
I am reading an input file and storing the elements of it in a 2D array. What I want to do is split that array in 2 separate arrays. 1st array would contain all the characters which is firstDimension
in my code. Now, I want another array which stores all the integers in an array. I just have to print those arrays. This array should be created as soon as the special character >
is observed.
This can be done in 2 ways:
- Read the strings in the file and store all of the elements in a 2D array and then divide the array into 1 double and one 2D char array
- Read only chars from the file and store it in char array and then read only double values from the file and store it in different array.
My input file has text:
a A b u>0.0001 b b X g>0.0005 Y z N H>0.0003
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Delimiter {
public static void main(String[] args)
{
try {
Scanner scanner = new Scanner(new File("hello.txt"));
scanner.useDelimiter(System.getProperty("line.separator"));
ArrayList<String> list = new ArrayList<String>();
while (scanner.hasNext()) {
list.add(scanner.next());
}
scanner.close();
// finally convert the arraylist to a char[][]
char[][] firstDimension = new char[list.size()][];
for (int i = 0; i < list.size(); i++) {
firstDimension[i] = list.get(i).toCharArray();
}
for (int i = 0; i < firstDimension.length; i++)
{
for (int j = 0; j < firstDimension[i].length; j++)
{
//System.out.println(firstDimension[i][j]);
System.out.print(" "+firstDimension[i][j]);
System.out.println("");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
double
values.... \$\endgroup\$