Results 1 to 9 of 9
Thread: Enciphing
- 04-18-2012, 09:29 AM #1
Enciphing
Can someone please help me fix this. It compiles correctly but it outputs the wrong things. I'm not sure what I've done wrong.
Java Code:import java.util.Scanner; public class Cipher { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String Input; String Output = ""; System.out.println("Input Word"); String Encipher = keyboard.nextLine(); Encipher.replaceAll("\\s",""); Integer EncipherPosition = 0; String Cipher = ""; for (EncipherPosition = 0; EncipherPosition < Encipher.length(); EncipherPosition ++) { char EncipherChar = Encipher.charAt(EncipherPosition); String EncipherString = "" + EncipherChar; boolean temp = Cipher.contains(EncipherString); if (temp == false) { Cipher += Encipher.charAt(EncipherPosition); } } System.out.println("This is what you Input: " + Cipher); String alphabet = "abcdefghijklmnopqrstuvwxyz"; Integer alphabetPosition = 0; for (alphabetPosition = 0;alphabetPosition < alphabet.length();alphabetPosition ++) { char alphabetTempChar = alphabet.charAt(alphabetPosition); String alphabetTempString = "" + alphabetTempChar; boolean alphabetTemp = Cipher.contains(alphabetTempString); if (alphabetTemp == false) { Cipher += alphabet.charAt(EncipherPosition); } } System.out.println("This is your ciphered text" + Cipher + alphabet + Cipher); System.out.println("Input message:"); Input = keyboard.nextLine(); Integer position = 0; for (position = 0; position < alphabet.length(); position ++) { char CipherChar = Cipher.charAt(position); String CipherString = "" + CipherChar; char alphabetChar = alphabet.charAt(position); String alphabetString = "" + alphabetChar; Input.replace(alphabetString,CipherString); } System.out.println("This is your encoded message : " + Input); } }
- 04-18-2012, 09:46 AM #2
Re: Enciphing
You need to be more specific. What output do you expect, and what wrong output do you get?it outputs the wrong things
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-19-2012, 07:52 PM #3
Re: Enciphing
This is what happens when I run the program
It outputs pretty near random letters instead of an enciphered one and doesn't finish the program because it should also output an enciphered message that was input.Input Word
testing
This is what you Input: tesing
This is your ciphered texttesinghhhhhhhhhhhhhhhhhhhabcdefghijklmnopqrstu vwxyztesinghhhhhhhhhhhhhhhhhhh
Input message:
test one
I get these errors:
Thanks for your help!Java Code:java.lang.StringIndexOutOfBoundsException: String index out of range: 25 at java.lang.String.charAt(String.java:686) at Cipher.main(Cipher.java:47)
- 04-19-2012, 08:22 PM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Enciphing
Using too much copy paste can be a problem.Java Code:Cipher += alphabet.charAt(EncipherPosition);
- 04-20-2012, 10:48 AM #5
Re: Enciphing
What's wrong with it?
- 04-20-2012, 12:55 PM #6
Re: Enciphing
Can you post what the output from the program should be?
You get the StringIndexOutOfBoundsException: String index out of range: 25
because the index you are using is past the end of the String. Check your logic to make sure the index stays within the length of the String.If you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 03:13 PM #7
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
- 04-21-2012, 09:31 AM #8
- 04-21-2012, 12:18 PM #9
Re: Enciphing
What should be printed out by the program?
Your post#3 shows some of what is printed out, what should the rest of the print out be?
What is the length of the String where the exception occurs? What is the value of the index when the exception occurs?
Add a println statement just before that statement and print out those values.Last edited by Norm; 04-21-2012 at 12:20 PM.
If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Enciphing Text based on a word
By DuncanHollows in forum New To JavaReplies: 1Last Post: 03-27-2012, 07:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks