【发布时间】:2023-04-04 09:51:01
【问题描述】:
我刚写完一个在一个数组中实现两个堆栈的代码,我的老师希望我们将源代码连同输出一起打印出来并在纸上交出。我很难将控制台发送到 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);
}
}
}
谢谢大家!
【问题讨论】:
-
首先 - 你的错误是什么?出于好奇,如果您不需要需要将其写入文件(以编程方式),为什么不直接将输出通过管道传输到文件中呢?在 Windows 和 Linux 中它应该是相同的,您可以通过
java myprogram > outputfile.txt之类的方式将控制台输出重定向到一个文件 - 这绝对等同于以编程方式将标准输出重定向到一个文件没有编写代码的麻烦。 -
大声笑我不知道,那我在哪里写呢?我会将 outputfile.txt 替换为我写的内容吗?像这样 java myprogram > "C:\\Users\\Ian McMahon\\Desktop\\CCSU\\CCSU SPRING 18\\CS 253\\cs253project2output.txt"
-
@dddJewelsbbb 它抛出的错误是说那里不允许使用 void 类型
-
您可以这样做,但由于您是从命令行运行它,您可能希望避免使用双反斜杠。不确定它是否会干扰,但没有必要。但是,是的,就这样。如果您在命令提示符(或 powershell)的目录中,只需像
java myprogram > outputfile.txt一样运行它,就会在该目录中使用给定名称生成一个输出文件。至于您的错误,您能否提供有关该错误的更多信息?它出现在哪一行,等等?
标签: java arrays writetofile