Two problems both with strings
Author |
Message |
gigaman
|
Posted: Mon Nov 12, 2007 11:22 am Post subject: Two problems both with strings |
|
|
I'm having two problems writing a program in java, both of which i'm sure have very simple solutions. the first is the the following for loop will not retrieve the character information from my variable temp. Trying to compile gives me an error saying
Error: C:\Users\Matt\Java\Spy.java:78: incompatible types
found : int
required: java.lang.String
Basically I need to take the characters in string temp and rearrange them, temp if a very long string and the characters are rearranged in sections 6 characters long. len is the integer length of temp/6, there is code after to rarrange the remaining section of text.
for (int i = 0; i <= len ; i++)
{
enPiece = temp.charAt((i*6+4)) + temp.charAt(i*6) + temp.charAt(i*6+5) + temp.charAt(i*6+1) + temp.charAt(i*6+3)+ temp.charAt(i*6+2);
enPass = enPass + "0" + enPiece;
}
second problem is that i will need to replace all of the characters # with nothing, i tried using string.replace('#','') but was given an error saying "Error: empty character literal"
Any help is appreciated as I am pretty new to java and haven't found a tutorial for either problem yet. If you'd like more background information on the program or need to see some more code just ask. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
HeavenAgain
|
Posted: Mon Nov 12, 2007 11:49 am Post subject: RE:Two problems both with strings |
|
|
have you decleared enPass or enPiece as int? if so thats the problem. or else, i dont see anything wrong with that for loop
for the 2nd problem you should try using
code: | String.replaceAll("#",""); |
|
|
|
|
|
|
gigaman
|
Posted: Mon Nov 12, 2007 12:46 pm Post subject: Re: Two problems both with strings |
|
|
Thanks, that was the issue with my replae function. My first problem though is still there. The vairable types of enPass and enPiece are both String, I guess for some reason java thinks the charAt function is returning a string instead of a character? |
|
|
|
|
|
HeavenAgain
|
Posted: Mon Nov 12, 2007 3:28 pm Post subject: RE:Two problems both with strings |
|
|
oh, okay i see the problem, charAt returns a character, but it can also return the value of the character, unicode or something, 65 is 'A', and 97 is 'a' and so on, so to make your coding forumla works, you gotta covert it into string, and by doing so....ummm
simply do this code: | enPiece = ""+temp.charAt((i*6+4)) + temp.charAt(i*6) + temp.charAt(i*6+5) + temp.charAt(i*6+1) + temp.charAt(i*6+3)+ temp.charAt(i*6+2); |
the lazy way out |
|
|
|
|
|
|
|