Results 1 to 5 of 5
  1. #1
    cedric11 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default Need help with output

    Hey, anything in Java so I do not know how a code that I ran in c + + rewrite to me in Netbeans throw errors, but when running the advantage of me:
    "Exception in thread" main "java.lang.ArrayIndexOutOfBoundsException: 1
    at semestralka.Main.main (Main.java: 32)
    Java Result: 1 "

    Could someone look at it and tell me, where's my mistake?

    semestral package;

    import java.util.Scanner;

    public class Main (

    public static int shift (int character, int number) (
    if (character <91) (
    character = (char) (((char - 65) + number + 26)% 26 + 65);
    ) Else (
    character = (char) (((char - 97) + number + 26)% 26 + 97);
    )
    return (char);
    )

    public static void main (String [] args) (
    Scanner s = new Scanner (System.in);
    char [] vetoes = new char [256];
    char [] cipher = new char [256];
    char [] key = new char [256];
    char [] desifra = new char [256];
    System.out.println ( "Enter the text you want to encrypt:");
    String string1 = s.nextLine ();
    veto retezec1.toCharArray = ();
    System.out.println ( "Enter a one-keys that you want the text to encrypt:");
    String string2 = s.nextLine ();
    key = retezec2.toCharArray ();

    int n = 0;
    int i;
    for (i = 0; i <257; i + +) (
    if ((key [i]> 64 & & key [i] <91) | | (key [i]> 96 & & key [i] <123)) (
    n + +;
    ) Else (
    break;
    )
    )
    int number;
    int m = 0;
    for (i = 0; i <257; i + +) (
    if ((sentence [i]> 64 & & sentence [i] <91) | | (sentence [i]> 96 & & sentence [i] <123)) (
    if (key [m] <91) (
    number = key [m] - 64;
    ) Else (
    number = key [m] - 96;
    )
    if (m <(n - 1)) (
    m + +;
    ) Else (
    m = 0;
    )
    cipher [i] = (char) shift (sentence [i], how many);
    ) Else (
    cipher [i] = sentence [i];
    )
    )
    System.out.printf ( "encryption string is:% n% n% s", cipher);

    m = 0;
    for (i = 0; i <257; i + +) (
    if ((cipher [i]> 64 & & cipher [i] <91) | | (cipher [i]> 96 & & cipher [i] <123)) (
    if (key [m] <91) (
    number = (-1) * (key [m] - 64);
    ) Else (
    number = (-1) * (key [m] - 96);
    )
    if (m <(n - 1)) (
    m + +;
    ) Else (
    m = 0;
    )
    desifra [i] = (char) shift (cipher [i], how many);
    ) Else (
    desifra [i] = cipher [i];
    )
    System.out.printf ( "deciphered string is:% n% n% s", desifra);

    )
    )
    )

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Oh my, what are all those magic numbers doing in your code? If you want to say 'A', don't say 65 etc. also 'else' is written all lowercase; your compiler must've whined about it; look at the error message: somewhere on line #32 something went wrong, you used an index value 1 and it was out of the bounds of an array. What I don't understand is how did you manage to run your code while it doesn't even compile?

    kind regards,

    Jos

  3. #3
    cedric11 is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    I wrote this code in c++ a few years ago and I used ASCII. I thought, that it's work in Java...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    C++ != Java.
    Some syntax similarities aside, trying to simply turn a C++ program into a Java one without actually doing some proper rework on it will almost always fail.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by cedric11 View Post
    I wrote this code in c++ a few years ago and I used ASCII. I thought, that it's work in Java...
    If you want to port that C++ code to an IBM dinosaur machine you'll be in big trouble because that beast uses EBCDIC code instead of ASCII. If you had used, e.g. 'A' for 65 you're leaving the job to your compiler because it has to translate that symbolic literal to a machine recognizable number.

    You're lucky that ASCII is a proper subset of Unicode so it'll work in Java, but I consider it just luck. It is a bad habit, not portable and hardly readable.

    kind regards,

    Jos

Similar Threads

  1. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  2. how to c the output
    By pro85 in forum Java Applets
    Replies: 1
    Last Post: 02-11-2009, 10:09 AM
  3. different output
    By pro85 in forum Java Applets
    Replies: 6
    Last Post: 08-05-2008, 02:42 PM
  4. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 11:14 PM
  5. Why the output is always zero
    By mehrotra.chitij in forum New To Java
    Replies: 12
    Last Post: 04-25-2008, 05:05 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
  •