I got the code to remove the spaces in between the words but cant get it to capitalize beginning of each word. can any find what the problem is. it needs to be in camelcase.
Orginal question is - Write a Java program that will read a text file containing unknown lines of strings, turn the whole file into camelCase, and finally save the camelCase into another text file.
package p3;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class CamelCase {
public static void main(String[] args) throws IOException {
String Str = null;
File file = new File("txt.txt");
if(!file.exists()) {
System.out.println("The file does not exist.");
System.exit(0);
}
Scanner filescanner = new Scanner(file);
while (filescanner.hasNext()) {
Str= filescanner.nextLine();
System.out.println(Str);
}
filescanner.close();
char[] characters = Str.toCharArray();
boolean capitalizeWord = true;
for (int i = 0; i < characters.length; i++) {
char c = characters[i];
if (Character.isWhitespace(c)) {
capitalizeWord = true;
}
else if (capitalizeWord) {
capitalizeWord = false;
characters[i] = Character.toUpperCase(c);
}
String capsandnospace = Str.replaceAll("\\s","");
FileWriter fw = new FileWriter("CamelCase.txt");
PrintWriter pw= new PrintWriter("CamelCase.txt");
pw.println(capsandnospace);
pw.close();
}
This code
is looping through the file replacing the contents of
Str
with the current line.After the loop has finished, the value of
Str
will be that of the last line.You need to do your conversion of the string (and writing of the result file) in the loop