7 Replies - 10552 Views - Last Post: 07 September 2013 - 01:35 PM Rate Topic: -----

#1 shellyb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-September 13

Trying to write code to encrypt input & get ASCII value

Posted 06 September 2013 - 05:03 AM

I've started a for loop to encrypt user input for any word and return the ASCII value of the user's input. i'm not sure how to write the actual loop to loop through the user's input and to print the ASCII value. I've tried to make the ascii value a variable and then calling it, but that isn't working.
Is This A Good Question/Topic? 0
  • +

Replies To: Trying to write code to encrypt input & get ASCII value

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Trying to write code to encrypt input & get ASCII value

Posted 06 September 2013 - 05:05 AM

Show what you've tried.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Trying to write code to encrypt input & get ASCII value

Posted 06 September 2013 - 11:06 AM

Yes, seeing what you've written would help. Also, please explain more clearly what it is you're trying to do. Specifically, what does "encrypt" mean here? What is the encryption process you want to apply?
Also, how do you want to calculate the ASCII value of a String? Presumably it's some combination of the ASCII values of the characters in the String, but what is the mode of combination supposed to be?


Quote

I've tried to make the ascii value a variable and then calling it, but that isn't working.


When you say "isn't working", please specify (1) what it would do if it were working and (2) what it's doing instead.
Was This Post Helpful? 0
  • +
  • -

#4 shellyb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-September 13

Re: Trying to write code to encrypt input & get ASCII value

Posted 06 September 2013 - 07:39 PM

what i'm trying to get the code to do is read user input, output the ASCII value, then ask user for decrypt text so i can return the ASCII value of the decrypt entry.
{
   public static void main(String[] args)
	   {
		  String temp = "";
		  int value;
                  char ch;

		   Scanner stdIn = new Scanner(System.in);
		   System.out.println("Please enter text to encrypt ");
		   String encrypt = stdIn.nextLine();
                   encrypt = encrypt.toUpperCase();
		   System.out.println("Please enter shift value ");
		   int shiftValueOne = stdIn.nextInt();
		   System.out.println(encrypt);

    for(int i = 0; i < encrypt.length(); i++)
	   {
		 ch = (char)(encrypt.charAt(i)+ (int)shiftValueOne);
		 temp += ch;
		 encrypt = temp;
		 System.out.println(temp);

		 if(encrypt.equals(""))
		 	 {
		 	   shiftValueOne = Integer.parseInt(encrypt) %26;
		 	 }
		 else
		 	 {
		 	   shiftValueOne = 0;
		 	 }
	   }


          System.out.println("Please enter text to decrypt ");
		  String decrypt = stdIn.next();
		  decrypt = decrypt.toUpperCase();
          System.out.println("Please enter shift value ");
          int shiftValueTwo = stdIn.nextInt();
          System.out.println(decrypt);

          for(int i = 0; i > decrypt.length(); i++)
		  	{
		  	  ch = (char)(decrypt.charAt(i)+ (int)shiftValueOne);
		  	  temp += ch;
		  	  decrypt = temp;
		  	  System.out.println(temp);

            if(decrypt.equals(""))
		   	  {
		   	    shiftValueTwo = Integer.parseInt(decrypt) %26;
		      }
	      else
		   	{
		   	  shiftValueTwo = 0;
		   	}
		  }

           System.out.println("Press any key to continue...");

This post has been edited by jon.kiparsky: 06 September 2013 - 07:40 PM
Reason for edit:: Deleted gratuitous double-post

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Trying to write code to encrypt input & get ASCII value

Posted 06 September 2013 - 07:40 PM

Yes, but what do you mean by the "ASCII value" of a String? chars have ASCII values, Strings do not. And what do you mean by "encrypt" and "decrypt"? If you can't say precisely what you're trying to do, you can't do it except maybe by accident, and surely nobody can help you do it unless you tell them what you need help with. Spell out what it is you're trying to do.
Was This Post Helpful? 0
  • +
  • -

#6 shellyb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-September 13

Re: Trying to write code to encrypt input & get ASCII value

Posted 06 September 2013 - 07:52 PM

View Postjon.kiparsky, on 06 September 2013 - 07:40 PM, said:

Yes, but what do you mean by the "ASCII value" of a String? chars have ASCII values, Strings do not. And what do you mean by "encrypt" and "decrypt"? If you can't say precisely what you're trying to do, you can't do it except maybe by accident, and surely nobody can help you do it unless you tell them what you need help with. Spell out what it is you're trying to do.




i have attached the assignment requirements...hope this helps explain what i need assistance with.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Trying to write code to encrypt input & get ASCII value

Posted 06 September 2013 - 08:22 PM

You're still missing the point: if you can't say it, you probably can't do it.


Here, you're clearly trying to execute an intermediate step. The requirements say nothing at all about printing out the "ASCII value" of a String - you want this output, you have to say what it means. Hint: I know what you're looking for, it's just going to be a lot easier for you to figure it out if you spell out what you're trying to do.
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Trying to write code to encrypt input & get ASCII value

Posted 07 September 2013 - 01:35 PM

Your for loop will not work

Assume you enter "abcd"

		  String temp = "";     // temp length is 0 here

// starting loop with i = 0 to "ABCD" length that is
    for(int i = 0; i < encrypt.length(); i++)
	   {
		 ch = (char)(encrypt.charAt(i)+ (int)shiftValueOne);
		 temp += ch;       // temp is now what ever the conversion did
		 encrypt = temp;   // encrypt = temp so its length is now 1

		 System.out.println(temp);

	   }  // next iteration of the loop i  i + 1 so i = 1 ... encrypt.length == 1 so done we exit the loop


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1