## Yutxz

My program receives no errors or run time errors, but for some reason it runs infinitely. I need help pinpointing the problem D:

``````package captcrunch;

/**
*
* @author Josh
*/

public class Main {

public static String captCrunch (String input, int n) {
// method captCrunch takes a string and produces a new one with
// each character replaced by (character + n).

String message = "";
int index = 0;
int length = input.length();

while (index < length) {
char letter = input.charAt(index);
// letter is the single char pulled from input
int code = letter;
// code will be the new letter produced by adding n

if ('a' <= letter && letter <= 'z') {
// lowercase
code = code + n;
if (code < 'a') {
code = code + 26;
// 26 = num of chars in alphabet, for wrap around
} else if (code > 'z') {
code = code - 26;
}
message = message + (char)code;
} else {
if ('A' <= letter && letter <= 'Z') {
// uppercase
code = code + n;
if (code < 'a') {
code = code + 26;
// 26 = num of chars in alphabet, for wrap around
} else if (code > 'z') {
code = code - 26;
}
message = message + (char)code;
}
}
}
return message;
}

public static void main(String[] args) {
// TODO code application logic here
System.out.println (captCrunch ("hi", 1));
}

}``````

## Yutxz

Exercise:
The Captain Crunch decoder ring works by taking each letter in a string and
adding 13 to it. For example, ’a’ becomes ’n’ and ’b’ becomes ’o’. The letters
“wrap around” at the end, so ’z’ becomes ’m’.
Write a method that takes a String and that returns a new String containing the
encoded version. You should assume that the String contains upper and lower
case letters, and spaces, but no other punctuation. Lower case letters should
be tranformed into other lower case letters; upper into upper. You should not
encode the spaces.
90 Strings and things
b. Generalize the Captain Crunch method so that instead of adding 13 to the
letters, it adds any given amount. Now you should be able to encode things by

## kramerd 29

The problem is you are not incrementing index inside your while loop so you're always looking at the first character of the input string.

## nagarjunrajen -4

``````import java.util.Scanner;

public class Encode {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String msg = s.nextLine();

System.out.print("\nEnter your Encoding Number : ");
int num = s.nextInt();
String encoded = scramble(msg,num);
System.out.println("\nOriginal Message is "+msg);

System.out.println("\nEncoded Message is "+encoded);
}

public static String scramble (String input, int n) {

String message = "";
int index = 0;
int length = input.length();

System.out.print("\nEncoding Numbers are : ");
while (index < length) {
char letter = input.charAt(index);

int code = letter;
System.out.print("\t"+code);

if ('a' <= code && code <= 'z') {
code = code + n;

if (code > 'z') {
code = code - 26;
}
message = message + (char)code;
}
else if ('A' <= letter && letter <= 'Z') {

code = code + n;

if (code > 'Z') {
code = code - 26;
}
message = message + (char)code;
}
else if(letter == ' '){
message = message + ' ';
}
index++;
}
return message;

}

}``````

This will definitely help u....

commented: No code tags, ugly coding, only 1 year too late -4

## JamesCherrill 3,661

1. Don't just give people answers to their homework - that's called "cheating", and doesn't help them learn.
2. That post was from October 2010 - your reply is a year too late!
3. Always post any code in CODE tags.