Put a txt in a Java Stack

Hello Guys,

I have this assgnement below and i have no idea how to do it i tried to create an array and after that insert ina a stack but did not work. Im starting with programing be patient with me.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

class stacksfun {
private static Object File;

stacksfun() {

public static void main(String[] args) throws FileNotFoundException {
    File file;
    File = file = new File("c:\\temp\\HW2.txt");
    Scanner scan = new Scanner(file);

    for(String filecontent = "."; scan.hasNextInt(); filecontent = filecontent.concat(scan.nextLine())) {



Write a Java program to read the HW2.txt file line by line and do the following:

  1. Store each line in a stack. ( Stack (Java Platform SE 7 ))

  2. loop through the stack and print each line as you pop it off

  3. Store each line in a queue. ( Queue (Java Platform SE 7 ))

  4. Loop through the queue and print each line as you remove the elements


It seems like this is a homework question for your class, so we can’t really answer this for you in this forum.

However the good news is that youtube and google have lots of resources that explain what stacks and queues are conceptually (don’t think about the code yet).

You should have a clear idea of what the concepts aim to do before you can write the code for it.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.