I have created a Deque in java and want to print out a string of integers, line
ID: 3871677 • Letter: I
Question
I have created a Deque in java and want to print out a string of integers, line by line, and from a text file. I tried setting my field "number" to an integer but it printed each number out in a singular row instead of line by line. I think I can get it to print line by line if I can convert the string into an integer, so where and how do I convert it to an integer in my code below? Also, if you know a way to set the field "number" as an integer, and print line by line without having to convert it then please show me how I would go about that. Your help is greatly appreciated.
Deque my = new LinkedList(); File inputFile = new File ("file.txt"); Scanner insertion - new Scanner (inputFile) String number; while (insertion.hasNext()) number = insertion . nextLine(); my.offerFirst (number)Explanation / Answer
please findthe java code
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class DequeDemo {
public static void main(String args[]) throws FileNotFoundException {
Deque<Object> my = new LinkedList();
File inputFile = new File("File.txt");
Scanner insertion = new Scanner(inputFile);
int number;
while(insertion.hasNext()) {
number = Integer.parseInt(insertion.nextLine());
my.offerFirst(number);
}
System.out.println("Content of deque is ");
Iterator<Object> itr = my.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}