So I have an assignment to read in animal's common names and scientific names from a text file, separated by commas. (ie. dog,Canis lupus). Sort them alphabetically by Common name first and print to console, then sort alphabetically by Scientific name and print to console. I'm having issues with a class to read in the file and place them into an array. Sorry if this code is horribly wrong, I'm still learning. Here is the code that's giving me issues:
String[] commonname = new String[25];
String[] scienname = new String[25];
public static String readNames(String[] commonname, scienname) throws IOException {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader("/C:/Desktop/animals.txt"));
String line = null;
while ((line = inputStream.readLine()) != null) {
Scanner sc = new Scanner(line);
sc.useDelimiter(",");
String common = sc.next();
String scient = sc.next();
String list = new String(common, scient);
}
}
finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
}
I'm getting 2 errors,
"
File: C:\Users\Nathan\Desktop\Program5.java [line: 16]
Error: Syntax error on token "(", ; expected
File: C:\Users\Nathan\Desktop\Program5.java [line: 16]
Error: Syntax error, insert ";" to complete LocalVariableDeclarationStatement
"
I already gave the ( in the line it's asking for, and the ; shouldnt be needed as far as I'm aware. It's very incomplete, and I'd love help just reading the names into a string that has both the Common and Scientific names, but can be sorted alphabetically by either or if that makes sense. Here's the entirety of the code if that sheds any light:
/**
* Auto Generated Java Class.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Program5 {
String[] commonname = new String[25];
String[] scienname = new String[25];
public static void main(String[] args) throws IOException {
String list = readNames;
Arrays.sort(list);
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
public static String readNames(String[] commonname, String[] scienname) throws IOException {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader("/C:/users/Nathan/Desktop/animals.txt"));
String line = null;
String[] list = new String[25];
while ((line = inputStream.readLine()) != null) {
Scanner sc = new Scanner(line);
sc.useDelimiter(",");
String common = sc.next();
String scient = sc.next();
String list = new String(common, scient);
}
}
finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
}