#ArrayList a redundancy? (code comparison)

3 messages · Page 1 of 1 (latest)

dusky scarab
#

My code below (passes the server test), model code in image. Some questions that come to mind would be, "Is my code a better way to store information as the program executes for various applications?"; "If this was iterated in, say, doubles, and an extremely high number of times, would the model code be optimal for managing computer resources?". Just a newbie trying to grasp a deeper concept of the "ambiguities" of coding 😄

import java.util.Scanner;
import java.util.ArrayList;

public class NumbersFromAFile {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("File? ");
        String file = scanner.nextLine();
        System.out.print("Lower bound? ");
        int lowerBound = Integer.valueOf(scanner.nextLine());
        System.out.print("Upper bound? ");
        int upperBound = Integer.valueOf(scanner.nextLine());
        
        ArrayList<Integer> list = new ArrayList<>();
        
        try(Scanner fileReader = new Scanner(Paths.get(file))){
            while(fileReader.hasNextLine()){
                list.add(Integer.valueOf(fileReader.nextLine()));
            }
        }catch(Exception e){
            System.out.println("Reading the file " + file + " failed.");
        }
        int numbers = 0;
        for(int i = 0; i < list.size(); i++){
            if(list.get(i) >= lowerBound && list.get(i) <= upperBound){
                numbers++;
            }
        }
        System.out.println("Numbers: " + numbers);
    }
}```
scenic currentBOT
#

This post has been reserved for your question.

Hey @dusky scarab! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.