Results 1 to 17 of 17
Thread: Working with strings
- 07-27-2012, 08:49 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Working with strings
I am making a decoder for a cipher called the Caesar Shift Cipher. I am making a first build of it and it will not be efficent at all so please ignore that. Here is my code so far:
import java.util.Scanner;
public class decoder {
I thought this would work but whenever I run it it does not print out the new letter. I have no previous work with stings and I would really appriciate any information. Thanks.Java Code:public static void main(String[] args) { Scanner number = new Scanner(System.in); System.out.println("Please enter your shift number"); int shift = number.nextInt(); int counter = 1; if (shift == 1) while (counter > 0) { System.out.println("Enter a letter"); String letter = number.next(); if (letter == "a") System.out.println("z"); else if (letter == "b") System.out.println("a");Last edited by gman1199; 07-28-2012 at 01:26 AM.
- 07-27-2012, 09:14 PM #2
Re: Working with strings
Why do they call it rush hour when nothing moves? - Robin Williams
- 07-27-2012, 10:25 PM #3
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Working with strings
1. You don't have a closing bracket for your class.
2. You don't have a closing bracket for your while-loop.
3. A letter will only print out if the user enters a or b.
4. A letter will only print out if the user enters 1 for the shift.
5. Inside your while-loop counter is never decremented, so the while-loop (if it had a closing bracket) would run forever.
6. You compare strings using the == operator when you should use the equals() method instead.
7. No precautions against invalid user input are taken.
8. You don't have a closing bracket for your main method.
Also, I don't understand the connection between the shift number and the actual cipher.
Since you haven't worked with Strings before, I recommend that you read this: Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)Last edited by awinston; 07-27-2012 at 11:54 PM. Reason: #8
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 07-28-2012, 01:34 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Re: Working with strings
awinston -
Thank you. The reason for a lot of the errors you pointed out is the program is not finished and I only posted a small section because it is, as I say in the post, very lengthy. The shift number is basically the amount of letters that are moved from the back of the alphabet to the front. For example, a cipher where the shift number is 4 would look like this: a=w, b=x, c=y, d=z, e=a, ... z=v. The cipher was used by Caesar to communicate with his generals. It's very easily solved because there are only 25 possible combinations.
- 07-28-2012, 02:06 AM #5
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Working with strings
That's interesting!
Why are you choosing to go about this in an inefficient manner (as you pointed out)?"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-13-2012, 07:35 PM #6
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Re: Working with strings
Sorry for not getting back sooner, out of town for a few weeks. The answer to your question is I have no idea how else to do it. Also, I was working on my code today, trying to make it analise a whole string, and I came up with this (using that web page you gave me) :
I thought this would work, but when I ran it, nothing was printed out. I originally got this error -Java Code:import java.util.Scanner; public class decoder { public static void main(String[] args) { Scanner number = new Scanner(System.in); System.out.println("Please enter your shift number"); int shift = number.nextInt(); int counter = 0; int i = 0; System.out.println("Enter a string"); String cipher = number.next(); int len = cipher.length(); if (shift == 1) while (counter < len) { Character letter = cipher.charAt(i); if (letter.equals("a")) System.out.println("z"); else if (letter.equals("b")) System.out.println("a"); counter++; i++; } } }
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(Unknown Source)
at decoder.main(decoder.java:15)
- but this was fixed when I changed to while loop to while (counter < len). Any thoughts you have on this would be appriciated, I have no idea what is wrong.
Thanks,
gman1199
- 08-13-2012, 07:44 PM #7
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Working with strings
Can you post the code that was giving you the error?
Also, it looks like you can use either counter or i without using both of them. They are always the same value, so you only need one."Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-13-2012, 07:52 PM #8
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Re: Working with strings
This is what the error was coming fromJava Code:import java.util.Scanner; public class decoder { public static void main(String[] args) { Scanner number = new Scanner(System.in); System.out.println("Please enter your shift number"); int shift = number.nextInt(); int counter = 0; int i = 0; System.out.println("Enter a string"); String cipher = number.next(); int len = cipher.length(); if (shift == 1) while (counter < len) { Character letter = cipher.charAt(i); if (letter.equals("a")) System.out.println("z"); else if (letter.equals("b")) System.out.println("a"); counter++; i++; } } }
- 08-13-2012, 08:10 PM #9
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Working with strings
I can compile and run that code with no errors. Didn't you say that you were receiving the error before you changed the condition of the while-loop to (counter < len)? What was the condition before?
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-13-2012, 08:17 PM #10
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Working with strings
Also, you should be aware that using the equals() method to compare a Character with a String will return false.
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-13-2012, 08:21 PM #11
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Re: Working with strings
Sorry, I forgot to change it from while (counter < len) to while (counter <= len). It was (counter <= len) that was causing the error. Also, is there a way to fix that equals() thing?
- 08-13-2012, 09:16 PM #12
Re: Working with strings
Yeah, just don't use a character.is there a way to fix that equals() thing?
In java, a literal 'a' is the character a where as "a" is a string. As long as you are not using a literal character, you're fine. The alternative is to use only characters in which case the == will work. You're current solution seems fine though!
- 08-13-2012, 09:27 PM #13
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Re: Working with strings
So if I replace "a" with a (or is it 'a'?) it should work?
- 08-13-2012, 09:29 PM #14
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
Re: Working with strings
Thank you so much! I replaced everything with the '' and it works!
- 08-13-2012, 09:39 PM #15
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Working with strings
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 08-14-2012, 02:58 AM #16
- 08-14-2012, 05:10 PM #17
Similar Threads
-
OR || not working in while loop when comparing strings
By janey4115 in forum New To JavaReplies: 4Last Post: 11-16-2011, 11:42 AM -
if statement with strings not working... again
By hardcorebadger in forum New To JavaReplies: 4Last Post: 01-11-2011, 06:02 AM -
\n not working in GUI (working code, but \n isn't working)
By cc11rocks in forum New To JavaReplies: 2Last Post: 01-04-2011, 04:30 AM -
working with unicode strings
By drift in forum New To JavaReplies: 4Last Post: 10-08-2010, 07:58 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks