## Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

• Help others & share knowledge
• Earn cash & points
Solved

# character manipulation of String variable

Posted on 2000-05-16
427 Views
Hi Folks,

i have two strings, say string1 and string2

what i want to do is to transform the first string by adding the ascii values of the relative characters to create a string of integers:

string1 = "abcd" (ascii 97, 98, 99, 100)
string2 = "defg" (ascii 101, 102, 103, 104)

transformed: 198%10, 200%10, 202%10, 204%10

string23 = "8024"

apologies if this is confusing, but the basic gist is that i want to know what processes are involved in manipulating strings in this manner:

turn a character into decimal, turn a decimal into character.

in c i would do it like:

char string3[4]

string[0] = 8+42
string[1] = 0+42

etc...

any pointers welcome! ;-)

thanks and regards.

0
Question by:meverest
• 12
• 9

LVL 3

Expert Comment

ID: 2814159
The easiest way might be to use the String method toCharArray to get an array of the characters in the String.  You can then get the numeric ascii value of each character using the static method in the Character class:

char [] chars = string1.toCharArray();

Character.getNumericValue(chars[0]); //returns unicode value of the character at this position (ie. ascii code for regular ascii characters)

This should be the tools you would use to get the numeric values, which you can manipulate as needed.

Laminamia :)
0

LVL 3

Expert Comment

ID: 2814181
Oops, forgot: To convert the integer back to a string:

Laminamia :)
0

LVL 3

Expert Comment

ID: 2814190

Good luck again,

Laminamia :)
0

LVL 37

Author Comment

ID: 2815125
thanks will try it out today.

cheers.
0

LVL 37

Author Comment

ID: 2815258
hi,

there does not appear to be any such member "getNumericValue()" of class Character (?)

cheers.
0

LVL 37

Author Comment

ID: 2815394
Adjusted points from 150 to 200
0

LVL 37

Author Comment

ID: 2815395
i also tried character.value() but these do not appear to return the correct integer value.

how can i get the ascii value of the given character?

0

LVL 3

Expert Comment

ID: 2815426
The call is:

Character.getNumericValue('a'); //or some other character.

Try that and it will return the ascii code as shown above.

I hope it works!

Drop another comment if you have trouble.

Laminamia :)
0

LVL 3

Expert Comment

ID: 2815428
See the jdk documentation on java.lang.Character:

getNumericValue

public static int getNumericValue(char ch)

Returns the Unicode numeric value of the character as a nonnegative integer. If the character does not have a numeric
value, then -1 is returned. If the character has a numeric value that cannot be represented as a nonnegative integer (for
example, a fractional value), then -2 is returned.
Parameters:
ch - the character to be converted.
Returns:
the numeric value of the character, as a nonnegative int value; -2 if the character has a numeric value that is not a
nonnegative integer; -1 if the character has no numeric value.
Since:
JDK1.1
forDigit(int, int), isDigit(char)

Good luck!

Laminamia :)
0

LVL 3

Expert Comment

ID: 2815436
Here's a little example using the getNumericValue(char a)

Since it's a class method (static) it is called from the class Character (not called on an instance) and a char primitive (i.e. not an object, but a regular ole char) is passed to it.

import java.io.*;

public class B {
public static void main(String [] args){
char a = 'a';
System.out.println(a + ": " + Character.getNumericValue(a));
}
}

Laminamia :)
0

LVL 3

Expert Comment

ID: 2815439
Oh, forgot something:

Remember to make the Character in that call capitalized, as you are calling on the class.  If you call character.someMethod() it probably won't work.

Good luck!

Laminamia ;)
0

LVL 37

Author Comment

ID: 2815681
i still get a compiler error
D:\programming\jfx_camtech\cfx_camtech.java(37,45) : error J0072: 'getNumericValue' is not a member of class 'Character'

- i am using M\$s visual j++ studio v5 - maybe this is the problem... M\$ specific code, wouldn't surprise me in the least :(

cheers.
0

LVL 37

Author Comment

ID: 2815682
sorry to keep rejecting, but it is not working for me :(
0

LVL 3

Expert Comment

ID: 2816653
Please post the code for that line :)  If VJ++ has followed the Java Language Specification for the class Character, it will be there.

Laminamia :)
0

LVL 3

Accepted Solution

Laminamia063099 earned 200 total points
ID: 2816760
MEverest:

The following code will do what you need to.  Sorry I led you astray in terms of the converting to ascii code.  Actually all you have to do to get the ascii value is cast the character to an integer.  I've used the modulus function found in BigInteger in this example.  If that part is confusing, drop a note.  Run the following code and you'll get the same result as specified originally.

I've set this up to run for the length of the smaller string.  It also outputs the ascii codes for each character (just my debugging).

import java.math.BigInteger;

public class Test {
public static void main (String [] args){
String str1 = "abcd";
String str2 = "efgh";
String str3 = "";  //this will be the new string
for (int i = 0; i < (Math.min(str1.length(), str2.length())); i++){
char c1, c2;
//get the relative characters
c1 = str1.charAt(i);
System.out.println(c1 + ": " + (int)c1);
c2 = str2.charAt(i);
System.out.println(c2 + ": " + (int)c2);
int sum = (int)c1 + (int)c2; //casting the characters to ascii codes and adding
//using modulus built into BigInteger class (not the only way to go, but it works)
BigInteger modulus = (BigInteger.valueOf(sum).mod(BigInteger.valueOf(10)));
str3 = str3 + modulus.toString(); //appending the new string
}
System.out.println("Result String: " + str3);
}
}

Good luck, and sorry for going off the wrong way.

Laminamia :)
0

LVL 37

Author Comment

ID: 2816926
>Actually all you have to do to get the ascii value is cast the character to an integer.

that's all i needed - <g> does it annoy you too when the answer is just staring you in the face! ;-)

thanks, now i have it working
nicely.optional question:  what does java.math.BigInteger do?

cheers.

0

LVL 3

Expert Comment

ID: 2817070
java.math.BigInteger is a class.  It is used to represent infinitely large integers.

I've used it here because the class has a method mod() for calculating the modulus of the BigInteger object with another number.
My code there could be split the following way:

//Note: the method valueOf(int x) in BigInteger is a factory method,
//i.e. it creates instances of BigInteger.  This method in case creates
//BigInteger objects with the value of the primitive integer passed.
//It is used to create BigInteger's for your sum amount and for
//your modulus result, and for the value of 10 in calculating the mod.

BigInteger modulus; //declare mod result variable

//this creates a BigInteger object with the value of sum
BigInteger sum = BigInteger.valueOf(sum); //declare sum variable

//This creates a BigInteger with the value of 10 for the modulus:
BigInteger int10 = BigInteger.valueOf(sum);

//now calculate the modulus of sum by int10 (sum % 10) by
//calling the method mod in BigInteger
modulus = sum.mod(int10);

I know for anyone coming from a non-object oriented background this can be a little overwhelming.  If this is the case, drop another note with your questions and I'll help you out ;)

Laminamia :)
0

LVL 37

Author Comment

ID: 2819348
> I know for anyone coming from a non-object oriented background this can be a little overwhelming.  If this is the case, drop another note with your questions and I'll help you out ;)

not at all!  most intriguing....

only problem is that when i try to import java.math.biginteger, i get an object not found error.

i am finding many discrepancies with this java builder environment (MS visiual J++ studio v5.0) and makes me very worried about the portability of this code.

do you know whether this is going to be an issue for cross platform portability? if it is not going to work unless a specific implementation of the jvm, then i may as well give up now! ;-)

cheers.
0

LVL 3

Expert Comment

ID: 2820797
> only problem is that when i try to import java.math.biginteger, i get an

I don't know if this is your problem, but Java is very case sensitive.  Did you try:
import java.math.BigInteger;

If so, and it still doesn't work, MS does it again.

When it comes to cross platform compatibility, you could very well be looking at problems.  It will probably provide you with the ability to package up any classes that your application needs so that it MAY be portable, but you'd be much safer if you went with something a little more standard.

There comes the big question: What is more standard?

The only pure standard is if you use the JDK for development.  But then you lose the ability to compose GUI's with drag and drop.  Most other GUI building tools will have their own custom packages, but they will support the complete set of classes in the Java Language Specification.

I'm not a Microsoft fan, but if you don't have too much invested in the VJ++, I would suggest shopping around.

Good luck!

Laminamia :)
0

LVL 37

Author Comment

ID: 2823687
Thanks Laminamia,

yes, i did try the appropriate case, and no-go :(

also, i am no big fan of MS either, but i have this j++ studio as it was packaged with C++ studio when i purchased it, so naturally it is the first one i tried.

i also have the sun jdk 1.2.2, and i use the sun jvm to run my classes, but i cannot get the MS builder to use these libraries instead! <grrr>

i don't care much about the GUI stuff, since i am writing server side applications anyway.

if i have trouble with portability, i will just use the javac.exe with the jdk.

regards,  Mike.

0

LVL 3

Expert Comment

ID: 2823866
Cheers.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question