Advertisement
advictoriam

Untitled

Mar 14th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. /**
  2.    Reads a file of lower case names, and writes a file of the
  3.    names with the first letter of each name in uppercase.
  4. */
  5.  
  6. import java.io.FileReader;
  7. import java.io.FileNotFoundException;
  8. import java.util.Scanner;
  9. import java.io.PrintWriter;
  10.  
  11. public class UpcaseNames
  12. {
  13.    public static void main(String[] args)
  14.       throws FileNotFoundException
  15.    {
  16.       String inputFileName = "names.txt";
  17.       String outputFileName = "upcaseNames.txt";
  18.  
  19.       FileReader fr = new FileReader(inputFileName);
  20.       PrintWriter writer = new PrintWriter(outputFileName);
  21.      
  22.       try
  23.       {
  24.          boolean isFirst = true;
  25.          while(true)
  26.          {
  27.             int ch = fr.read();
  28.          
  29.             if(ch == -1)
  30.                break;
  31.             else if((char)ch == ' ' || (char)ch == '\n')
  32.             {
  33.                isFirst = true;
  34.                writer.write((char)ch);
  35.             }
  36.             else if(isFirst)
  37.             {
  38.                isFirst = false;
  39.                writer.write(Character.toUpperCase((char)ch));  
  40.             }
  41.             else
  42.                writer.write((char)ch);
  43.          }
  44.       }
  45.       catch(Exception e){}
  46.      
  47.       try
  48.       {
  49.          fr.close();
  50.          writer.close();
  51.       }
  52.       catch(Exception e){}
  53.    }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement