0

This program is supposed to be able to sort by either firstname lastname ect, but when I run the program in cmd as ( java Sorting_2 < patients.txt ) i get there error ( Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Sorting_2.main(Sorting_2.java:10) ) What im I doing wrong?

import java.io.*;
import java.util.*;

public class Sorting_2
{
public static void main(String[] args)
{
    try
    {
        String fileName = args[0];
        ArrayList<Sorting> patient = new ArrayList<Sorting>();
        Scanner scan = new Scanner ( new File (fileName) );

        while (scan.hasNext())
        {
            String line = scan.nextLine();
            String[] tokens = line.split(",");

            String lastName = tokens[0];
            String firstName = tokens[1];
            int age = Integer.parseInt(tokens[2]);
            String insuranceCompany = tokens[3];
            int roomNum = Integer.parseInt(tokens[4]);

            Sorting sorting = new Sorting(lastName, firstName, insuranceCompany, age, roomNum);
            patient.add(sorting);
        }
        String comparisonType = args [1];
        if (comparisonType.equals("lastName"))
        {
            Collections.sort(patient, new lastNameComarator());
        }
        else if (comparisonType.equals("firstName"))
        {
            Collections.sort(patient, new firstNameComparator());
        }
        else if (comparisonType.equals("age"))
        {
            Collections.sort(patient,new ageComparator());
        }
        else if (comparisonType.equals("insuranceCompany"))
        {
            Collections.sort(patient, new insuranceCompanyComparator());
        }
        else if (comparisonType.equals("roomNum"))
        {
            Collections.sort(patient, new roomNumComparator());
        }
        System.out.println();
        System.out.printf("%-13s %-17s %-9s %-10s %9s%n", "lastName", "firstName", "age", "insuranceCompany", "roomNum");

        for(Sorting c : patient)
        {
            System.out.println(c);
        }
    }
    catch (FileNotFoundException ex)
    {
        ex.printStackTrace();
    }
}
}

other program

import java.io.*;
import java.util.*;

public class Sorting
{
public String lastName;
public String firstName;
public int age;
public String insuranceCompany;
public int roomNum;


public Sorting(String l, String f, String i, int a, int r)
    {
        lastName = l;
        firstName = f;
        age = a;
        insuranceCompany = i;
        roomNum = r;

    } 


}

class lastNameComarator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{

    return a.lastName.compareToIgnoreCase(a.lastName);
}
}
class firstNameComparator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{
    return a.firstName.compareToIgnoreCase(b.firstName);
}
}
class insuranceCompanyComparator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{
    return a.insuranceCompany.compareToIgnoreCase(b.insuranceCompany);
}
}
class ageComparator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{
    if (a.age < b.age) return -1;
    else if (b.age < a.age) return 1; 
    else return 0;
}
}
class roomNumComparator implements Comparator<Sorting>
{
public int compare(Sorting a, Sorting b)
{
    if (a.roomNum < b.roomNum) return -1;
    else if (b.roomNum < a.roomNum) return 1;
    else return 0;
}
}
Samuel Fipps
  • 193
  • 1
  • 13
  • You're calling the program by directing the contents of the file to STDIN, rather than passing the filename. So when the program does 'args[0]', there are no arguments and it dies. – zanerock Mar 27 '18 at 01:56

2 Answers2

1

The main method takes an argument called args. This should be defined in the run configuration of your program. Args is an array of strings. You're calling the first value in the array, args[0]. This requires an array of a length with at least one value, args.length() equals 1.

So either you must define args. See this for more information: What is "String args[]"? parameter in main method Java, https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html.

Or replace args[0] with the path to the files, for example, "relative/path/to/filename"

  • Figured it out the code was right, I just need the arg that i was sorting by which was kinda what you said which was to define the arg as ( java Sorting_2 test.txt lastName ) – Samuel Fipps Mar 27 '18 at 01:40
  • @SamuelFipps glad I could help. If this answer solved your problem you can confirm it beneath the vote counter left from the answer :) – Stan Van Der Bend Mar 27 '18 at 02:56
0

I just forgot the arg ( java Sorting_2 test.txt lastName ) or ( java Sorting_2 test.txt firstName ) ect.

Samuel Fipps
  • 193
  • 1
  • 13