Java 将多个输出保存到csv文件
因此,自从加入这个社区以来,我收到了很多学习Java的技巧和资源。我现在已经到了第六周,正在完成我的第三个项目。我觉得我学到了很多,但如果我想掌握Java,我还有很长的路要走 这次我的问题是如何让代码将多个输出保存到文件中 我当前项目的一部分是做以下工作: 当窗口关闭时,效率值应使用从0到10的>n值计算并写入文件。文件>>的每一行都应该包含n的值、n的迭代方法的效率以及递归方法的效率。值>应以逗号分隔,以便使用Excel打开文件 我已经设法让程序在输出文件中写入一个条目。但是,我要么在代码中出错,要么遗漏了一些关键内容。有人能给我指出正确的解决办法吗?我想我可能需要创建一个数组,将输出存储在那里,然后将数组输出到csv。我看过roseindia和viralpatel,但它们没有透露我的希望 序列部分我搞砸了Java 将多个输出保存到csv文件,java,csv,output,Java,Csv,Output,因此,自从加入这个社区以来,我收到了很多学习Java的技巧和资源。我现在已经到了第六周,正在完成我的第三个项目。我觉得我学到了很多,但如果我想掌握Java,我还有很长的路要走 这次我的问题是如何让代码将多个输出保存到文件中 我当前项目的一部分是做以下工作: 当窗口关闭时,效率值应使用从0到10的>n值计算并写入文件。文件>>的每一行都应该包含n的值、n的迭代方法的效率以及递归方法的效率。值>应以逗号分隔,以便使用Excel打开文件 我已经设法让程序在输出文件中写入一个条目。但是,我要么在代码中出
package cmisproject3;
public class Sequence {
private static int efficiency = 0;
// method to compute iterative
public static int computeIterative(int n) {
int result = 0;
efficiency = 0;
if (n == 0) {
result = 0;
} else if (n == 1) {
result = 1;
} else {
int secondPrevious = 0;
int previous = 1;
for (int i = 2; i <= n; i++) {
efficiency++;
result = 2 * previous + secondPrevious;
secondPrevious = previous;
previous = result;
}
}
return result;
}
// method to comopute recursive
public static int computeRecursive(int n) {
efficiency = 0;
return computeRecursiveHelper(n);
}
private static int computeRecursiveHelper(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
efficiency++;
return 1;
} else {
efficiency++;
return 2 * computeIterative(n - 1) + computeIterative(n - 2);
}
}
public static int getEfficiency() {
return efficiency;
}
}
对于那些感兴趣的人,这里是我正在使用的参数。
每次执行操作时,您都会重新打开文件,而不会告诉FileWriter追加而不是覆盖 见:
我认为你的项目有了一个良好的开端。然而,除了你的问题之外,我还看到了其他问题 首先我要回答你的问题,然后再谈其他问题。我想当你说: 将单个条目写入输出文件 你是说你可以在文件中写一行。这意味着你的问题是:我怎样才能在一个文件中写入多行 在这种情况下,您至少有两种选择。一种是将FileWriter设置为覆盖现有文件内容,而不是默认行为 另一种选择是避免在完成编写之前关闭FileWriter。例如,您可以通过将fileWriter的构造移动到GUI的构造函数中,并将对close方法的调用移动到GUI关闭时触发的事件处理程序中来实现这一点 无论您选择做什么,您都需要记住在每行末尾写换行符,否则您的文件将是一个很长的行。因此,修改您现在拥有的内容将如下所示: fileWriter.writeresult+,+sequence.getEfficiency+\n 其他问题: 您的Sequence.ComputerCursiveHelper方法不是递归的。递归方法会调用自身,而您的方法不会这样做 我认为你没有正确地按照指示去做。也许您还没有完成,您打算修改代码。如果是这样的话,你可以忽略我要指出的。指示说明: 当窗口关闭时,应使用从0到10的n值计算效率值,并将其写入文件
当前,每当用户单击“计算”按钮而不是执行上述操作时,您都会写入文件。此外,您没有写入正确的数据-您正在写入基于用户输入的值,而不是使用n从0到10获得的值 所有左对齐代码==非常难读的代码。你确定你想让你的问题比必须的更难回答吗?
package cmisproject3;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class CMISProject3 extends JFrame implements ActionListener {
private final int TWICE = 2;
private JLabel jLabel1 = new JLabel(), jLabel2 = new JLabel(), jLabel3 = new JLabel(), jLabel4 = new JLabel(), jLabel5 = new JLabel(), jLabel6 = new JLabel();
private ButtonGroup radioButtons = new ButtonGroup();
private JRadioButton iterativeBtn = new JRadioButton(), recursiveBtn = new JRadioButton();
private JTextField enterN = new JTextField(16), textResult = new JTextField(16), textEfficiency = new JTextField(16);
private JButton computeBtn = new JButton();
private int efficiency;
private Sequence sequence;
private static FileWriter fileWriter;
private File file = new File("output.txt");
// Beginning of the constructor for the GUI
public CMISProject3() throws IOException {
sequence = new Sequence();
setSize(300, 200); // define size of GUI
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(6, 2));
getContentPane().add(jLabel4);
radioButtons.add(iterativeBtn);
iterativeBtn.setSelected(true); // sets Iterative as default GUI selection
iterativeBtn.setText("Iterative");
getContentPane().add(iterativeBtn);
getContentPane().add(jLabel5);
radioButtons.add(recursiveBtn);
recursiveBtn.setText("Recursive");
getContentPane().add(recursiveBtn);
jLabel1.setText("Enter n: ");
getContentPane().add(jLabel1);
getContentPane().add(enterN);
getContentPane().add(jLabel6);
computeBtn.setText("Compute");
computeBtn.addActionListener(this);
getContentPane().add(computeBtn);
jLabel2.setText("Result: ");
getContentPane().add(jLabel2);
getContentPane().add(textResult);
textResult.setEditable(false);
jLabel3.setText("Efficiency: ");
getContentPane().add(jLabel3);
getContentPane().add(textEfficiency);
textEfficiency.setEditable(false);
pack();
}
public void actionPerformed(ActionEvent event) {
int result;
efficiency = 0;
try {
fileWriter = new FileWriter(file);
} catch (IOException e1) {
e1.printStackTrace();
}
if (iterativeBtn.isSelected()) {
result = sequence.computeIterative(Integer.parseInt(enterN.getText()));
} else {
result = sequence.computeRecursive(Integer.parseInt(enterN.getText()));
}
try {
System.out.println(result);
fileWriter.write(result + ", " + sequence.getEfficiency());
} catch (IOException e) {
e.printStackTrace();
}
textResult.setText(Integer.toString(result));
textEfficiency.setText(Integer.toString(sequence.getEfficiency()));
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
CMISProject3 CMISProject3 = new CMISProject3();
CMISProject3.setVisible(true);
}
}