Converting text file to all uppercase letters

nerd4life picture nerd4life · Mar 11, 2014 · Viewed 17.1k times · Source

I have an assignment for class and I have to make a program that takes an existing file and converts all the letters to uppercase. Below is part of the code (specifically the loop):

 // Read lines from the file until no more are left.
  while (inputFile.hasNext())
  {
      // Read the next name.
     String friendName = inputFile.nextLine();

      // Need line to make all letters uppercase

  }

  System.out.print("Enter the saved file name: ");
  String filename2 = keyboard.nextLine();

When I run it now: I am able to open the first text file and a second txt file is created, but there is no letters in the new file.

Answer

Rahul Tripathi picture Rahul Tripathi · Mar 11, 2014

You may try like this:

File file1 = new File("initial.txt");
File file2 = new File("final.txt"); 
char CharCounter = 0;       
BufferedReader in = (new BufferedReader(newFileReader(file1)));
PrintWriter out = (new PrintWriter(new FileWriter(file2)));
int ch;
while ((ch = in.read()) != -1)
{
   if (Character.isLowerCase(ch))
   {
        ch=Character.toUpperCase(ch);// convert assign variable
   }
out.write(ch);
}
in.close();
out.close();