1

I have asked a question like this before but am now getting a new error. I am trying to take a file of ages and names (age, name) that the user put in through the console and sort them in ascending order. I keep running into problems and getting errors when I am am running it.
The file looks like this: (56, Suresh) (89, Mahesh) (81, Shyam) (92, Vikas) (84, Shloka) (62, Nalini) (71, Ahbi)

Scanner inputFile = new Scanner(System.in);
System.out.println("File to read from");
File file = new File (inputFile.nextLine());
inputFile = new Scanner(file);
ArrayList<ponySort> nameAge = new ArrayList<ponySort>();
String currentLine = inputFile.nextLine();
while (currentLine != null) {
    String[] parts = currentLine.split("(?<=\\)) (?=\\()");
    for (String part : parts) {
        String input = part.replaceAll("[()]", "");
        int age = Integer.parseInt(input.split(", ")[0]);
        String name = input.split(", ")[1];
        nameAge.add(new ponySort(name, age));
        currentLine = inputFile.nextLine();
    }
}

This is the error message that I am getting when I am running my code:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at sortFile.main(sortFile.java:26)

Now I thought maybe if I changed the currentLine = inputFile.nextline() (at the bottom of my while) to - currentLine = inputfile.next(), that it might do something but all that happened was that I got another error. This time it said:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Nalini"
at 
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at sortFile.main(sortFile.java:23)

If anyone could help me so that it just reads through the file correctly that would be great. I have a toString and everything else in order so that it prints out the ages and names in ascending order.

omer muhit
  • 65
  • 7
max
  • 13
  • 3

0 Answers0