Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have been facing a problem with the following program:
Write a program to accept a sentence and print it in the following encoded format:
Sample Input: abc def xyz
Sample Output: bcd efg yza


It has compiled perfectly and gives the required output for characters from 'a' to 'y'.
Both running and BLOCK characters work.

However in case of'z' or 'Z',it does not return 'a' or 'A',
it gives '{' or '[' which is 1 after them in the ASCII system.
Please Help me to fix this issue.

Regards,
Chrono

What I have tried:

This is my attempt:

Java
import java.util.*;
class Character
{
    public static void main(String[]args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a sentence");
        String s=sc.nextLine(),s3="";
        char s2;
        int l=s.length();
        for(int i=0;i<l;i++)
        {
        char s1=s.charAt(i);
            if(s1!=' ')
            {
               s2=(char)((int)s1 +1);
               {
               if(s2=='z'||s2=='Z')
               {
               s2=(char)((int)s2 -25);
            }
            else
               { 
                   s2=s2;
                }
            }      
            }
           else
            {
                 s2=s1;

        }
        s3=s3+s2;
    }
        System.out.println(s3);
    }
}
Posted
Updated 23-Dec-17 4:10am

1 solution

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Java
import java.util.*;
class Character
{
  public static void main(String[]args)
  {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter a sentence");
    String s=sc.nextLine(),s3="";
    char s2;
    int l=s.length();
    for(int i=0;i<l;i++)
    {
      char s1=s.charAt(i);
      if(s1!=' ')
      {
        s2=(char)((int)s1 +1);
        {
          if(s2=='z'||s2=='Z')
          {
            s2=(char)((int)s2 -25);
          }
          else
          {
            s2=s2;
          }
        }
      }
      else
      {
        s2=s1;
      }
      s3=s3+s2;
    }
    System.out.println(s3);
  }
}

Pay attention to this re-indented code, it don't look like in your question, but it is the same code.
Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
-----
A sentence can have commas and points that are not handled by your code.
Use the debugger and test the code behavior with each letter, especially with y, z and non letters chars.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900