试着用文件写,但是用display()方法很难

时间:2018-02-24 21:36:31

标签: java arrays writetofile

我刚刚编写了一个代码,在一个数组中实现了两个堆栈,我的老师希望我们将源代码和输出一起打印出来并将其交给纸上。我很难将控制台发送到pdf,所以我说我还会开始学习如何使用java写入文件。现在我几乎把所有需要的东西写到文件中,但是当我尝试使用我编写的三种显示方法中的任何一种时,都会出错,我明白显示方法使用System.out.print但是我不断变化的东西试图让它工作,我无法弄清楚。无论如何,这正是我遇到麻烦的地方

   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);
        }

这是我的其余代码。

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);
        }

    }
}

谢谢你们!

1 个答案:

答案 0 :(得分:0)

我认为只需要输入tsa.display()方法,就像你正常输入它们一样,就在System.setOut(myconsole)下,它将它直接写入txt文件< / p>