-1

I have a text file named read.txt that says "JAVA PROGRAMMING" and i want to copy it to another file named write.txt and replace the space into underscore like this: "JAVA_PROGRAMMING". Big thanks to anyone who would help :)

My code:

import java.io.*;  
import java.util.*;  
class Main {  
 public static void main(String arg[]) throws Exception {  
  FileReader fin = new FileReader("read.txt");  
  FileWriter fout = new FileWriter("write.txt");  
  int i;  
  while ((i = fin.read()) != -1) {  
   fout.write(i);
  }  
  System.out.println("Successfully copied!");  
  fin.close();  
  fout.close();
 }
}
Noran
  • 13
  • 4
  • 1
    Within your `while` loop you need to test for a space character. If its not a space, write `i`, otherwise write an underscore. – codebod Mar 09 '21 at 10:15

1 Answers1

0

Revised code with the correct answer:

import java.io.*;  
import java.util.*;  
class Main {  
 public static void main(String arg[]) throws Exception {  
  FileReader fin = new FileReader("read.txt");  
  FileWriter fout = new FileWriter("write.txt");  
  int i;  
   while ((i = fin.read()) != -1) {  
    if(i!=' '){
       fout.write(i);}
    else{
       fout.write("_");}
    }  
   System.out.println("Successfully copied!");  
   fin.close();  
   fout.close();
   }
  }
Noran
  • 13
  • 4