Skip to content
October 7, 2013 / TDS Software

Java Q&A

Write a Java program to store the following data set in a two dimensional array. Then access
the aray and write all these information in to a text file called “student_data.txt” . Use a
traditional for loop to access the array. Save your answer as “Question13.java”
STUDENT NO STII SPD PS DCCNII SEI
DIT/10/C1/0001 A A- B+ A ADIT/
10/M2/0123 C- C B B B+
DIT/10/M1/0054 D C- C B- B
DIT/10/M1/0025 A A A- A ADIT/
10/C2/1254 C C- B B+ B

 

Re-write the above Java program again. Use a for-each loop to access the array. Save your
answer as “Question14.java”

 

import java.io.*;
class Question14{
public static void main(String[] args) {
String s[][]={{“STUDENT NO”, “STII”, “SPD”, “PS”, “DCCNII”, “SEI”},
{“DIT/10/C1/0001”, “A”, “A-“, “B+”, “A”, “A”},
{“DIT/10/M2/0123”, “C-” ,”C” ,”B” ,”B”, “B+”},
{“DIT/10/M1/0054”, “D” ,”C-” ,”C” ,”B-“, “B”},
{“DIT/10/M1/0025”, “A” ,”A” ,”A-“, “A”, “A”},
{“DIT/10/C2/1254”, “C” ,”C-” ,”B” ,”B+”, “B”}};

try {
File file = new File(“E:/tudent_data.txt”);

// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);

for(String[] sI : s){
String content = “”;
for (String sJ : sI){
content += sJ + “\t” ;
}

bw.write(content);
bw.newLine();
}

bw.close();
System.out.println(“Done”);

} catch (IOException e) {
e.printStackTrace();
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: