|
-
October 9th, 2009, 11:30 AM
#1
Arrays
Hi.. Why is it that when i print the output would show all those junk at the bottom how do i make them print properly in chars? The idea is to printout this table below
Code:
import java.util.Scanner;
public class CiphersApp{
public static void main(String[]args){
Ciphers ciphernew=new Ciphers();
int i,j;
Scanner sc=new Scanner(System.in);
for(i=0;i<27;i++)
{
for(j=0;j<27;j++)
System.out.print(ciphernew.lookupTable+" ");
System.out.println();
}
Output
Code:
[[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb
[[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb
[[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb
[[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb [[C@14318bb
Last edited by hugo84; October 10th, 2009 at 05:04 AM.
-
October 9th, 2009, 11:33 AM
#2
Re: Arrays
This is because Arrays are objects, even Arrays of primitive types. In order to print the individual elements you need to access them individually. Such as:
ciphernew.lookupTable[i][j]
-
October 9th, 2009, 01:26 PM
#3
Re: Arrays
hmm ok in this case how am i going to print this out in this format
Code:
a b c d e f g h i j k l m n o p q r s t u v w x y z
a A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
b B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
c C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
...................................
...................................
...................................
z Z A B C D E F G H I J K L M N O P Q R S T U V W X Y
-
October 9th, 2009, 01:56 PM
#4
Re: Arrays
hmm ok in this case how am i going to print this out in this format
Exactly as ProgramThis has already said by accessing each individual element. If its a 2D array you can use a nested loop to iterate through the array printing out each element (or appending it to a StringBuilder) as you go.
An alternate way is:
The java.util.Arrays class has a utility method for converting multi-dimension arrays to a String. If the output of Arrays.deepToString(myArray) is in the format you require you can use this instead.
-
October 10th, 2009, 08:23 AM
#5
Re: Arrays
Why is this giving me arrayindexoutofboundsexception??
Code:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
H I J K L M N O P Q R S T U V W X Y Z A B C D E F G
I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
J K L M N O P Q R S T U V W X Y Z A B C D E F G H I
K L M N O P Q R S T U V W X Y Z A B C D E F G H I J
L M N O P Q R S T U V W X Y Z A B C D E F G H I J K
M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
O P Q R S T U V W X Y Z A B C D E F G H I J K L M N
P Q R S T U V W X Y Z A B C D E F G H I J K L M N O
Q R S T U V W X Y Z A B C D E F G H I J K L M N O P
R S T U V W X Y Z A B C D E F G H I J K L M N O P Q
S T U V W X Y Z A B C D E F G H I J K L M N O P Q R
T U V W X Y Z A B C D E F G H I J K L M N O P Q R S
U V W X Y Z A B C D E F G H I J K L M N O P Q R S T
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
Z A B C D E F G H I J K L M N O P Q R S T U V W X Y
Enter the number of your choice:1
Enter a line of plain text:asad
plaintext = asad
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26
Code:
do{
System.out.print("Enter the number of your choice:");
choice=sc.nextInt();
switch(choice){
case 1:
//cipher1.encrypt();
System.out.print("Enter a line of plain text:");
String plaintext=sc.next();
System.out.println("\tplaintext = "+plaintext);
System.out.println("\tciphertext = "+cipher1.encrypt(plaintext));
break;
case 2:
cipher1.decryptText();
break;
case 3: System.out.println("Program terminating...");
}
}
while (choice!=3);
}
Last edited by hugo84; October 15th, 2009 at 07:11 AM.
-
October 10th, 2009, 08:44 AM
#6
Re: Arrays
hi, check your code again..it also say where the exception appears..
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
ArrayIndexOutOfBoundsException
Please use code tags like this : [SIGPIC][/SIGPIC]
Code Your Dreams 
-
October 11th, 2009, 01:31 PM
#7
Re: Arrays
to me the problem appears to be in this line of code:
Code:
while (i<plaintext.length())
the nature of arrays, and Strings, and ArrayLists is such that the index values start at 0. From your code I gather that your array size is 26 (for 26 letters in the alphabet, I presume). The .length() method returns the integer 26 which is not an accessible index of the array because it's indices go from 0 to 25. try modifying it like so:
Code:
while (i<plaintext.length() - 1)
that will make the loop look to the last index and not beyond, and should solve you problem of the out of bounds
-
October 11th, 2009, 02:04 PM
#8
Re: Arrays
The condition i < plaintext.length() guarantees that i will never get further than plaintext.length(), so the -1 is not needed.
I think the problem might be (based only on the little fragment of code posted) in one of the other indexes, j or k, which are incremented but never reset within the outer loop. For a better diagnostic (and if that is not the problem) please post the complete code and the stacktrace of the exception (that will say in which line of your code the error occurs).
-
October 12th, 2009, 07:41 AM
#9
Re: Arrays
 Originally Posted by jcaccia
... I think the problem might be (based only on the little fragment of code posted) in one of the other indexes, j or k, which are incremented but never reset within the outer loop. ...
Yes - this is a case where using 'for..' loops is the better choice, as they initialize the loop control variable when starting the loop, and increment it each time round. 'while..' loops are fine for situations where you don't have a counter for loop control.
Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson and G. Sussman
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
October 15th, 2009, 02:59 AM
#10
Re: Arrays
Ive changed my codes.. however i cant encrypt anything more than the length of my keyword could some1 please help me on this? also i dont know why i get a null infront before the encryption as shown in the test case.
Testcase
Code:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
H I J K L M N O P Q R S T U V W X Y Z A B C D E F G
I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
J K L M N O P Q R S T U V W X Y Z A B C D E F G H I
K L M N O P Q R S T U V W X Y Z A B C D E F G H I J
L M N O P Q R S T U V W X Y Z A B C D E F G H I J K
M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
O P Q R S T U V W X Y Z A B C D E F G H I J K L M N
P Q R S T U V W X Y Z A B C D E F G H I J K L M N O
Q R S T U V W X Y Z A B C D E F G H I J K L M N O P
R S T U V W X Y Z A B C D E F G H I J K L M N O P Q
S T U V W X Y Z A B C D E F G H I J K L M N O P Q R
T U V W X Y Z A B C D E F G H I J K L M N O P Q R S
U V W X Y Z A B C D E F G H I J K L M N O P Q R S T
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
Z A B C D E F G H I J K L M N O P Q R S T U V W X Y
Works if the plaintext is <= keyword
Keyword:asdfhgjk
Enter plaintext to encrypt:asdjfksq
Encrypted text:nullAKGOMQBA
Does not work is plaintext is > keyword
Enter plaintext to encrypt:asdjfksqa
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 8
Code:
public String encrypt(String plaintext)
{
int i=0,j=1,k=1,f=0, g=0,c=0;
fo
return encryptedText;
}
//encryptText method
public void encryptText()
{Scanner sc=new Scanner(System.in);
String plaintext=new String();
System.out.print("Enter text needed to encrypt:");
plaintext=sc.nextLine();
System.out.println("Encrypted text:"+encrypt(plaintext));
}
Last edited by hugo84; October 15th, 2009 at 07:12 AM.
-
October 15th, 2009, 04:44 AM
#11
Re: Arrays
It doesn't work for plaintext longer than keyword because the first thing you do in the loop is adjust the index you use for the keyword in case it is out of bound and then, just before using it you reassign it with the index for the plaintext. Just remove this:
Code:
if(c>keyword.length()){
c-=keyword.length();
}
and replace
with:
Code:
c = i % keyword.length();
I don't see where you declare encryptedText, but I guess it must be something like:
Code:
String encryptedText = null;
Later in your code you do:
Code:
encryptedText+=lookupTable[row][col];
That's why you get null at the beginning of your output. Use this to declare the variable:
Code:
String encryptedText = '';
However it will be more efficient to use a StringBuilder instead. And it should be a local variable. I don't see if you reset it either, if you are not resetting it and if you run the encryption method more than once the different results will be concatenated and with every call you will have the previous result plus the new encrypted text.
-
October 15th, 2009, 05:20 AM
#12
Re: Arrays
ok got it.. thanks.. 1 question why do you use c = i % keyword.length();
Last edited by hugo84; October 15th, 2009 at 07:14 AM.
-
October 15th, 2009, 07:52 AM
#13
Re: Arrays
Using % you will always get a number between 0 and keyword.length(). When i equals keyword.length(), % will give you 0, when i is keyword.length() + 1, % gives you 1, &c. That is the same thing you wanted to do with
Code:
if(c>keyword.length()){
c-=keyword.length();
}
but less verbose.
You've editted your last post before I could answer it, but the problem (I guess you figured it out already) was that the ciphertext is all uppercase characters and the method was using 97 instead of 65 to convert the characters to indexes.
-
October 15th, 2009, 10:07 AM
#14
Re: Arrays
ok got it arleady.. thanks for the pointer
Last edited by hugo84; October 15th, 2009 at 10:19 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|