1

I am trying to read a file, which the user inputs, and the file has numbers and characters. I only want to store the numbers in an Arraylist but I keep getting stuck, help would be greatly appreciated. This is what I have. Sorry if this has been answered, I am new to the site.

import java.util.*;
import java.io.*;

public class ArrayListClient {
  public static final int SIZE = 100;

  public static void main(String[] args) throws FileNotFoundException {
    String fileName, fileName2;

    UnorderedArrayList list1 = new UnorderedArrayList(SIZE);

    Scanner input = new Scanner(System.in);
    System.out.print("Please input the name of the file to be opened for the first list: ");
    fileName = input.nextLine();

    System.out.println();
    Scanner inputFile = new Scanner(new File(fileName));
    int num = inputFile.nextInt();

    while(inputFile.hasNextInt()) {
      int num2 = inputFile.nextInt();
      list1.insertEnd(num);
      num = num2;
    }
    list1.print();

 }         
}

the input file is 13 c v b 25 34 x x 67 56 10 a a 20 27 2 a s 5 1 45 59

0

2 Answers 2

4

The loop you provided is correct, although you don't need this line outside of the while loop:

int num = inputFile.nextInt();

If the file you provided didn't have a Integer then this would crash your program.

I think you can simply write this and it should work:

while (inputFile.hasNextInt()) {
    int num = inputFile.nextInt();
    list1.insertEnd(num);
}

The loop checks to see if there is another Integer left in the file (inputFile.hasNextInt()) and then gets it to add to the list (inputFile.nextInt()).

Sign up to request clarification or add additional context in comments.

1 Comment

This was posted as I was writing my comment below. It's basically an illustration of what I was trying to point you towards - use the Scanner class to ignore the non-numbers, and then this example doesn't use parseInt because I think nextInt parses the number to an int for you, but if not, know that the Integer class is there for you.
1

This sounds like it could be a homework question, so I'm hesitant to just give the answer, but if I were you, I would consider writing a filter function (make it a lazy filter if you have to consider files that are very large/won't fit in memory). Your filter function can try Integer.parseInt(yourString); and catch any NumberFormatExceptions that occur because it tried to parse a letter. This approach has the obvious danger of using exceptions to control program flow (normally considered bad practice), but you won't have to traverse the list twice.

Your other obvious option is to write a filter that filters the characters out so that you are only left with number strings, and then just run parseInt over those number strings to turn them into integer values. If performance is a concern, you can avoid double-traversing the list by writing functions that validate a single string value (reject if it's not a number), and then parse it into an int if it is a number, and then add the parsed integers into your array as you go within a foreach loop.

You are most of the way there already since integer detection is built into the Scanner class and the Integer class contains the parseInt() method. Just mutate an array which you define outside of a for each loop and you're good to go.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.