Q1. Write a IO class that prompts a user for a text file. The text file name may
ID: 3540419 • Letter: Q
Question
Q1.
Write a IO class that prompts a user for a text file. The text file name may be passed as an argument to your program. The text file may contain one or more strings per line such as:
Course Java
Hello
Your name
Good bye
The strings on the same line can be separated by space(s) and/or tabs. Your goal is to reproduce all the strings that you read from the file. For added fun you can print them in reverse such as %u201ColleH%u201D for %u201CHello%u201D. For super programming fun you can print them in the reverse order of appearance, e.g. the above would be printed as:
eyb dooG
eman rouY
olleH
avaJ esruoC
Q2.
Implement bubble sort, like we did insertion sort in class.
Explanation / Answer
Here is the code you need... It also takes the filename from the user .
It is fully working :)
import java.io.BufferedReader;
import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
public class ReadData {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("ENTER THE FILE PATH");
s = in.nextLine();
String file = s;
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
int lineNumber=0;
String[] lineContents = new String[100];
int i = 0;
while ((line = bufferedReader.readLine()) != null) {
++lineNumber;
lineContents[i]= line;
i++;
}
bufferedReader.close();
for (int j = lineNumber-1; j >= 0; j--){
StringBuffer buffer = new StringBuffer(lineContents[j]);
buffer.reverse();
System.out.println(buffer);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.