0

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);
	}

}
4
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by sam.udo
0

"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

Edited by JamesCherrill: n/a

0

"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.

Edited by jon.kiparsky: n/a

0

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

0

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){

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.