Given the code below add a readlist method to it (you can just write the new met
ID: 3571973 • Letter: G
Question
Given the code below add a readlist method to it (you can just write the new method in the answer without copying the full class). This method should read int values from a file, print each value, and append them to the end of the ArrayList. You should catch all appropriate errors. import java.io.*; import java.util.List; import java.util.ArrayList; public class ListOfNumbers {private List list; private static final int SIZE = 100; public ListOfNumbers () {list = new ArrayList(SIZE); for (int i: list) list.add(new Integer(i));} public void writeLis() {PrintWriter out = null; try {System.out.println("Entering try statement"); out = new PrintWriter(new FileWriter C"OutFile")); for (int i = 0; iExplanation / Answer
Explanation :-
1)Create a file with name "inFile.txt" in you project folder
2) Add Integers to it , One Integer in one line
3) call the method from you Programme "readList"
Programme :-
------------------------------
public void readList(){
try {
Scanner scanner = new Scanner(new File("inFile.txt"));
while(scanner.hasNextInt())
{
list.add(new Integer(scanner.nextInt())) ;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.