My code compiles successfully but doesn't run why??
What problem whatsoever is with the syntax?????

import java.util.Scanner;
public class uppercase{
	public static void main(String args){
		Scanner in=new Scanner(System.in);
		System.out.println("Enter string:");
		String str=in.nextLine();
		for(int i=0;i<str.length();i++)
		{if(i!=str.length()-1&&str.charAt(i+1)>='a'&&str.charAt(i+1)<='z'&&str.charAt(i)==' ')
		{ str = Character.toUpperCase(str.charAt(i)) + str.substring(i+1);

		}


		}
	if(str.charAt(0)>='a'&&str.charAt(0)<='z')
		str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
	System.out.print(str);
	}

}

Recommended Answers

All 4 Replies

"Doesn't run"
Do you mean when you try to execute it nothing happens? Or does it throw a runtime Exception? Or does it give a wrong result, and if so, what's the difference between the expected and actual result.

But anyway, line 9 you lose all the text before the char you are upper-casing

"But anyway, line 9 you lose all the text before the char you are upper-casing "

But that line will never execute:

{if(i!=str.length()-1&&str.charAt(i+1)>='a'&&str.charAt(i+1)<='z'&&str.charAt(i)==' ')

The set of chars a such that a>='a' && a<='z' && a ==' ' is the empty set.

Sorry.
Correction:
But anyway, line 9 you would lose all the text before the char you are upper-casing, if the previous if condition were correct:D

your program was correct but i think the only error found is that the argument passed in your main method was (String args) instead of (String [] args)..........Thanks
i.e use public static void main(String [] args){

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.