0

hi guys im trying to capitalise the first letter of every string however it doesn't seem to work.

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class Main {
  public static void main(String[] args) throws IOException {
    InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
    BufferedReader in = new BufferedReader(reader);
    String line = in.readLine();
    while ((line = in.readLine()) != null) {
      String output = line.substring(0, 1).toUpperCase() + line.substring(1) + " ";
      System.out.println(output);
    }
  }
}

the system inputs a sentence like "stack overflow" and needs the output to be "Stack Overflow" so every letter is capital.

It seems to only put the same input, no change. i have searched everywhere online however most resources are only for the first string.

any help would be appreciated.

sarah
  • 13
  • 4
  • 4
    Does this answer your question? [How to capitalize the first character of each word in a string](https://stackoverflow.com/questions/1892765/how-to-capitalize-the-first-character-of-each-word-in-a-string) – OH GOD SPIDERS Jul 13 '22 at 09:50
  • What data would `line` contain? Individual words or entire sentences? If the latter then you need to split the line by whitespace to get an array of individual words. – Thomas Jul 13 '22 at 09:53
  • also, for future reference, if you create a new question: don't forget to mention what goes wrong. – Stultuske Jul 13 '22 at 09:54
  • @OHGODSPIDERS not really, it only explains to capitalise the first letter, dont see any helpful part. – sarah Jul 13 '22 at 09:57
  • @Thomas it is just two words like shown in the example "stack overflow" – sarah Jul 13 '22 at 09:57
  • @sarah What do you mean "not really"? The linked duplicate does precisely what you seem to want: "capitalize the first character of each word in a String". – OH GOD SPIDERS Jul 13 '22 at 10:02
  • 1
    "dont see any helpful part" - the answers on that question contain a lot of helpful parts depending on your needs. Some even cover capitalizing only at the start of a sentence, after special characters or after spaces (which is what I guess you want). So I'd suggest having a deeper look at those. - That being said, your code should capitalize at least the first character of each line. If you can't see any change then maybe the first character isn't anything that can be capitalized, i.e. are you sure `line` would be `"stack overflow"` and not `" stack overflow"`? – Thomas Jul 13 '22 at 10:03
  • 1
    If `line` contains the entire sentence, i.e. `"stack overflow"` then you need to _split_ the line into words first, then iterate over those words and capitalize the first character. The linked question contains several smart approaches to that which don't require splitting, but splitting might be easier for your level atm. – Thomas Jul 13 '22 at 10:04
  • You are reading a line before you start your while loop, so you'll always ignore the first line of input. – tgdavies Jul 13 '22 at 11:27

2 Answers2

2

Try with this code.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class Main {
   public static void main(String[] args) throws IOException {
       InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
       BufferedReader in = new BufferedReader(reader);
       String line = null;
       String output = "";
       while ((line = in.readLine()) != null) {
           line = line.trim();
           String[] lineSplitArr = line.split(" ");
           for (String s : lineSplitArr) {
               output += (s.substring(0, 1).toUpperCase() + s.substring(1)+" ");
           }
           System.out.println(output);
           break;
       }
   }
}
1

A "simple" a.k.a. an overlooked typo is what you have at line no. 9.

Because of that typo, it reads the input then goes to the next line, skipping your while-loop.

I also saw that you can only capitalize the first letter of your first word, which is definitely not what you were planning to do.

Simply modify your code into:

public class Main {
  public static void main(String[] args) throws IOException {
    try(
      InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
      BufferedReader in = new BufferedReader(reader)
    ){
      String line;
      while ((line = in.readLine()) != null) {
        String output="";
        for (String s:line.split(" ")) {
          output += s.substring(0, 1).toUpperCase() + s.substring(1) + " ";
        }
        System.out.println(output);
      }
    }
  }
}

I also noticed that you have several "leakages" into your code which is~ in other words, "bad practice" so the code looks a tad bit different.

JumperBot_
  • 551
  • 1
  • 6
  • 19