0

I just finished writing a code that implements two stacks in one array, and my teacher wants us to print the source code along with the output and hand it in on paper. I was having a hard time just sending the console to a pdf so I said what the hell i'll also start learning how to write to a file with java. Right now I have almost all of what I need written to a file, but when I try to use any of the three display methods I wrote, it pulls an error, I understand that the display methods use the System.out.print but I keep changing things trying to make it work and I can't figure it out. Anyway here's exactly where I'm running into trouble

   try {
        PrintStream myconsole = new PrintStream(new File("C:\\Users\\Ian McMahon\\Desktop\\CCSU\\CCSU SPRING 18\\CS 253\\cs253project2output.txt"));
            System.setOut(myconsole);


            //Right here if I try to do
            //myconsole.print(tsa.display()); it won't write it to the txt file and just throws an error


            myconsole.println("Now Popping red: \n");

            myconsole.println("Red pop is " + tsa.popRed());
            myconsole.println("Red pop is " + tsa.popRed());
            myconsole.println("Red pop is " + tsa.popRed());
            myconsole.println("Red pop is " + tsa.popRed());
            myconsole.println("Red pop is " + tsa.popRed());

            myconsole.println("\nNow Popping Blue: \n");

            myconsole.println("Blue pop is " + tsa.popBlue());
            myconsole.println("Blue pop is " + tsa.popBlue());
            myconsole.println("Blue pop is " + tsa.popBlue());
            myconsole.println("Blue pop is " + tsa.popBlue());
            myconsole.println("Blue pop is " + tsa.popBlue());
        }
        catch(FileNotFoundException fx){
            System.out.println(fx);
        }

and this is the rest of my code.

import java.io.File;
import java.io.PrintStream;
import java.io.FileNotFoundException;


public class twoStacks
{
    int size;
    int top1, top2;
    int arr[];

    twoStacks(int n){
        arr = new int[n];
        size = n;
        top1 = -1;
        top2 = size;
    }

    void pushRed(int x){
        if (top1<top2-1){

            top1++;
            arr[top1] = x;
        }else{
            System.out.println("Stack full");
        }
    }

    void pushBlue(int x){
        if (top1 < top2 -1){
            top2--;
            arr[top2] = x;
        }else{
            System.out.println("Stack full");
        }
    }

    int popRed(){
        if (top1 >= 0){
            int x = arr[top1];
            top1--;
            return x;
        }else{
            System.out.println("Stack Empty");
        }
        return 0;
    }

    int popBlue(){
        if (top2 < size){
            int x = arr[top2];
            top2++;
            return x;
        }else{
            System.out.println("Stack Empty");
        }
        return 0;
    }

