Ok, i didn't know whether to put this to my already posted question which is the "String.trim() method help....." post since this is a totally different question, but it relates to the same program..
What i'm very confused about is how to read a passage from a text file, and convert the first character of each word to a Capital letter.. I'm sure the program has a lot of bugs in it that still needs to be fixed, but i'm trying to work on it piece by piece... The code that i'm confused on right now is the, but that's neither here nor there.. The problem i'm having right now is that i'm getting the "variable may not have been initialized" era, and i'm really unsure of how i'm getting it.... That variable that getting the era is "the_words" variable all the way in the last statement(For loop and the print statement)Code:str[i]= the_words[i].substring(0, 1).toUpperCase()+str[i].substring(1);
Code:public class project2
{
public static void main(String[] args)
throws java.io.IOException
{
// Declare Variables
int i;
int num_lines = 0;
String infilename;
String str [] = new String [1000];
String [] the_words;
// Establish keyboard input stream
Scanner sc = new Scanner(System.in);
// Get required data items from user
// Prompt user for input file name
System.out.print("Enter name of text file: ");
infilename = sc.nextLine();
// Check for existence of the file
File infile = new File(infilename);
if(!infile.exists())
{
System.out.println("The file" +infilename+ "does not exists.");
System.out.println("Quitting now. Please try again.");
System.exit(1);
}
//Open the file for reading
Scanner fsc = new Scanner(infile);
//Read the data in the file line by line
while (fsc.hasNext() )
{
str[num_lines] = fsc.nextLine();
num_lines ++;
}
//fsc.close();
// Print text passage
for(i=0; i<num_lines; i++)
System.out.println(str[i]);
System.out.println();
System.out.println();
// Count the total number of words in passage
int sum = 0;
for(i=0; i<num_lines; i++)
{
the_words = str[i].trim().split("\\s+");
// Calcualte amount of words
sum += the_words.length;
}
// Print the amount of words
System.out.println("Amount of words in passage: "+sum);
System.out.println();
System.out.println();
// Capitalize first letter of every word
for(i = 0; i<the_words.length; i++)
{
if (i==0)
{
// Capitalize the first letter of the string
str[i]= the_words[i].substring(0, 1).toUpperCase()+str[i].substring(1);
}
}
System.out.println(the_words[i]);
}
}

