• Register
0 votes
20 views
I have a presentation about capitalize the first letter of each word of a sentence 
I write a program like that :
 public static void main(String[] args){
	 Scanner in = new Scanner(System.in);
         System.out.print("Input a Sentence: ");
	 String line = in.nextLine();
	 String upper_case_line = ""; 
         Scanner lineScan = new Scanner(line); 
         while(lineScan.hasNext()) {
             String word = lineScan.next(); 
             upper_case_line += Character.toUpperCase; 
         }
      System.out.println(upper_case_line); 
    }

But it gives some error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
  symbol:   variable toUpperCase
  location: class java.lang.Character
	at javastackoverflow.NewClass1.main(NewClass1.java:15)
C:\Users\Hp\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 9 seconds)

What is the error ?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes
You can use trim method to take a upper case number after space:
 public static void main(String[] args){
	 Scanner in = new Scanner(System.in);
         System.out.print("Input a Sentence: ");
	 String line = in.nextLine();
	 String upper_case_line = ""; 
         Scanner lineScan = new Scanner(line); 
         while(lineScan.hasNext()) {
             String word = lineScan.next(); 
             upper_case_line += Character.toUpperCase(word.charAt(0)) + word.substring(1) + " "; 
         }
      System.out.println(upper_case_line.trim()); 
    }

Happy coding

 

11 6 4
34,950 points

Related questions

0 votes
1 answer 21 views
21 views
Problem: Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others? Examples: jon skeet -> Jon Skeet miles o'Brien -> Miles O'Brien (B remains capital, this rules out Title Case) old mcdonald -> Old Mcdonald* *(Old McDonald would be find too, but I don't expect it to be THAT smart.)
asked Dec 3, 2020 Mashhoodch 6.1k points
0 votes
1 answer 2 views
2 views
What can i do?
asked 1 day ago TeamScript 10.3k points
0 votes
1 answer 20 views
20 views
I want to know a function built into Java that capitalizes the first character of each word in a String. It doesn’t affect the others?
asked Sep 28, 2020 Daniel Anderson 4k points
0 votes
1 answer 10 views
1 vote
1 answer 15 views
15 views
Problem: Hello all, I have a sample String. Let’s say it is "my name is gavin" As you can see there is no capital letter in my String. I want to make a program that can replace every first position of each word with a capital word. And the final output ... is enough, make things clear to you guys. So, my question is very simple. How can I capitalize the first letter of each word in Java?
asked Apr 8, 2020 Gavin 15.3k points
0 votes
1 answer 10 views
10 views
I need to know how to remove the first cgaracter of a string in Java.
asked Sep 25, 2020 Daniel Anderson 4k points
0 votes
1 answer 21 views
21 views
I was working on that but I got into some problems. Solve Please.
asked Nov 5, 2020 TeamScript 10.3k points
1 vote
1 answer 19 views
19 views
Problem: For the following string I tried to convert is’s first character to a upper case. So I write a program. The string: forgodsake The program I wrote is: main_str = "forgodsake" print("The main string youput is : " + str(main_str)) key = main_str[0] + ... I am getting is not right. What is the problem in my code, why am I not getting the expected result. Can anyone sort this out?
asked Jun 10, 2020 samhaz 5k points
0 votes
2 answers 116 views
116 views
Problem : How to count the number of occurrences of a character in a string in java
asked Oct 23, 2019 peterlaw 6.9k points