Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8209727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:49:38+00:00 2026-06-07T09:49:38+00:00

This program sorts the first n words from a file using insertion sort .

  • 0

This program sorts the first n words from a file using insertion sort.

This is not made by me. We were asked to use this program our teacher provided to implement other sorting techniques. I imported the source code and when I run it. It says:

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
at SortingAnalysis.main(SortingAnalysis.java:26)

But when our teacher demonstrated it in our class, there is no error in it.

I’m also wondering how it sorts the words from a file without even stating the file name (e.g. tobesorted.txt). Maybe as long as it’s within the JRE System Library, it will work, won’t it?

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

/**
 * Compares the running times of sorting algorithms
 * @author bryann
 *
 */
public class SortingAnalysis {

    public static void insertionSort(String[] a) {
        int n = a.length;
        for(int i = 1; i < n; i++) {
            String cur = a[i];
            int j = i - 1;
            while((j >= 0) && (a[j].compareTo(cur) > 0)) {
                a[j + 1] = a[j--];
            } // end while
            a[j + 1] = cur;
        } // end for
    } // end insertionSort

    public static void main(String[] args) {
        final int NO_OF_WORDS = 5000;
        try {
            Scanner file = new Scanner(new File(args[0]));
            String[] words = new String[NO_OF_WORDS];

            int i = 0;
            while(file.hasNext() && i < NO_OF_WORDS) {
                words[i] = file.next();
                i++;
            } // end while
            long start = System.currentTimeMillis();
            insertionSort(words);
            long end = System.currentTimeMillis();
            System.out.println("Sorted Words: ");
            for(int j = 0; j < words.length; j++) {
                System.out.println(words[j]);
            } // end for        
            System.out.print("Running time of insertion sort: " + (end - start) + "ms");

        } // end try
        catch(SecurityException securityException) {
            System.err.println("You do not have proper privilege to access the files.");
            System.exit(1);
        } // end catch
        catch(FileNotFoundException fileNotFoundException) {
            System.err.println("Error accessing file");
            System.exit(1);
        } // end catch
    } // end main
} // end class SortingAnalysis

Is the error because of import? Using Eclipse, I just clicked
File > Import > General > File System > From directory (the whole folder he sent to us) > Into folder (I created a new project and there is where I “imported” the code) > Finish

Please help me. I can’t start with the assignment (that is, trying other sorting techniques in the same source file) because I can’t run it. Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:49:40+00:00Added an answer on June 7, 2026 at 9:49 am

    The main function of a program takes arguments :

    public static void main(String[] args) {
    

    Those arguments are the ones passed to the program on the command line :

    java SortingAnalysis /home/somebody/tobesorted.txt
    

    In this case, args[0] would be "/home/somebody/tobesorted.txt"

    This enables the program to know what file to open :

    Scanner file = new Scanner(new File(args[0]));
    

    But when you launch your program without providing the path to a file, args is too short and you have this java.lang.ArrayIndexOutOfBoundsException: 0 as args[0] doesn't exist you got.

    So give the path to the file to sort to remove this error. For example :

    java SortingAnalysis C:\somepath\tobesorted.txt
    

    EDIT:

    If you want to hardcode the path, you may do this :

    Scanner file = new Scanner(new File("C:\\somepath\\tobesorted.txt"));
    

    (note the double \\).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I'm looking at this program that reads input lines and then sorts them, from
This program I use has it's own variables to set when you run it,
This program reads emails (really just a .txt file structured like an email) and
Basically the purpose of this program is to read up to 100 names from
So I've built this mail-program-like HTML viewer (no real emails, just some messages from
this program hangs after taking first argument:- #include <stdio.h> #include <conio.h> void ellip(char*,...); int
This is my first post on here.. I'm writing a program in MVC3 that
This program builds a dictionary out of a list based on what two index
This program returns: package main import ( flag fmt ) func main() { num_agents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.