    public void displayRed(){
        System.out.print("The red stack is: \n");
        for(int i = 0; i < arr.length/2; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.print("\n\n");
    }

    public void displayBlue(){
        System.out.print("The blue stack is: \n");
        for(int i = 5; i < arr.length; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.print("\n\n");
    }

    public void display(){
        System.out.print("The two Stacks together are: \n");
        for(int i = 0; i < arr.length; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.print("\n\n");
    }



    public static void main(String args[]){
        twoStacks tsa = new twoStacks(10);

        tsa.pushRed(1);
        tsa.pushRed(2);
        tsa.pushRed(3);
        tsa.pushRed(4);
        tsa.pushRed(5);
        tsa.pushBlue(6);
        tsa.pushBlue(7);
        tsa.pushBlue(8);
        tsa.pushBlue(9);
        tsa.pushBlue(10);

        tsa.displayRed();
        tsa.displayBlue();
        tsa.display();

        System.out.println("Now Popping red: \n");

//        System.out.println("Red pop is " + tsa.popRed());
//        System.out.println("Red pop is " + tsa.popRed());
//        System.out.println("Red pop is " + tsa.popRed());
//        System.out.println("Red pop is " + tsa.popRed());
//        System.out.println("Red pop is " + tsa.popRed());
//
//        System.out.println("\nNow Popping Blue: \n");
//
//        System.out.println("Blue pop is " + tsa.popBlue());
//        System.out.println("Blue pop is " + tsa.popBlue());
//        System.out.println("Blue pop is " + tsa.popBlue());
//        System.out.println("Blue pop is " + tsa.popBlue());
//        System.out.println("Blue pop is " + tsa.popBlue());

        //after testing code with console, now i'm writing it to a file
        try {
        PrintStream myconsole = new PrintStream(new File("C:\\Users\\Ian McMahon\\Desktop\\CCSU\\CCSU SPRING 18\\CS 253\\cs253project2output.txt"));
            System.setOut(myconsole);


            //Right here if I try to do
            //myconsole.print(tsa.display()); it won't write it to the txt file and just throws an error


            myconsole.println("Now Popping red: \n");

            myconsole.println("Red pop is " + tsa.popRed());
            myconsole.println("Red pop is " + tsa.popRed());
            myconsole.println("Red pop is " + tsa.popRed());
            myconsole.println("Red pop is " + tsa.popRed());
            myconsole.println("Red pop is " + tsa.popRed());

            myconsole.println("\nNow Popping Blue: \n");

            myconsole.println("Blue pop is " + tsa.popBlue());
            myconsole.println("Blue pop is " + tsa.popBlue());
            myconsole.println("Blue pop is " + tsa.popBlue());
            myconsole.println("Blue pop is " + tsa.popBlue());
            myconsole.println("Blue pop is " + tsa.popBlue());
        }
        catch(FileNotFoundException fx){
            System.out.println(fx);
        }

    }
}

Thanks guys!

  • First - what is your error? Out of curiosity, if you don't *need* to write this to a file (programmatically), why not just pipe your output to a file? In Windows and Linux it should be the same, you can just redirect your console output to a file by something like `java myprogram > outputfile.txt` - this would be the absolute equivalent of programmatically redirecting standard output to a file *without* the hassle of writing the code to do so. – dddJewelsbbb Feb 24 '18 at 21:50
  • [writing to a file in java](https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java) – Random Guy Feb 24 '18 at 21:52
  • lol i did not know that, so where would I write that? and would I replace the outputfile.txt to what I have written in mine? like this java myprogram > "C:\\Users\\Ian McMahon\\Desktop\\CCSU\\CCSU SPRING 18\\CS 253\\cs253project2output.txt" – alwaysquestioning Feb 24 '18 at 21:53
  • @dddJewelsbbb the error it's throwing is saying that void type is not allowed there – alwaysquestioning Feb 24 '18 at 22:08
  • You can do it like that, but being you're running it from the command line, you may want to avoid the double backslash. Not sure if it would interfere, but it's not necessary. But yeah, just like that. If you're in the directory in the command prompt (or powershell) just running it like `java myprogram > outputfile.txt` will make an output file in that directory with the given name. As for your error, can you provide more information about the error? What line it occurs on, etc? – dddJewelsbbb Feb 24 '18 at 22:21
  • @dddJewelsbbb I actually just figured it out, I didn't realize(since I'm new to writing filles with java) that in the try catch clause, when you write System.setOut() whatever you write in that try statement will be written to a file, it actually makes much more sense now that I figured it out thanks for the help tho man! – alwaysquestioning Feb 24 '18 at 22:26
  • Glad to help!:) – dddJewelsbbb Feb 24 '18 at 22:38

2 Answers2

0

I figured it out all out have to do is just type in the tsa.display() methods as if you're typing them normally, right under System.setOut(myconsole) and it writes it right to the txt file

0

I tested your all your code. And I can't understand which is your problem. if your problem is with your display function: I can say that it works fine. if your problem is with your txt document. I can say that it works fine too.

already I have the document with all the outputs.

  • yeah it all works fine, my problem was using System.out.print(tsa.display()) but I just learned i shouldn't have put the System.out.print before and all I needed was tsa.display(); – alwaysquestioning Feb 24 '18 at 22:40
  • yes, because inside your function display you already have the System.out.print... – Angel Astorga Feb 24 '18 at 23:21