Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need Help in modifing String in Java

java string code programming uppercase

  • Please log in to reply
6 replies to this topic

#1 the876kat

the876kat

    CC Newcomer

  • Member
  • PipPip
  • 10 posts

Posted 23 October 2012 - 03:17 PM

Can an expert programmer help me with writing a JAVA code that converts every 2nd character in a string to upper case?
Please and thanks in advance.
  • 0

#2 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 23 October 2012 - 03:21 PM

You could use .split("") to "cut" it up into individual characters, and then go through a loop, changing ever other one to upper case, like so:
public void printUppercaseSecond(String s){
String[] cut = s.split("");
for(int i = 0; i < cut.length; i++){
	 if(i%2==0){
		 System.out.print(cut[i].toUpperCase());
	 }else{
		 System.out.print(cut[i]);
	 }
}
}

Or if you want the new String back:
public String secondToUppercase(String s){
String[] cut = s.split("");
for(int i = 0; i < cut.length; i++){
	 if(i%2==0){
		 cut[i] = cut[i].toUpperCase();
	 }
}
String toReturn = "";
for(int i = 0; i < cut.length; i++){
	 toReturn = toReturn+cut[i];
}
return toReturn;
}

Or you could use a StringBuilder to reconstruct string faster and more efficiently.
  • 0
Speaks fluent Java

#3 the876kat

the876kat

    CC Newcomer

  • Member
  • PipPip
  • 10 posts

Posted 23 October 2012 - 03:32 PM

Thanks alot ! I appreciate it..also can you help me write a code that will print the last four characters of the string?
  • 0

#4 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 23 October 2012 - 03:39 PM

A lot of the same concepts apply when trying to print the last 4 characters of a String. Try to write the program yourself and if you need help with a specific part, then you can ask your question here and we'll be glad to help. I usually don't encourage spoon feeding as it really doesn't help you.
  • 0

#5 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 23 October 2012 - 03:42 PM

Sure.
First, make sure the lenght of the string is at least 4 (or less, if thats what you want), then have it go through a loop that loops 4 times, one for each character, then rebuiled the string.

Like this:
public String getLastFour(String s){
   String[] cut = s.split("");
   String[] last4 = new String[4];
   String toReturn = "";
   if(cut.length>4){
	   for(int i = cut.length - 1; i > cut.length - 5; i--){ // -5 because I included the subtraction from cut's length (otherwise it would be out of bounds)
		    last4[i] = cut[i];
	   }
	   for(int i = 3; i > 0; i--){
		    toReturn = toReturn+last4[i];
	   }
   }else{
	   toReturn = s; // Already of or less than length 4
   }
   return toReturn;
}

  • 0
Speaks fluent Java

#6 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 23 October 2012 - 03:54 PM

I think you can do a substring to get the last 4, but check for null first, I don't think you need to split.


public String getLastFour(String s){
String[] cut = s.split("");

This function will throw exception if you send a null string.
  • 2

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#7 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 23 October 2012 - 03:57 PM

I think you can do a substring to get the last 4, but check for null first, I don't think you need to split.


public String getLastFour(String s){
String[] cut = s.split("");

This function will throw exception if you send a null string.

Hehehe, yeah. Forgot that. Thanks :)
  • 0
Speaks fluent Java





Also tagged with one or more of these keywords: java, string, code, programming, uppercase

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download