Need some help with programming (Java).

Post » Mon Jan 20, 2014 11:25 pm

I'm in an intro to Java class right now and we were just asked to create a encryption program that would take an argument and return it as an encrypted message.

What I wanted to do was take two command line arguments. The fist one would be the word or message that would be encrypted, and the second would be the integer value used to determine the encryption.

It would take the first argument and convert it to a character array and it would take the second argument and change the string to an integer.

It would then have a loop that would continue to loop until it had looped through the whole character array once. Each character would, in theory, be assigned a new encrypted character by adding a certain value to it and reassigned it in the array.

After every character has been encrypted then the program would print out the encrypted string.

So far this is what I have:

public class cypherCaesar{      public static void main(String args[]){             String message = args[0];               char arr[] = message.toCharArray();                 String cypher = args[1];                        int  cypherVal = Integer.parseInt(cypher);               int c = 0;                             for (int i = 0; i < arr.length; i++) {                     arr[(0 + c)] = arr[(0 + c)] + (cypherVal%26);                  c++;                                               }             String encryptedMessage = new String(arr);              System.out.println(encryptedMessage);           }}

And this is the error I get back when I try to compile it:

cypherCaesar.java:11: error: possible loss of precision                        arr[(0 + c)] = arr[(0 + c)] + (k%26);                                                      ^  required: char  found:    int1 error

I know it's probably something really small that I'm looking over. Does anyone know how I can fix this?

User avatar
Andrew Tarango
 
Posts: 3454
Joined: Wed Oct 17, 2007 10:07 am

Post » Tue Jan 21, 2014 11:36 am

this is probably wrong since i'm not completely familiar with java so i may be missing something important, but in that equation, arr[(o + c)] is a char and cypherVal%26 is an integer so you definitely cannot just add them together

since you are adding it back into arr[(0 + c)] i assume you want it to remain a char value so you would need to convert cypherVal%26 into a char value

User avatar
Naomi Lastname
 
Posts: 3390
Joined: Mon Sep 25, 2006 9:21 am

Post » Tue Jan 21, 2014 10:45 am

You need to cast the result back into a char as the addition you perform means you get an int instead.
There are other problems with your program but I'm on mobile and can't bother to type too much.
User avatar
Taylor Thompson
 
Posts: 3350
Joined: Fri Nov 16, 2007 5:19 am

Post » Tue Jan 21, 2014 9:41 am

Just noticed that as I was reading back through the post. :P

And a lot of the other things like c is pretty much equal to i. I'm getting there slowly but surely haha.

User avatar
Heather M
 
Posts: 3487
Joined: Mon Aug 27, 2007 5:40 am

Post » Tue Jan 21, 2014 2:06 pm

Try/catch the ParseInt as well.
User avatar
barbara belmonte
 
Posts: 3528
Joined: Fri Apr 06, 2007 6:12 pm

Post » Tue Jan 21, 2014 12:40 am

EDIT: Stupid forum eliminated half of my post and left me with just a code block. Oh well. Let's try this again.

for (int i = 0; i < arr.length; i++) {     arr[(0 + c)] = arr[(0 + c)] + (cypherVal%26);       c++;}

This is needlessly complex. Three things:

Why are you keeping a second variable as your array index (int c) when you could just be using the same one in your for() statement (int i)?

Why are you using the assignment operator on your array when you could be using add and assign (+=)?

What's the point of the (0 + c) expression anyway? Isn't that exactly the same thing as c?

Try this:

for (int i = 0; i < arr.length; i++) {          arr[i] += cypherVal%26;}

This isn't perfect, you still need to typecast (cypherVal%26) as a (char) or else the compiler will complain about a "loss of precision". But it's a start.

User avatar
djimi
 
Posts: 3519
Joined: Mon Oct 23, 2006 6:44 am

Post » Tue Jan 21, 2014 1:27 am

I'd also use an array of byte instead of an array of char.

User avatar
Jacob Phillips
 
Posts: 3430
Joined: Tue Aug 14, 2007 9:46 am


Return to Othor Games