0

I am writing a program that takes strings from .txt file and sorts them but I am getting this error and have no idea how to fix. Please help!

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 300 at StringSorter.main(StringSorter.java:37)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.io.PrintWriter;

public class StringSorter {

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

        //read line
        File strdata = new File("strings.txt");

        try (Scanner test = new Scanner(strdata)) {
            //
            int x = test.nextInt();
            //create intarray
            String[] mystr = new String[x];

            //sets counbto to 0 for the next loop
            int count = 0;

   //loop to transfer .txt to array         
  while (test.hasNextLine() && count <= x ) {

     String i = test.nextLine();
     mystr[count] = i;
     count++;
  }
 if (test.hasNextLine() && count >= x ) {
      System.out.println("array is full");
  }

            //sorts array 
            mystr = sortStrings(mystr);
            //output
            FileWriter fw = new FileWriter("strings.txt");
            PrintWriter pw = new PrintWriter(fw);

            //prints size
            pw.println("The Size of string array " + mystr.length);
            System.out.println("size" + mystr.length );
            //info text
            pw.println("Sorted numbers from least to greatest:");

            //sorted numbers, one per line
            for (int counter = 0; counter < mystr.length; counter++) {
                String names = mystr[counter];

                pw.println(names);
System.out.println("sorted names aplabetical" + names );
            }
           
            int target;
           
            
if (count % 2 == 0) {

pw.println(count + "is even");   
//this is what I would do if there isnt a typo on requirment 3  count = count - 2;
target = count / 2;

pw.println(count + "is even");
}
else{
    count++;
    target = count /2;
}
        

           
pw.close();
            test.close();

        }

    }

    public static String[] sortStrings(String[] mystr) {

        Arrays.sort(mystr);
        return (mystr);
    }
}

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.io.PrintWriter;

public class StringSorter {

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

        //read line
        File strdata = new File("strings.txt");

        try (Scanner test = new Scanner(strdata)) {
            //
            int x = test.nextInt();
            //create intarray
            String[] mystr = new String[x];

            //sets counbto to 0 for the next loop
            int count = 0;

   //loop to transfer .txt to array         
  while (test.hasNextLine() && count <= x ) {

     String i = test.nextLine();
     mystr[count] = i;
     count++;
  }
 if (test.hasNextLine() && count >= x ) {
      System.out.println("array is full");
  }

            //sorts array 
            mystr = sortStrings(mystr);
            //output
            FileWriter fw = new FileWriter("strings.txt");
            PrintWriter pw = new PrintWriter(fw);

            //prints size
            pw.println("The Size of string array " + mystr.length);
            System.out.println("size" + mystr.length );
            //info text
            pw.println("Sorted numbers from least to greatest:");

            //sorted numbers, one per line
            for (int counter = 0; counter < mystr.length; counter++) {
                String names = mystr[counter];

                pw.println(names);
System.out.println("sorted names aplabetical" + names );
            }
           
            int target;
           
            
if (count % 2 == 0) {

pw.println(count + "is even");   
//this is what I would do if there isnt a typo on requirment 3  count = count - 2;
target = count / 2;

pw.println(count + "is even");
}
else{
    count++;
    target = count /2;
}
        

           
pw.close();
            test.close();

        }

    }

    public static String[] sortStrings(String[] mystr) {

        Arrays.sort(mystr);
        return (mystr);
    }
}
bobjones
  • 1
  • 2
  • You're trying to add more items to the array than you allocated when you made it. You create the array with size "x", but appear to be putting more than x strings in it. You should also test the size of count while looping and exit if it goes too far. – TDWebDev Nov 16 '16 at 04:11
  • See http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Henry Nov 16 '16 at 06:12
  • try using a debugger and examine what the values of `x` and `count` are. – Scary Wombat Nov 16 '16 at 06:52
  • debugger console gives me this: " Listening on javadebug User program running Debugger stopped on uncompilable source code." User program finished – bobjones Nov 16 '16 at 06:53
  • the problem was you should get rid of >= it should just be > THank you for the help – bobjones Nov 16 '16 at 07:09

0 Answers0