-2

Actually i wanted to check the condition that the input sentence has its first letter already in capital The code below is converting the lowercase character of first letter in the string to upper case i wanted to add the line if suppose user enter a string like "I Am Human Being" means in this the first character is already in upper case then how to add a line to the existing code

Scanner sc=new Scanner(System.in);
String line=sc.nextLine();
String uc="";
Scanner line_sc=new Scanner(line);
while(line_sc.hasNext()) {
    String word=line_sc.next();
    uc+=Character.toUppercase(word.charAt(0))+ word.substring(1)+" ";
}
System.out.println(uc.trim());
Ragnarok
  • 37
  • 1
  • 8

1 Answers1

1

You can do this:

String str = "input";
String capital = str.substring(0, 1).toUpperCase() + str.substring(1);

return str.equals(capital);

or even better:

return Character.isUpperCase(str.charAt(0));
dome
  • 820
  • 7
  • 20
  • The first answer won't work since the "==" comparison between two objects (here, two Strings) won't return true unless both have the same address. Please change this comparison to `str.equals(capital)` – William A. May 17 '19 at 15:12
  • RIght, just changed. – dome May 17 '19 at 15:13
  • No sir its not i wanted to print a output not to return the sentence – Ragnarok May 17 '19 at 15:21
  • what is exactly the result you want? – dome May 17 '19 at 15:22
  • If user enters Hello I Am Human Being then it must print the first character of the string is already in caps – Ragnarok May 17 '19 at 15:31
  • 1
    @Ragnarok we have shown you *how* to determine wether the first char is uppercase or not. **Please** try to **understand** how that works and transfer that knowledge. That is such a basic thing to do, you **have** to be able to do that. – luk2302 May 17 '19 at 15:34
  • I showed you how to check if the first char is capital. Shouldn't be difficult to print it. – dome May 17 '19 at 15:51