0

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

5
Contributors
15
Replies
16
Views
6 Years
Discussion Span
Last Post by jwenting
0

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'

0

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

0

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

0

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

0

The simple method

String.replaceAll()

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

0

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);
0

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

i realize that , and i want a solution

0

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

it worked , thanks man

0

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???

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.