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!