简体   繁体   中英

how to write one and two dimensional array into a text file in Java

I want to write each elements of arrays into a text file. Eg below will demonstrate more clearly

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}

I want the following in the text file

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

I created output file

PrintStream output = new PrintStream(new File("output.txt"));

I used a for loop

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

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

But this did not work. Please help.

My guess is the compiler didn't like this line:

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

There is not an Int type in Java. Assuming you mean int , the compiler will still complain because [45,56,59,74] is not an int!

What you need is an int[][] and a declaration like: {{45,56,59,74}}

Still, I'm not sure you will be happy with the output...

  1. two-dimensional arrays need two brackets instead of one,
  2. Int should be lower-case,
  3. variables should be lower case (scores instead of Scores).

so it should look like this:

int[][] scores = {{45,56,59,74},{43,67,77,97},{56,78,98,87}};

also, the for-loop should run from 0 to one less than length or else you'll go out of bounds.

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

so, when you try to access names[3], you'll get an out-of-bounds exception because the array only contains 3 elements.

maybe you forgot to flush or close the PrintStream (I also fixed the errors mentioned above)

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

    }



}

note that this is java7 syntax (the try..catch block with the () )

see: http://blog.sanaulla.info/2011/07/10/java-7-project-coin-try-with-resources-explained-with-examples/

You must use the FileWriter instead of the 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM