Results 1 to 6 of 6
  1. #1
    dantzig is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default The code is compiling but it is not giving the expected output.

    Java Code:
    import java.util.Scanner;
    
    
    public class ProjectTools {
    	public static void main(String[] args){
    		
    	        Scanner scan = new Scanner(System.in);
    	        System.out.println("Enter the key");
    			int key = scan.nextInt();
    		
    			System.out.println("Enter the text");
    			String text = scan.next() ;
    			String x = "aa";
    			
    			String ciphertext = encrypt( text , key);
    			System.out.println("ciphertext" + ciphertext);
    		}
    	private static String encrypt(String str , int key){
    		
    		if (key < 0)
    		{
    			key = 26-(-key%26);
    		}
    		String result = "" ;
    		for (int i = 0 ; i<str.length() ; i++){
    			char ch = str.charAt(i);
    			result+= encryptchar(ch, key);
    			
    		}
    		       return result;
    	}
      private static char encryptchar(char ch , int key){
    	   if (Character.isUpperCase(ch))
    	   {
    		 return ((char) ( 'A'+ ((ch-'A'+key)%26)));
    		   
    	   }
    	   return ch ;
    	
    	  
      }
    		
    	}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: The code is compiling but it is not giving the expected output.

    You forgot to tell us what output you expect, and what you get.

    Also, go through Code Conventions for the Java Programming Language: Contents -- your indenting is all over the place.


    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    dantzig is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: The code is compiling but it is not giving the expected output.

    input:
    aaa
    output:
    aaa
    but i expect from my code ddd.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: The code is compiling but it is not giving the expected output.

    Describe what you expect your encryptchar(char ch , int key) to return when ch isn't an uppercase letter.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    dantzig is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: The code is compiling but it is not giving the expected output.

    I figure that out.
    Thank you.

  6. #6
    dantzig is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: The code is compiling but it is not giving the expected output.

    The following line should be ( 'A'+ ((ch-'A'+key)%26)));

    ( 'A'+ ((-6 +ch-'A'+key)%26))); if you plug it in the code it will run, and you can try different inputs.
    Thanks.

Similar Threads

  1. Why is out.println giving me a compiling error?
    By dragstang86 in forum New To Java
    Replies: 5
    Last Post: 06-20-2011, 03:37 AM
  2. output giving null, methods not reading properly
    By csuever in forum New To Java
    Replies: 22
    Last Post: 04-01-2011, 03:13 AM
  3. output not as expected.What's wrong?
    By abdullahansari in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 09:01 PM
  4. Replies: 2
    Last Post: 12-31-2009, 01:45 PM
  5. output not what expected!
    By aza101 in forum Java Applets
    Replies: 0
    Last Post: 07-10-2009, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •