0

I can't seem to get StringUtils.capitalize to actually capitalize my whole word string.

I've tried various ways, but I just end up getting sentence-type case. I tried using StringUtils.capitalize inside of what I want to print, but that doesn't work either. Nothing I look up helps me either.

  File file = 
      new File("C:\\Users\\mikek\\Desktop\\name.txt"); 
    BufferedReader abc = new BufferedReader(new FileReader(file));
List<String> data = new ArrayList<String>();
String s;
String t;
while((s=abc.readLine()!=null) {
    data.add(s);

    System.out.println("public static final Block " + s.toUpperCase() + "     = new "
+  StringUtils.capitalize(s).replace("_","") + "(\"" + s + "\", Material.ROCK);");
}

abc.close();
 }

Expected: Charcoal Block Got: Charcoal block

Michael K.
  • 43
  • 1
  • 9
  • 1
    I don't understand. Do you except `Charcoal Block` to be stored in `data` or what? – Luiggi Mendoza Aug 02 '19 at 21:11
  • 1
    The documentation says "Capitalizes a String changing the first character to title case as per Character.toTitleCase(int). No other characters are changed.", so it doesn't bother about other words in your String. It just uses the first char, nothing else. The documentation also suggests to look at `WordUtils.capitalize(String)`. You should take their advice. – Tom Aug 02 '19 at 21:15
  • @Luiggi I am trying to make a mod tool. It is extremely tedious adding each item and I am writing this to improve my efficiency. Effectively I am generating code to copy/paste into my mod. – Michael K. Aug 02 '19 at 21:18
  • Changing `t` after calling `data.add(t)` does not change what’s in `data`. It just makes the variable `t` point to a different String object, one which was never added to any List. – VGR Aug 03 '19 at 03:37
  • VGR so basically, when I am bringing "charcoal_block" in from my text file, "charcoal_block" becomes the string and replacing the "_" with " " edits that one string? Like, it becomes "Charcoal block" because the .capitalize only capitalizes each individual string and I'm not doing anything to make it 2 strings? (If this makes any sense) – Michael K. Aug 03 '19 at 03:45

2 Answers2

1

How about this??

        String s = "camel case word";
        String camelCaseSentence = "";
        String[] words = s.split(" ");
        for(String w:words){
            camelCaseSentence += w.substring(0,1).toUpperCase() + w.substring(1) + " ";

        }
        camelCaseSentence = camelCaseSentence.substring(0, camelCaseSentence.length()-1);
        System.out.println(camelCaseSentence);
  • This worked when I replaced the s.split(" ") to s.split("_"). Then I added camelCaseSentence.replace(" ","") to it instead of t. Thank you! – Michael K. Aug 03 '19 at 03:53
0

What Chris Katric helped me figure out:

File file = 
  new File("C:\\Users\\mikek\\Desktop\\name.txt"); 
BufferedReader abc = new BufferedReader(new FileReader(file));
List<String> data = new ArrayList<String>();
String s;

while((s=abc.readLine())!=null) {
data.add(s);
String camelCaseSentence = "";
    String[] words = s.split("_");
    for(String w:words){
        camelCaseSentence += w.substring(0,1).toUpperCase() + w.substring(1) + " ";

    }
    camelCaseSentence = camelCaseSentence.substring(0, camelCaseSentence.length()-1);

System.out.println("public static final Block " + s.toUpperCase() + " = new "
+  camelCaseSentence.replace(" ", "") + "(\"" + s + "\", Material.ROCK);");
}

abc.close();
 }

Now I get(for the full part of the System.out.println): "public static final Block CHARCOAL_BLOCK = new CharcoalBlock("charcoal_block", Material.ROCK);" just like I wanted.

Michael K.
  • 43
  • 1
  • 9