I'm trying to read each line from input.txt
and print each line so each letter in the input line turns uppercase if it is lowercase, and turns lowercase if it is uppercase.
Additionally, I'd like to use a Thread
to do this, as I'd also like to print the reverse of each line also.
I get an error for printUppLow uppLow = new printUppLow();
and printRev rev = new printRev();
non-static variable this cannot be referenced from a static context.
Code
public static void main(String args[])
{
String inputfileName="input.txt"; // A file with some text in it
String outputfileName="output.txt"; // File created by this program
String oneLine;
try {
// Open the input file
FileReader fr = new FileReader(inputfileName);
BufferedReader br = new BufferedReader(fr);
// Create the output file
FileWriter fw = new FileWriter(outputfileName);
BufferedWriter bw = new BufferedWriter(fw);
printRev rev = new printRev();
printUppLow uppLow = new printUppLow();
rev.start();
uppLow.start();
// Read the first line
oneLine = br.readLine();
while (oneLine != null) { // Until the line is not empty (will be when you reach End of file)
// Print characters from input file
System.out.println(oneLine);
bw.newLine();
// Read next line
oneLine = br.readLine();
}
// Close the streams
br.close();
bw.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
public class printUppLow extends Thread
{
public void run(String str)
{
String result = "";
for (char c : str.toCharArray())
{
if (Character.isUpperCase(c)){
result += Character.toLowerCase(c); // Convert uppercase to lowercase
}
else{
result += Character.toUpperCase(c); // Convert lowercase to uppercase
}
}
return result; // Return the result
}
}
public class printRev extends Thread
{
public void run()
{
StringBuffer a = new StringBuffer("input.txt");
System.out.println(a.reverse());
}
}