Help with simple program
Hello, all.
I have to make a simple program , here is what i need to do:
Input : ABC
Output : BCD
Input : EFG
Ouput : FGH
Input : zzz
Output : aaa
Input : ZaB
Output : AbC
(i.e i have to print the alphabet after the one entered, and if it is Z, print an A)
The program is simple enough, but i just cant seem to figure out how to fix a bug:
Basically , my problem is that it prints the ASCII values of the next characters, but not the characters themselves, for example on entering "ABC" the output will be 666768 (66- B 67-C 68-D)
Heres the code:
publicclass ABCtoBCD
{
publicvoid convert(String str)
{
for (int i=0;i<=str.length()-1;i++)
{
if (str.charAt(i)==90)
{
System.out.print("A");
}
elseif (str.charAt(i)==122)
{
System.out.print("a");
}
else
{
System.out.print(str.charAt(i)+1);
}
}
}
}
[1705 byte] By [
Overkilla] at [2007-11-27 9:38:16]

Currently, you're adding an integer to the character, which causes the result to convert to an integer. Cast the result to char, that is, substitute the last actual line of code with System.out.print((char) (str.charAt(i)+1));
There's nothing to parse
I'm in a helpful mood. Have some code
for (int i=0;i<=str.length()-1;i++)
{
if (str.charAt(i)==90)
{
System.out.print("A");
}
else if (str.charAt(i)==122)
{
System.out.print("a");
}
else
{
char c = str.charAt(i);
c++;
System.out.print(c);
}
}
Although I would say that just because you haven't been taught about casting yet, doesn't mean you shouldn't use it. When taking a course, it's expected that you will do some learning on your own - and you've learnt a bit about casting. You could use it, might even earn you credits, or brownie points. It's up to you, though, you know the course and what might or might not be appropriate better than I do
Anyways, you've got a couple of ways to do this. Go with whicheve r you think is best
> Actually its not a course , im in school ^^ (10th
> std)
> Ill ask my teacher if its alright otherwise ill use
> the way you just posted, shouldve thought of that :(
For extra marks, submit both. It's worth bearing in mind that you might never be taught about casting. We weren't taught it on my University course, bizarrely