Page 1 of 2 12 LastLast
Results 1 to 20 of 24
Like Tree1Likes

Thread: Convert some characters of a string to uppercase

  1. #1
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Convert some characters of a string to uppercase

    hello, i want to convert some characters of a string to uppercase. For example if the string that is read from the keyboard is :"Now it's time to act!" , i want to get the string "Now It's Time To Act!" . Can anyone help me,please?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Convert some characters of a string to uppercase

    What have you tried? Where are you stuck?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Convert some characters of a string to uppercase

    i tried to do something like this:
    for(int i=0;i<s.length();i++){
    if(check) {
    s.toUpperCase();
    }
    if(s.charAt(i)== ' '){
    check=true;
    }
    else
    check=false;
    }
    System.out.println(s.toUpperCase());
    }
    But this converts the whole string to uppercase. i thought that only if the character is :' ' ,it will convert the character to uppercase.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Convert some characters of a string to uppercase

    The toUpperCase method converts the contents of the whole String object. If you only want to change one char in the String, you will need to find a way to get to that one char.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Convert some characters of a string to uppercase

    i want to transform the first letter of each word belonging to a string into a capital letter.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Convert some characters of a string to uppercase

    A couple of ways to do it:
    Break the String up into parts, change the letter and then put the String back together.
    Put the String into a StringBuffer or StringBuilder, scan the buffer and change the letters and then create a new String from the StringBuffer.
    noobplus likes this.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Convert some characters of a string to uppercase

    could you please write how to do it because i have some problem?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Convert some characters of a string to uppercase

    I think the reason for your assignment is for you to learn how to program. You won't learn if I do the work.

    Describe how you are trying to solve the problem and Post your code with the problems you are having.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Convert some characters of a string to uppercase

    i tried this:
    Java Code:
    import java.util.Scanner;
    import java.lang.String;
    import java.lang.Character;
    public class exercise4 {
    
    	
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		String s = in.nextLine();
    		boolean check=true;
    		char[] array = new char[s.length()];
    		
    		for(int i=0;i<s.length();i++){
    			if(Character.isLetter(s.charAt(i))){
    				System.out.print(s.charAt(i));
    			}else{
    				System.out.println("");
    				array[i]=s.charAt(i);
    				array = s.toCharArray();
    			}
    			if(check) {
    				   
    				   s.toUpperCase();		   
    			}
    				if(s.charAt(i)== ' '){
    					check=true;
    				}
    				else
    					check=false;
    		}
    		System.out.println(s.toUpperCase());
    
    	}
    }
    Last edited by Norm; 03-22-2012 at 04:56 PM. Reason: added code tags

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Convert some characters of a string to uppercase

    You did not answer my questioh: Describe how you are trying to solve the problem.

    How does your code get the characters one at a time?
    How does it determine if a character should be converted to uppercase?
    How does it test if that character is upper or lower case
    how does it change that character to uppercase?

    How does it create the result String using the characters it looked at one by one?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Convert some characters of a string to uppercase

    i also tried to do this:if the character is ' ' , i should call the toUpperCase method and make this character uppercase. but this doesnot work

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Convert some characters of a string to uppercase

    Post the code where you try to change the case of one character.
    Given this:
    char aChar = 'a';

    show how you would change the value of aChar to 'A'.

    You still have not answered this:
    Describe how you are trying to solve the problem.
    Last edited by Norm; 03-22-2012 at 04:57 PM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Convert some characters of a string to uppercase

    import java.lang.Character;

    char aChar = 'a';
    System.out.println(Character.toUpperCase(aChar));

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Convert some characters of a string to uppercase

    Now put it all together.
    Describe/list the steps the program must do to solve this problem.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Convert some characters of a string to uppercase

    import java.lang.String;
    import java.util.Scanner;

    public class Main{

    public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    String s = in.nextLine();


    for(int i=0;i<s.length();i++){
    s.toUpperCase();
    }
    System.out.println(s.toUpperCase());
    }

    }

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Convert some characters of a string to uppercase

    Don't write any code until you know what the code is supposed to do.
    Before you write any more code, respond to this:

    Describe/list the steps the program must do to solve this problem.

    See post#10 for some ideas
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Convert some characters of a string to uppercase

    This code that i wrote reads a string from the keyboard, then it converts the characters of the string to uppercase and finally it prints the string with the uppercase characters

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Convert some characters of a string to uppercase

    Is the problem solved then? Does the program do what you want it to do?
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    evaduli is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: Convert some characters of a string to uppercase

    no, if the problem was solved, i wouldn't be there.
    i want the first character of each word to be uppercase not all the characters of the string.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Convert some characters of a string to uppercase

    Quote Originally Posted by evaduli View Post
    no, if the problem was solved, i wouldn't be there.
    i want the first character of each word to be uppercase not all the characters of the string.
    Do you know how to convert a single character to upper case? (hint: see the Character class and its static methods)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with convert to uppercase program!
    By ultimashadow in forum New To Java
    Replies: 3
    Last Post: 01-31-2012, 09:49 AM
  2. Replies: 1
    Last Post: 09-17-2010, 12:40 PM
  3. Automatic convert to uppercase in keyevents
    By ashin in forum SWT / JFace
    Replies: 4
    Last Post: 08-09-2010, 01:59 PM
  4. Convert integers into ACSII characters.
    By Valkyrie in forum New To Java
    Replies: 4
    Last Post: 11-24-2009, 02:39 AM
  5. String uppercase/lowercase
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-19-2007, 09:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •