Java file help

+2 Seamus Mackie · February 7, 2015

import java.util.*;
public class Format{
    public static void main(String[] args)throws IOException{
        String inputFileName = "location of input file";
        String outputFileName = "formatted.txt";
        // Construct the Scanner and PrintWriter objects for reading and writing
        File inputFile = new File(inputFileName);
        Scanner in = new Scanner(inputFile);
        PrintWriter out = new PrintWriter(outputFileName);
        // Read input and write output
            //Variable to hold first name
            String firstName =;
            //Variable to hold second name
            String secondName =;
            //Variable to hold date
            String date =;
            // Converts first letter to uppercase and adds the rest of the string for first name
            firstName = firstName.substring(0,1).toUpperCase()+firstName.substring(1);
            // Converts first letter to uppercase and adds the rest of the string for Second name
            secondName = secondName.substring(0,1).toUpperCase()+secondName.substring(1);
            // formats the date
            date = date.substring(0,2)+"/"+date.substring(2,4)+"/"+date.substring(4,8);
            // Tests formatted output in console
            System.out.print(firstName+" "+secondName+" "+date+"\n");
            // Writes formatted output to the outputfile
            out.print(firstName+" "+secondName+" "+date+"\n");

Im struggling to modify my code so that it can handle a middle name if given and capitalise the first letter of the middle name. As the code stands it can read a file for example containing :

james joyce 12122009
sam smith 09082007
blair gold 01022004

And produces the output file :

James Joyce 12/12/2009
Sam Smith 09/08/2007
Blair Gold 01/02/2004

I would like to handle a file that may or may not also contain a middle name for example :

james  i joyce 12122009
sam m smith 09082007
blair gold 01022004

Would output: 

James I Joyce 12/12/2009
Sam M Smith 09/08/2007
Blair Gold 01/02/2004

Any help or suggestions appreciated :) 

Post a Reply


Oldest  Newest  Rating
+1 Stefan Gost · February 8, 2015
To find a schema in a string you schould use regex (regular expression).
Regex are realy helpful but a litle bit more complex.
Here is a site you can try out some regex
0 Seamus Mackie · February 8, 2015
Thanks I will look into the use of regex in Java :)
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator