0
String currentLine = reader.readLine();
while (currentLine != null)
{
  String[] studentDetail = currentLine.split("");

  String name = studentDetail[0];

  int number = Integer.valueOf(studentDetail[1]);
  currentLine = reader.readLine();
}

So I have a file like this:

   student1
   student16
   student6
   student9
   student10
   student15

When I run the program said: ArrayIndexOutOfBoundsException:1

the output should look like this:

   student1
   student6
   student9
   student10
   student11
   student15
   student16
Ossin Java guy
  • 365
  • 3
  • 12
Camila
  • 1
  • 3
  • You need to post more of your code. – Elliott Frisch Jun 25 '17 at 05:44
  • You can debug to find what your variable name contains, because IndexOutOfBounds indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array. – Fady Saad Jun 25 '17 at 05:46
  • what you need to know? – Camila Jun 25 '17 at 05:46
  • How do you get `reader`, where is your attempt at sorting... do you just want to know how to get `number`? – Elliott Frisch Jun 25 '17 at 05:47
  • BufferedReader reader = new BufferedReader(new FileReader(new File(infile))); ArrayList student= new ArrayList(); – Camila Jun 25 '17 at 05:50
  • Edit your question. Include `Student` as well. – Elliott Frisch Jun 25 '17 at 05:51
  • Ok but that is not the problem, the problem is how i can separate the String and the number for each line in the file !!!!! – Camila Jun 25 '17 at 05:52
  • `split()` is the wrong tool for the job. `split` is best suited for when you have a number of fields separated by a common delimiter or delimiter pattern (like spaces or commas). But you don't have a separator in your input. Elliott's answer shows the best way to handle this. I think there are ways to do this with `split()`, but they're a little more complicated and there's no advantage to using them. – ajb Jun 25 '17 at 17:45

3 Answers3

0

Assuming all the lines start with student and end with a number, you can read all the lines and add them to a list and then sort the list by the number after student, then print each element. For example:

String currentLine;
List<String> test = new ArrayList<String>();
while ((currentLine = reader.readLine()) != null)
    test.add(currentLine());
test.stream()
    .sorted((s1, s2) -> Integer.parseInt(s1.substring(7)) - Integer.parseInt(s2.substring(7)))
    .forEach(System.out::println);

output:

student1
student6
student8
student9

If you don't want to use stream() and lambda, You can sort the list with a custom Comparator then loop through the list and print each element:

Collections.sort(test, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        int n1 = Integer.parseInt(s1.substring(7));
        int n2 = Integer.parseInt(s2.substring(7));
        return n1-n2;
    }
});
Mohd
  • 5,523
  • 7
  • 19
  • 30
0

First, program to the List interface instead of the ArrayList concrete type. Second, use a try-with-resources (or explicitly close your reader in a finally block). Third, I would use a Pattern (a regex) and then a Matcher in the loop to look for "name" and "number". That might look something like,

List<Student> student = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(new File(infile)))) {
    Pattern p = Pattern.compile("(\\D+)(\\d+)");
    String currentLine;
    while ((currentLine = reader.readLine()) != null) {
        Matcher m = p.matcher(currentLine);
        if (m.matches()) {
            // Assuming `Student` has a `String`, `int` constructor
            student.add(new Student(m.group(1), Integer.parseInt(m.group(2))));
        }
    }
} catch (FileNotFoundException fnfe) {
    fnfe.printStackTrace();
}

Finally, note that Integer.valueOf(String) returns an Integer (which you then unbox). That is why I have used Integer.parseInt(String) here.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
-1

Your file must be like this

student 1
student 2
student 3

don't forget to add space character between student and number. And inside your iteration you must add this line: currentLine = reader.readLine(); you can split like this: String[] directoryDetail = currentLine.split(" "); instead of String[] directoryDetail = currentLine.split(""); because when you use String[] directoryDetail = currentLine.split(""); with student1 the result is a string array of String with length = 0

Kenany
  • 446
  • 3
  • 11
  • 1
    Generally, you don't solve programming problems by changing the nature of the input. That's beyond your control. The problem is usually "how do I deal with the input I'm given"? – ajb Jun 25 '17 at 17:41