- Joined
- Sep 1, 2008
- Messages
- 2,181
Well, I am working on a project for java class. What we are supposed to do is make a program, that when given a string and a number, will convert said string to ASCII, add the given number to each of the ASCII numbers, and then convert it back to English. If a capital letter is given, it must return a capital letter. It must be able to do whole sentences. It must wrap around(ex. if the original letter was x, and the number was 5, it should end up being c). With the code I currently have, it doesn't take into effect capitals, or wrapping around. All I am trying to get it to do at this point is to get it to return a word. For some unknown reason, it won't return more than the first letter converted. I have no idea why. Help? Code below. Thanks.
import java.util.Scanner;
public class ASCIICoder
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int z = 0;
while(z==0)
{
System.out.println("Which would you like to do?" +
"\n1. Code a sentence" +
"\n2. Decode a sentence" +
"\n3. Quit.");
int choice = keyboard.nextInt();
switch (choice)
{
case 1:
System.out.println("What is the string?");
String sentence = keyboard.next();
System.out.println("What is the ASCII number?");
keyboard.nextLine();
int num = keyboard.nextInt();
System.out.println(oneLetter(sentence, num));
;break;
case 2:
;break;
case 3:
System.out.println("Good bye.");
z++;
;break;
}
}
}
public static String coder(String a, int num)
{
String temp = "";
for(int i = 0; i < a.length();i++)
{
String oneLetter = a.charAt(i)+"";
char letter = oneLetter(oneLetter, num);
temp += letter;
}
return temp;
}
public static char oneLetter(String a, int num)
{
int temp = (int)a.charAt(0);
temp += num;
char b = (char)temp;
return b;
}
}









