0

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.

3
  • Please tell us in what specific way "it did not work". Commented Feb 23, 2013 at 1:17
  • 3
    Why didn't it work? What happened? Commented Feb 23, 2013 at 1:17
  • 1
    Int with uppercase is not regular java. Commented Feb 23, 2013 at 1:19

4 Answers 4

2

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...

Sign up to request clarification or add additional context in comments.

1 Comment

Arrays.toString(int[]) would fix the input easily.
0

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/

Comments

0
  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.

Comments

-1

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

2 Comments

instead of "/n" is backslash to insert in a new line. I don't know where that character is on the keyboard of my new machine.
that does not mean that using buffered writer is a bad answer or solution then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.