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