Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中将一维和二维数组写入文本文件_Java_Arrays_Printf - Fatal编程技术网

如何在Java中将一维和二维数组写入文本文件

如何在Java中将一维和二维数组写入文本文件,java,arrays,printf,Java,Arrays,Printf,我想将数组的每个元素写入一个文本文件。下面的示例将更清楚地演示 String[] Name = {"Eric","Matt","Dave"} Int[] Scores = {[45,56,59,74],[43,67,77,97],[56,78,98,87]} double[] average = {45.7,77.3,67.4} 我希望在文本文件中包含以下内容 Student Eric scored 45,56,59,74 with average of 45.7 Student Matt

我想将数组的每个元素写入一个文本文件。下面的示例将更清楚地演示

String[] Name = {"Eric","Matt","Dave"}

Int[] Scores = {[45,56,59,74],[43,67,77,97],[56,78,98,87]}

double[] average = {45.7,77.3,67.4}
我希望在文本文件中包含以下内容

Student Eric scored 45,56,59,74 with average of 45.7
Student Matt scored 43,67,77,97 with average of 77.3
Student Dave scored 56,78,98,87 with average of 67.4
我创建了输出文件

PrintStream output = new PrintStream(new File("output.txt"));
我用了一个for循环

for(int i =0;i<=Name.length;i++){

    output.println("Student  " + Name[i] + " scored " + Scores[i] + " with average of " + average[i]);
}

for(int i=0;i我猜编译器不喜欢这一行:

Int[] Scores = {[45,56,59,74],[43,67,77,97],[56,78,98,87]}
Java中没有
Int
类型。假设您的意思是
Int
,编译器仍然会抱怨,因为
[45,56,59,74]
不是Int

您需要的是一个
int[][]
和一个声明,如:
{{45,56,59,74}


不过,我不确定您是否会满意输出…

您必须使用FileWriter而不是PrintStream

BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
                                        "C:/new.txt"), true));

StringBuffer sb = new StringBuffer();

for(int i =0;i<=Name.length;i++){
    sb.append("Student " + Name[i] + " scored " + Scores[i]
    + " with average of " + average[i] + "\n"); 
}

bw.write(sb.toString());
bw.close();
BufferedWriter bw=new BufferedWriter(new FileWriter)(新文件(
“C:/new.txt”),正确);
StringBuffer sb=新的StringBuffer();
对于(int i=0;i
  • 二维数组需要两个括号而不是一个括号
  • Int应该是小写
  • 变量应为小写(分数而不是分数)
  • 所以它应该是这样的:

    int[][] scores = {{45,56,59,74},{43,67,77,97},{56,78,98,87}};
    
    此外,for循环应该从0运行到小于长度的1,否则将超出范围

    names.length = 3
    names[0] = "Eric"
    names[1] = "Matt"
    names[2] = "Dave"
    

    因此,当您尝试访问名称[3]时,会出现越界异常,因为数组仅包含3个元素。

    可能您忘记刷新或关闭打印流(我还修复了上述错误)

    导入java.io.File;
    导入java.io.FileNotFoundException;
    导入java.io.PrintStream;
    公共班机{
    公共静态void main(字符串[]args){
    字符串[]名称={“Eric”、“Matt”、“Dave”};
    int[][]得分={45,56,59,74},{43,67,77,97},{56,78,98,87};
    双[]平均值={45.7,77.3,67.4};
    试一试(
    PrintStream输出=新的PrintStream(新文件(“output.txt”);
    ){
    
    对于(int i=0;i请告诉我们“它不起作用”的具体方式。它为什么不起作用?发生了什么事?
    int
    大写不是常规的java。而不是“/n”是要插入新行的反斜杠。我不知道该字符在我的新机器的键盘上的什么位置。这并不意味着使用缓冲编写器是一个不好的答案或解决方案。
    Arrays.toString(int[])
    将很容易修复输入。
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    
    public class Main {
        public static void main(String[] args) {
            String[] Name = {"Eric","Matt","Dave"};
    
            int[][] Scores = {{45,56,59,74},{43,67,77,97},{56,78,98,87}};
    
            double[] average = {45.7,77.3,67.4};
    
    
    
            try (
                    PrintStream output = new PrintStream(new File("output.txt"));
                ){
    
                for(int i =0;i<Name.length;i++){
                    String sc ="";
                    for (int j=0;j<Scores[i].length;j++){
                            sc+=Scores[i][j]+" ";
                    }
                    output.println("Student  " + Name[i] + " scored " + sc + " with average of " + average[i]);
                }
                output.close();
    
            } catch (FileNotFoundException e) {
    
                e.printStackTrace();
            }
    
        }
    
    
    
    }