Jake Owen Jake Owen - 1 year ago 185
Java Question

Java; Txt file of single integer on each line, how can I single out each integer to use in my program?

I have a txt file, each line has a single integer. I need to make a class that singles out each one, so I can then put them in an array in a different class.

I understand that it may be weird to want each single integer but I need it this way for my program.

Any help would be greatly appreciated.

Answer Source

Have a look at the Scanner class: https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

More specifically, you can use Scanner.nextInt to read into whatever data structure you want.

Example reading from system input (I will leave the exercise to you to figure out how to read from a text file):

    Scanner sc = new Scanner(System.in);
    List<Integer> numberList = new ArrayList<>();
    while (sc.hasNextInt()) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download