I try to read in a tab-seperated TSV-File in Java and want to store the values per line in 2 variables. (variable name: everything before the tab, variable 2: everything after the tab). The file looks like this:
Name1 Lastname1 TAB directory1/subdir1/subdir11
Name2 SecondName2 Lastname2 TAB directory1/subdir2/subdir22
So i have 1) Names and Last Names, seperated by Space 2) TAB 3) url without blank spaces 4) new line (after the last url-character, so that the next entry starts in a new line)
I followed a tutorial and what i already have is:
// Open TSV File
public static Scanner openFile(String path) {
try {
Scanner scan;
scan = new Scanner(new File(path));
System.out.println("TSV-File found");
return scan;
} catch (Exception e) {
System.out.println("TSV-File not found");
}
return null;
}
public static void readFile(Scanner scan) {
while(scan.hasNext()) {
String name = scan.next();
String url = scan.next();
System.out.printf("%s %s\n", name, url);
}
}
The problem is in my readFile() Method, because I do not know how to to say "take everything before tab and store it to variable name" and "take everything from tab to new line and store it to variable url".
Thanks and greetings, Patrick