i want to write a program that changes all the 'a' charachters to 'v'
but the problem instead prints all the message as vvvvvvvvv
here is the code

class operation
{
public String encode (String args)
{
 StringBuilder mago = new StringBuilder();
        char a,  b;
        b='a';
         for (int i=0; i<args.length(); i++)
        {
            a=Character.toLowerCase(args.charAt(i));
          if (Character.isLetter(a))
          {      a='v';
          }
        
       b=a;
  mago.append(a);
        }
    
           
    	
         return mago.toString(); 
    }
}
        }

the main class

public class trial {
    
    public static void main(String[] args) {
    	
    	// TODO, add your application code
    // To read a sentence from the user
Scanner jack = new Scanner(System.in);
operation z= new operation();
System.out.println(z.encode("i am a programmer"));
}
}

plz help me

Line 11 I think you misunderstand the isLetter method. It returns true if a is a letter (as opposed to a number or a punctuation symbol) so you are replacing every letter with 'v'. You should be testing whether the char is equal to 'a'

i used this
if (b.equals('a'))
but the progam says char cannot be dereferenced

now i replaced the char with Charachter , but the programm outputs "v av avprogravmer" instead of "i vm v progrvmmer"

chars are primitive numeric types. Unlike Strings, you can compare them with ==

stills the same problem it outputs "v av avprogravmer" instead of "i vm v progrvmmer"

The simple method

String.replaceAll()

would solve your problem with a single line of code. Look it up in the API documentation.

well i fixed the problem , but i faced another problem when i tried to make the program change various charachters.
it outputs "d uh u mqkbquhhjq" instead of "d vh v mqkbqvhhnq"
i guess there is a problem with the equality thing , but i dunno how 2 fix it , plz help me
here is the class

public String encode (String args)
{
 StringBuilder mago = new StringBuilder();
        char a;
        
         for (int i=0; i<args.length(); i++)
        {
   a=Character.toLowerCase(args.charAt(i));
          if (a=='a')
          {      a='v';
          }
  if (a=='b')
          {      a='i';
          }
   if (a=='c')
          {      a='l';
          }
    if (a=='d')
          {      a='a';
          }
    if (a=='e')
          {      a='n';
          }
   if (a=='f')
          {      a='o';
          }
   if (a=='g')
          {      a='b';
          }
     if (a=='h')
          {      a='c';
          }
    if (a=='i')
          {      a='d';
          }
    if (a=='j')
          {      a='e';
          }
   if (a=='k')
          {      a='f';
          }
    if (a=='l')
          {      a='g';
          }
    if (a=='m')
          {      a='h';
          }
    if (a=='n')
          {      a='j';
          }
    if (a=='o')
          {      a='k';
          }
    if (a=='p')
          {      a='m';
          }
    if (a=='q')
          {      a='p';
          }
   if (a=='r')
          {      a='q';
          }
   if (a=='s')
          {      a='r';
          }
    if (a=='t')
          {      a='s';
          }
  if (a=='u')
          {      a='s';
          }
  if (a=='v')
          {      a='u';
          }
  if (a=='w')
          {      a='w';
          }
  if (a=='x')
          {      a='x';
          }
  if (a=='y')
          {      a='y';
          }
  if (a=='z')
          {      a='z';
          }


   mago.append(a);

You do realize you are potentially reassigning the value of a many times, right?

You do realize you are potentially reassigning the value of a many times, right?

i realize that , and i want a solution

Perhaps you should read up on the 'else' statement.

Perhaps you should read up on the 'else' statement.

it worked , thanks man

hey , In your code , what if you want the user to enter a sentence to encode ?

hey , In your code , what if you want the user to enter a sentence to encode ?

sorry I found the answer : by adding a scanner and then print the user input after changing letters.
but
would you mind show us the final solution???

Start your own thread if you have questions about your homework.

Perhaps you should read up on the 'else' statement.

no, he should figure out what he's trying to do and do it. The structure he came up with is rubbish, with or without else statements, and should get him a major fail if he submitted it to his teachers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.