'Converting text file to all uppercase letters

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.



Solution 1:[1]

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();

Solution 2:[2]

Try String upper = friendName.toUpperCase(). Now the upper String has all the character in the uppercase.Use it according to your need. Refer String API

Solution 3:[3]

Look at the String class documentation and try to find a method to change the string value to all uppercase.

Also you need to get the output file name above the while loop and then write to the output file inside the loop since for every iteration of the loop, the string value of the previous iteration is lost.

Solution 4:[4]

Use FileUtils readFileToString() and writeFileToString() to do the work simple,

String content = FileUtils.readFileToString(new File(FILE_PATH));
FileUtils.writeStringToFile(new File(FILE_PATH), content.toUpperCase());

Solution 5:[5]

friendName = friendName +System.lineSeparator()+friendName.toUpperCase();

//After the while loop finishes, write the contents of friendName in your output file.

Solution 6:[6]

Try This Demo Code:

       import java.io.*;
       import java.util.*;
       class m{

  public static void main (String[] args) {
     try
    {
     FileReader fr = new FileReader("file.java");
     BufferedReader br = new BufferedReader(fr);
     PrintWriter out = (new PrintWriter(new FileWriter("mahesh.java")));
     String s="";
   while((s = br.readLine()) != null) 
   {
      out.write(s.toUpperCase()+"\n");
    }
  out.close();
  }
    catch(Exception e)
    {
     e.printStackTrace();
    }
   }

  }

Solution 7:[7]

package WT_LAB_THIRTEEN;
import java.io.*;

public class StringsToUpperCase {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(
                new FileReader(new File("C:\\Users\\Web Tech\\WT_LAB_THIRTEEN\\InputFile.txt")));
        PrintWriter pw = new PrintWriter(
                new FileWriter(new File("C:\\Users\\Web Tech\\WT_LAB_THIRTEEN\\OutputFile.txt")));
        int ch;
        while ((ch = br.read()) != -1) {
            if (Character.isLowerCase(ch)) {
                ch = Character.toUpperCase(ch);
            }
            /*the below part of the code can be used to convert UpperCase characters to lowerCase 
            else if (Character.isUpperCase(ch)) {
                ch = Character.toLowerCase(ch);
            }*/
            pw.write(ch);
        }
        br.close();
        pw.close();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 displayName
Solution 2 amudhan3093
Solution 3 anirudh
Solution 4 newuser
Solution 5 Hirak
Solution 6 Benjamin
Solution 7 procrastinator