Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-25-2008, 08:54 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
What is the answer yo my problem with this code
this is the code
Code:
// this code should replace any occurence of the middle char with the nex char in unicode String str; int x,z,w; char y; System.out.print("Please Enter String \n"); str=console.next(); x=str.length(); y=str.charAt(x/2); // Now The problem that I want to get the next char in unicode str=str.substring(x/2, x); // I will use the replace method
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-25-2008, 09:47 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
There are two replace methods in the String class api (javadocs Method Summary). One takes single char arguments. This won't work because hex strings will likely be longer than a single char. The other method takes CharSequence interface arguments. Looking up the CharSequence interface we see it has four methods and is implemented by (look up top under "All Known Implementing Classes:" the String class. Therefore, String must implement the methods defined in the interface. So we can use the subSequence method to convert from String to CharSequence for the replace method.
Code:
public class Test { public static void main(String[] args) { String s = "hello"; System.out.println("s = " + s); int len = s.length(); int index = len/2; char c = s.charAt(index); int n = (int)c; String hex = Integer.toHexString(n); System.out.println("hex = " + hex); CharSequence target = s.subSequence(index, index+1); CharSequence replacement = hex.subSequence(0, hex.length()); s = s.replace(target, replacemment); System.out.println("s = " + s); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-25-2008, 10:32 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
thanks but i don't mean this
i want the program to do as the next example:
ex: abccc
the output should be abddd
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-25-2008, 07:11 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
String s = "abccc"; System.out.println("s = " + s); int len = s.length(); int index = len/2; char c = s.charAt(index); int n = (int)c; c = (char)(n+1); System.out.println("c = " + c); String target = s.substring(index, index+1); String replacement = String.valueOf(c); s = s.replace(target, replacement); System.out.println("s = " + s);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-26-2008, 08:33 AM
Member
 
Join Date: Mar 2008
Posts: 31
masaka is on a distinguished road
Thanks alot this what i need
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to handle each display/shell dialog box (not knowed) and choose default answer lenar SWT / JFace 0 04-04-2008 02:15 AM
Problem with code jvasilj1 New To Java 5 02-02-2008 10:34 AM
Problem with code oregon New To Java 3 08-05-2007 07:57 PM
Problem with zero in my code fernando New To Java 1 08-05-2007 08:39 AM
Problem with my first code paul New To Java 2 07-26-2007 06:09 PM


All times are GMT +3. The time now is 04:59 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org