我正在编写一个程序,它从.txt文件中获取字符串并对它们进行排序但是我收到此错误并且不知道如何修复。请帮忙!
线程中的异常" main" java.lang.ArrayIndexOutOfBoundsException:300 在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);
}
}
&#13;
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);
}
}
&#13;