• Register
0 votes
18 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,050 points

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,930 points

Related questions

0 votes
1 answer 9 views
9 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 Daniel Anderson 4k points
0 votes
1 answer 7 views
7 views
I need to know how to remove the first cgaracter of a string in Java.
asked Sep 25 Daniel Anderson 4k points
1 vote
1 answer 12 views
12 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 Gavin 15.3k points