Question
Asked Feb 7, 2019
9 views

How do I read a sequence of values in a file into a arraylist in a method? It's in java progarmming.

check_circle

Expert Answer

Step 1

We need to use Scanner calss. In which java.util package used for obtaining the input of the primitive types like int, double etc. and strings. Then we must pass object of the class File since we are going to read from the file. Scanner class provides 'readLine()' method to read an entire line and 'hasNext()' method to indicate whether more values are available for reading or not.

Step 2

We will first initiallize File object.

Scanner s = new Scanner(new File(filename))

Step 3

So the code for the method will be.

ArrayList<String> result = new ArrayList<>();
 
try (Scanner s = new Scanner(new FileReader(filename))) {
    while (s.hasNext()) {
        result.add(s.nextLine());
    }
    return result;
}
...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Assume the following declarations and initializations (MIN, MAX, num) are made just before each sub-...

A: Since these are separate blocks of code, for the purpose of demonstration we put the blocks in same ...

question_answer

Q: SQL(Oracle): How do I create an index for the number of days it takes to ship an order? i.e. shipdat...

A: Since we need to create an index based on a calculated field we need to create a calculated index. T...

question_answer

Q: Write a program called LoopMinMax that reads in 4 values from the user and prints the lowest and hig...

A: You didn’t mention which language should be used for the given program. Here, I have used JAVA langu...

question_answer

Q: The figure below shows an ER schema for a university database. Map this ER schema into a relational ...

A: Since the question refers to a mapping algorithm which is not described in the question we are going...

question_answer

Q: Construct npda that accept the following regular language: L1 = L (aaa∗bab).

A: Non-deterministic pushdown automaton (NPDA):Given: aaa*bab To construct NPDA, when “a” comes then pu...

question_answer

Q: Design a Turing machine that computes the function f(w) = 1 if w is even   = 0 if w ...

A: To discover whether a string is even or odd, just examine the LSB (least significant bit).If LSB is ...

question_answer

Q: How would I open a file and encrypt that file from a dictionary in Python? Attached is a picture of ...

A: The functions are implemented in python 3. Since the python dictionary given in convert function is ...

question_answer

Q: Rainfall StatisticsWrite a program that lets the user enter the total rainfall for each of 12 months...

A: The following program is in C++ language and it is compiled in the “repl.it” compiler.Program plan:I...

question_answer

Q: how is macro work in word 2016

A: At first we need to enable the developer Tab if it's not enabled previously.Next, we need to click t...