Below is 2 java code. The first code is the text file and the second one allows
ID: 3883594 • Letter: B
Question
Below is 2 java code. The first code is the text file and the second one allows me to print that file. When I run the program file, I get
5 9 9 9 8
9 8 9 3 6
6 6 7 6 10
I am trying print only the larger value from each row. Can someone guide or help me make it look like this?
Row 1: 9
Row 2 : 9
Row 3: 10
import java.util.Random; import java.io.*; ublic class TestData public static void main(String[] args) throws IOException int value; String file"test.dat"; Random rand = new Random(); FileWriter fw- new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); PrintWriter outFile = new PrintWriter(bw); for (int lines]; lineExplanation / Answer
import java.io.*;
import java.util.*;
public class Test1 {
public static void main(String[] args) {
String filename = "test.dat";
String line = null;
try {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int count = 0;
while ((line = bufferedReader.readLine())!= null) {
count++;
String[] num = line.split(" ");
int max = 0;
for (int i = 0; i<num.length; i++){
if (max < Integer.parseInt(num[i]))
max = Integer.parseInt(num[i]);
}
System.out.println("Row "+count+":" + max);
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file " + filename + "");
}
catch(IOException ex){
System.out.println("Error reading file " + filename + "");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.