135k views
0 votes
The first question is to write a java code that reads the content of the text file "MIS104HW2_Questions.txt".

- You should use a loop to read the content and search your student ID.
- The lines start with the student ID in the format of "S012345".
- You need to use substring (0,7) to catch Student ID.
- You should print the lines only that your student ID exists to the console screen.
An example of the first question output
Q1: Well done. You have been successful in completing the first part. Please d
Q2: Write a class named
Q3. Write a class named
Q4. Write a class named
Q5. Write a class named 2
Q6. Use the
Q7. Use the
Q8. Use the
Q9. Create a
Q10. Initialize

User Danyal
by
8.1k points

1 Answer

2 votes

Final answer:

The Java code that reads the content of the text file "MIS104HW2_Questions.txt" is

import java.io.BufferedReader;

import java.io.FileReader;

class Main {

public static void main(String[] args) {

String studentID = "S012345";

try {

FileReader fileReader = new FileReader("MIS104HW2_Questions.txt");

BufferedReader bufferedReader = new BufferedReader(fileReader);

String line;

while ((line = bufferedReader.readLine()) != null) {

if (line.startsWith(studentID)) {

System.out.println(line);

}

}

bufferedReader.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Step-by-step explanation:

To read the content of a text file in Java, you can use the FileReader and BufferedReader classes. First, you need to open the file using the FileReader class. Then, you can use a loop to read each line of the file and check if it starts with your student ID using the substring method. If it does, you can print the line to the console.

Thus, to read the content of a text file in Java and search for your student ID using a loop, you can use the FileReader and BufferedReader classes. Here's an example code that demonstrates how to do this.

User Liau Jian Jie
by
8.1k points