package caesar;

import java.util.Scanner;

public class Caesar
{

public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	int key = scan.nextInt();
       String str = scan.next();


String encrypted = encrypt(str, key);
System.out.println(encrypted);

}
CAUSES 
public static String encrypt(String str, int key) {
String encrypted = " ";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c > 'Z')
c = c -26;
} else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c > 'z')
c = c -26;
}
encrypted += (char) c;
}
return encrypted;
}

}

Input
5
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
Output
IN
Why did not Output this.---->IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES

But

package caesar;

import java.util.Scanner;

public class Caesar
{

public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	
String str = "NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX";
int key = 5;

String encrypted = encrypt(str, key);
System.out.println(encrypted);

}

public static String encrypt(String str, int key) {
String encrypted = " ";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c > 'Z')
c = c -26;
} else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c > 'z')
c = c -26;
}
encrypted += (char) c;
}
return encrypted;
}

}

Output
IN =AR, E<ENTS OF IMPORTANCE ARE THE RESULT OF TRI<IAL CAUSES
Why did not Output this.---->IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
Please help me.

Comments
For starting two same threads

The first part isn't working properly because you are using scan.next(). That only returns the next token, using a space as a delimiter. Instead use scan.nextLine() to return the value. Like this:

Scanner scan = new Scanner(System.in);
int key = scan.nextInt();
scan.nextLine();
String str = scan.nextLine();

I had to use nextLine() twice because it wasn't blocking to allow me to input the text with just one.

The reason it isn't giving the correct output is because you're not checking for the correct char value. Instead of checking if it's greater than 'Z' check if it is less than 'E'. At least this is how I got it to work:

public static String encrypt(String str, int key) {
	String encrypted = "";
	for (int i = 0; i < str.length(); i++) {
		int c = str.charAt(i);
		if (Character.isUpperCase(c)) {
			if(c < 'E'){
				c = c + 26;
			}
			c = c - (key % 26);
		} else if (Character.isLowerCase(c)) {
			if(c < 'e'){
				c = c + 26;
			}
			c = c - (key % 26);
		}
		encrypted += (char) c;
	}
	return encrypted;
}

This is because subtracting from 'B' or 'A' is giving you a value that is not a letter, but is actually below the letter scope in chars. So, add 26 to increase it to above the max value for a letter and then do the subtraction.

Code tags. Please use them.

[code=JAVA] // paste code here

[/code]

or

[code]

// paste code here

[/code]


However, code tags are useless when the code is formatted the way you have formatted it. You need to indent in a way that blocks of code stand out. Here is your code indented properly.

With Java-specific code tags. Note indentation, line numbers and syntax highlighting.

package caesar;

import java.util.Scanner;

public class Caesar
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int key = scan.nextInt();
        String str = scan.next();

        String encrypted = encrypt(str, key);
        System.out.println(encrypted);
    }

    public static String encrypt(String str, int key)
    {
        String encrypted = " ";
        for (int i = 0; i < str.length(); i++)
        {
            int c = str.charAt(i);
            if (Character.isUpperCase(c))
            {
                c = c - (key % 26);
                if (c > 'Z')
                {
                    c = c - 26;
                }
            }
            else if (Character.isLowerCase(c))
            {
                c = c - (key % 26);
                if (c > 'z')
                {
                    c = c - 26;
                }
            }
            encrypted += (char) c;
        }
        return encrypted;
    }
}

Second code you posted (without Java-specific code tags. Indentation stays, but line numbers and syntax highlighting do not.)

package caesar;

import java.util.Scanner;

public class Caesar
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);

        String str = "NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX";
        int key = 5;

        String encrypted = encrypt(str, key);
        System.out.println(encrypted);

    }

    public static String encrypt(String str, int key)
    {
        String encrypted = " ";
        for (int i = 0; i < str.length(); i++)
        {
            int c = str.charAt(i);
            if (Character.isUpperCase(c))
            {
                c = c - (key % 26);
                if (c > 'Z')
                {
                    c = c - 26;
                }
            }
            else if (Character.isLowerCase(c))
            {
                c = c - (key % 26);
                if (c > 'z')
                {
                    c = c - 26;
                }
            }
            encrypted += (char) c;
        }
        return encrypted;
    }
}

You should point out the difference between code when you post the same class twice.

First of all, osiris003 you are wrong.
Second kuay you had a thread started, I posted an answer, you asked another question and then started a new one with same question. I already gave you an answer to your first thread but now I need to answer again in this new one. Why did you created a new thread with same question? That will get you a bad rep from me
As for the answer:
You are correctly subtracting the key instead of adding but:

Before:

c = c + (key % 26);
if (c > 'Z')
c = c - 26;
}

After:

c = c - (key % 26);
                if (c > 'Z')
                {
                    c = c - 26;
                }

Since you are subtracting, shouldn't change these as well: if (c > 'Z') and c = c - 26; Remember now you are subtracting so there is chance you will go "below" 'A'

Oh right... my apologies. Obviously it will only work for E if the key is 5. Thanks javaAddict!

Thank you everyone for good advice.
Ps.Thank you so much.
T_T i poor codeing;

This article has been dead for over six months. Start a new discussion instead.