0

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();
            }
        }
    }
}

}
11
  • What sort of issues are you having? Commented Dec 15, 2016 at 21:03
  • Sorry, just added the error messages below the code Commented Dec 15, 2016 at 21:06
  • The term you're looking is "comma-separated-values" Commented Dec 15, 2016 at 21:06
  • where is your catch block? Commented Dec 15, 2016 at 21:06
  • I was more worried about getting the errors taken care of before writing the catch block @an Commented Dec 15, 2016 at 21:08

2 Answers 2

1

The line reading and parsing looks fine, but you're not storing the list anywhere. Before your while loop, declare a container for your pairs. You can use an Array, but since they have to be sorted alphabetically, you may as well throw them into a TreeSet<(String,String)>, which will sort them for you. To print out the sorted list, call the set's iterator() method to loop through its values.

Sign up to request clarification or add additional context in comments.

2 Comments

Added "String[] commonname = new String[25]; String[] scienname = new String[25];", I've never used the TreeSet or iterator in the class before, is there a way to do it using an Array and sort it in the Array?
Also I'm still getting the same errors telling me I need a "(" where there already is one, and that I need a ";" where I already have one. Any clue what is causing that?
0

It seems as if your issues is in the constructor

readNames(String[] commonname, scienname)

You need to define a type for scienname.

i.e.

readNames(String[] commonname, String[] scienname)

The second error, mentioning a missing ;, is being cause by the compile error mentioned here.

Fix both, and it is my assumption that both errors will go away.

edit

Pasted this into my IDE, and found more issues

String list = new String( common, scient );
                  /\ the constructor for string takes many things, two String arrays is not one of them.

Available String constructors here.

And

This method must return a result of type String

Your method never returns a value, however its defined return type is that of String.

After adding the missing String[] in the constructor, I am no longer seeing the issue posted in the question.

edit edit

I do not have the time to debug you application, and you have some call that I am unsure of your intentions. Here is your code, with some changes, that will compile. You can touch it up from here.

public class JTest
{

    static String[] commonname = new String[25];

    static String[] scienname = new String[25];

    public static void main( String[] args )
        throws IOException
    {

        // make a call to the rean names method with parameters
        String list = readNames( commonname, scienname );

        // not sure what you intend to sort, you can not sort a string
        // 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();

                // not sure what your intention is here
                //String list = new String( common, scient );
            }
        }
        finally
        {
            if ( inputStream != null )
            {
                inputStream.close();
            }
        }

        // return a String, as specified in method definition
        return new String("with some content");
    }
}

11 Comments

Changed the above to code "public static String readNames(String[] commonname, String[] scienname) throws IOException { " code
Changed the above to 'public static String readNames(String[] commonname, String[] scienname) throws IOException { ' but I still get the same two errors.
Sorry I can't edit comments yet and I'm new to how this site formats. I hope you get what I mean
Oh? Give me a minute, spinning up my IDE to test. code goes between two `ticks` in a comment.
See my edit. It appears as if the constructor fix was causing those two issues you posted, but you now have different issues that need to be fixed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.