Help!!, Changing from uppercase to lowercase
I am trying to read in a file and change the first letter of the first word to uppercase and then change the second word to upperCase and so on. Also, I need to delete all the spaces in between. Example:
Contol Measure Mapping (needs to look like) controlMeasureMapping
If anyone can assist me, I would really appreciate it. I will provide the code where I am stuck below.
import java.io.*;
import javax.swing.*;
class BufferReaderDemo {
public static void main(String args[]) {
try {
FileReader fr = new FileReader("C:\\Documents and Settings\\rwhitehurst\\Desktop\\Test.txt");
BufferedReader br = new BufferedReader(fr);
String s;
int ToCharArray[] = new int[4];
String output = "Index\tValue\n";
while((s = br.readLine()) != null){
if (s.length() > 0)
{
}
System.out.println(s);
}
fr.close();
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
This one leaves at least one blank, and it's generic I suppose...
Code:
/**
* Generic (?) capitalizer.
* @author sjalle
* @version 1.0
*/
import java.io.*;
public class Capitalizer {
static final String PUNCTUATION = "!\"§$%&/()=?{[]}\\#'~+-*/,;.:<>|^°";
static final String WHITESPACE = " \t\n\f\r" + (char)0x0B;
private StringBuffer sb=new StringBuffer();
private byte [] buf=null;
private boolean setCap=false;
/**
* Three diferent ways to use, nice...
* @param s
*/
public Capitalizer (String s) {
buf=s.getBytes();
}
public Capitalizer (byte [] b) {
buf=b;
}
public Capitalizer (InputStream in) throws IOException {
int n=in.available();
if (n==0) return;
buf=new byte[n];
in.read(buf);
in.close();// perhaps ....
}
/**
* Do the stuff
* @return
*/
public String capitalize() {
boolean inBlank=false;
sb.setLength(0);
for (int i=0; i<buf.length; i++) {
char c=(char)buf[i];
if (c==' ' || WHITESPACE.indexOf(c) >= 0) {
if (inBlank) continue;
inBlank=true;
setCap=true;
} else if (PUNCTUATION.indexOf(c) >= 0) {
sb.append(c);
setCap=true;
continue;
} else {
inBlank=false;
}
if (setCap) {
c=(char)new String(new char[]{c}).toUpperCase().getBytes()[0];
if (c!=' ') setCap=false;
}
sb.append(c);
}
return sb.toString();
}
/**
* ******************************' MAIN ******************************
* @param args
*/
public static void main (String [] args) {
Capitalizer ct=new Capitalizer("try this $one \t \t \tfirst%then a file");
String s=ct.capitalize();
try {
FileInputStream in = new FileInputStream("c:\\tmp\\classes_tut.txt");
ct = new Capitalizer(in);
s = ct.capitalize();
System.out.println(s);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}