Results 1 to 12 of 12

Thread: Java Code help     submit to reddit submit to twitter

  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Java Code help

    Ok assignment is to prompt user for a sentence, paragraph, ext and it is suppose change each letter to 13 letters ahead, ex: turn a to m, b to n, spaces to $. I have the code done its just some letters work and some dont, can someone please tell me where i messed up. I'm pretty sure its a simple typing error but i cant see it for some reason ><. Thanks Also is there a easer way to do it? I did it fastest and easiest way i could think of

    Code:
    import java.util.Scanner;
    
    public class Rotter {
    	public static void main (String[] args) {
    		String message;
    		String vert1, vert2, vert3, vert4, vert5, vert6, vert7, vert8, vert9, vert10, vert11, vert12;
    		String vert13, vert14, vert15, vert16, vert17, vert18, vert19, vert20, vert21, vert22, vert23;
    		String vert24, vert25, vert26, vert27;
    		Scanner scan = new Scanner (System.in);
    		System.out.println("Please enter your coded message:");
    		message = scan.nextLine();
    		vert1 = message.replace( 'a', 'm');
    		vert2 = vert1.replace( 'b', 'n');
    		vert3 = vert2.replace( 'c', 'o');
    		vert4 = vert3.replace( 'd', 'p');
    		vert5 = vert4.replace( 'e', 'q');
    		vert6 = vert5.replace('f', 'r');
    		vert7= vert6.replace('g', 's');
    		vert8 = vert7.replace('h','t');
    		vert9 = vert8.replace('i','u');
    		vert10 = vert9.replace('j', 'v');
    		vert11 = vert10.replace('k', 'w');
    		vert12 = vert11.replace('l', 'x');
    		vert13= vert12.replace('m', 'y');
    		vert14= vert13.replace('n', 'z');
    		vert15 = vert14.replace('o', 'a');
    		vert16 = vert15.replace('p', 'b');
    		vert17 = vert16.replace('q', 'c');
    		vert18 = vert17.replace('r', 'd');
    		vert19 = vert18.replace('s', 'e');
    		vert20 = vert19.replace('t', 'f');
    		vert21 = vert20.replace('u', 'g');
    		vert22 = vert21.replace('v', 'h');
    		vert23 = vert22.replace('w', 'i');
    		vert24 = vert23.replace('x', 'j');
    		vert25 = vert24.replace('y', 'k');
    		vert26 = vert25.replace('z', 'l');
    		vert27 = vert26.replace(' ', '$');
    		System.out.println(vert27); 
    	}
    }

  2. #2
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    I think i found the problem... vert 1-14 dont work but but everything after does... I think it is replacing A with M, on vert1, then converting the M to y on vert13, then y to k on vert 24. How do i prevent this.

  3. #3
    Chram
    Join Date
    Aug 2007
    Posts
    2,699
    BG Level
    7
    FFXIV Character
    Nours Sruon
    FFXIV Server
    Moogle
    FFXI Server
    Fenrir

    its just some letters work and some dont
    Which work and which don't?

    Wild guess but...

    vert2 = vert1.replace( 'b', 'n'); replaces b by n

    then when it gets to

    vert14= vert13.replace('n', 'z'); it replaces the "new" n by z?

    I don't know anything about Java but that's the first thing I noticed.

  4. #4
    Banned.

    Join Date
    Jul 2005
    Posts
    5,821
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Loop through the string and add to the ascii code. This can be done in about 5 lines of code.

    edit: Also, there is a pretty major logical error in your code. If you're going to write the program this way, have one string acting as the "new" string, and replacing things from the old string.

    If you need help, my aim is Senotaru. I'd be happy to help you.

    This isnt the answer, but should also help:
    Spoiler: show

    Code:
    int total = 0;    
    for (int i=0; i < word.length(); i++)       
    {       
    char c = word.charAt(i);       
    int j = (int) c;       
    total += c;       
    System.out.println("ASCII of: "+ c +" = " + j);       
    }       
    System.out.println(total);

  5. #5
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Senoska View Post
    Loop through the string and add to the ascii code. This can be done in about 5 lines of code.

    edit: Also, there is a pretty major logical error in your code. If you're going to write the program this way, have one string acting as the "new" string, and replacing things from the old string.

    If you need help, my aim is Senotaru. I'd be happy to help you.

    This isnt the answer, but should also help:
    Spoiler: show

    Code:
    int total = 0;    
    for (int i=0; i < word.length(); i++)       
    {       
    char c = word.charAt(i);       
    int j = (int) c;       
    total += c;       
    System.out.println("ASCII of: "+ c +" = " + j);       
    }       
    System.out.println(total);
    havent learned about ASCII yet so perfer to stay away from it atm. How would be the best way to do a new string to edit? I have ideas but just wondering your opitions.

  6. #6
    Banned.

    Join Date
    Jul 2005
    Posts
    5,821
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Does your prof. care how you do this? Honestly, if you don't have an understanding of ascii it doesn't make much sense that he would want a SPACE to turn into $ How could you possible know what 13 characters past , are without knowing a bit about ascii?

  7. #7
    Hydra
    Join Date
    Apr 2008
    Posts
    119
    BG Level
    3

    ascii should of been some of the first things your professor taught you. also, use loops, writing code will only get more complicated from here on out so it's best to use loops whenever possible, especially if you don't know how to use them very well yet

  8. #8
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    I dont think he really cares as long as it does what he wants when ran. He likes it to be simple but really im pressed for time this year so just trying to get stuff that works atm lol. Would love to improve and use loops and stuff but what i know about loops atm doesn't seem helpful. Only thing i could think of would be like a for loop that said (int i=0;i<message.length; i++) then change the characters one by one but i have no clue how to change the letters, unless i broke them all down into numbers and added 13 to each then reconverted ><. On another note i read the assignment over and he wants us to give a option to code or decode... basically put the same code in a if statement. I did that just to try and i got this with a major error. It runs, ask the user for input, gets their input then says "enter coded message" or "Enter decode message" or "invalid input" the correct ones its suppose be then kicks out of the program, never lets the user enter their input.

    Code:
    import java.util.Scanner;
    
    public class RotterTest {
        public static void main (String[] args) {
            String messageCoded;
            int messageChoice;
            String vert1, vert2, vert3, vert4, vert5, vert6, vert7, vert8, vert9, vert10, vert11, vert12;
            String vert13, vert14, vert15, vert16, vert17, vert18, vert19, vert20, vert21, vert22, vert23;
            String vert24, vert25, vert26, vert27;
            Scanner scan = new Scanner (System.in);    
            System.out.println("Press 1. For code, Press2. for Decode");
            messageChoice = scan.nextInt();
    
            if(messageChoice == 1) {        
                System.out.println("1:");
                messageCoded = scan.nextLine();
                vert1 = messageCoded.replace( 'a', 'n');
                vert2 = vert1.replace( 'b', 'o');
                vert3 = vert2.replace( 'c', 'p');
                vert4 = vert3.replace( 'd', 'q');
                vert5 = vert4.replace( 'e', 'r');
                vert6 = vert5.replace('f', 's');
                vert7= vert6.replace('g', 't');
                vert8 = vert7.replace('h','u');
                vert9 = vert8.replace('i','v');
                vert10 = vert9.replace('j', 'w');
                vert11 = vert10.replace('k', 'x');
                vert12 = vert11.replace('l', 'y');
                vert13= vert12.replace('m', 'z');
                vert14= vert13.replace('n', 'a');
                vert15 = vert14.replace('o', 'b');
                vert16 = vert15.replace('p', 'c');
                vert17 = vert16.replace('q', 'd');
                vert18 = vert17.replace('r', 'e');
                vert19 = vert18.replace('s', 'f');
                vert20 = vert19.replace('t', 'g');
                vert21 = vert20.replace('u', 'h');
                vert22 = vert21.replace('v', 'i');
                vert23 = vert22.replace('w', 'j');
                vert24 = vert23.replace('x', 'k');
                vert25 = vert24.replace('y', 'l');
                vert26 = vert25.replace('z', 'm');
                vert27 = vert26.replace(' ', '$');
                System.out.println(vert27); 
            }
            else if(messageChoice == 2) {
                System.out.println("2");
                messageCoded = scan.nextLine();
                vert1 = messageCoded.replace( 'a', 'n');
                vert2 = vert1.replace( 'b', 'o');
                vert3 = vert2.replace( 'c', 'p');
                vert4 = vert3.replace( 'd', 'q');
                vert5 = vert4.replace( 'e', 'r');
                vert6 = vert5.replace('f', 's');
                vert7= vert6.replace('g', 't');
                vert8 = vert7.replace('h','u');
                vert9 = vert8.replace('i','v');
                vert10 = vert9.replace('j', 'w');
                vert11 = vert10.replace('k', 'x');
                vert12 = vert11.replace('l', 'y');
                vert13= vert12.replace('m', 'z');
                vert14= vert13.replace('n', 'a');
                vert15 = vert14.replace('o', 'b');
                vert16 = vert15.replace('p', 'c');
                vert17 = vert16.replace('q', 'd');
                vert18 = vert17.replace('r', 'e');
                vert19 = vert18.replace('s', 'f');
                vert20 = vert19.replace('t', 'g');
                vert21 = vert20.replace('u', 'h');
                vert22 = vert21.replace('v', 'i');
                vert23 = vert22.replace('w', 'j');
                vert24 = vert23.replace('x', 'k');
                vert25 = vert24.replace('y', 'l');
                vert26 = vert25.replace('z', 'm');
                vert27 = vert26.replace(' ', '$');
                System.out.println(vert27);
            }
            else {
                System.out.println("Invalid Choice");
            }
        }
    }

  9. #9
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    My idea is to put everything into 2 loops. The first reads each letter one by one, while it has that letter it changes it. But im not sure how to code it.

  10. #10
    The Wang
    Join Date
    Jun 2006
    Posts
    1,343
    BG Level
    6
    FFXIV Character
    Furt Wangler
    FFXIV Server
    Coeurl
    FFXI Server
    Sylph

    Do yourself a favor- look at the ascii table, and consider what a few others + what im about to tell you and redesign your code. You'll end up saving time and it will look 10x cleaner. It's really simple.

    Every string character that they will type to you can be converted to a char(another data type, look it up if you aren't familiar with it), which java treats as an integer. This works very easily for cyphering/decyphering text. As you can see if you look at an ascii table, all letters are assigned a value, which you don't actually need to know for this but it will help you understand what's going on.

    Because chars are also integers, you can add them, and use them for operations in java.

    Basically what you'll want to do, is take the string they entered, and iterate over it. Use a loop like for(int i =0; i<string.length; i++) and call .charAt(i) on the string. at this point you should take what it returns and assign it to an int variable for changing the letter.

    It doesn't look like you have to handle for uppercase letters so it'll be a bit easier. Each char returned(the letter you're working with) will either be in the range from a-z, or it'll be a space. So you'll want to check if that is so. You can simply say if(int >= 'a' && int <= 'z') to check for this, otherwise it is a space and that will be your else statement.

    Once you have the letter you want to work with,
    1) because of the way the ascii table works, you'll want to subtract 'a' from it. As you can see from the table, this makes your current letter have a number value between 1 and 26.
    2)Now you can add x number to it, and mod the whole thing by 26 (that accounts for if you add like 12 to x and it has wrap around).
    3)finally, you can do something like newChar = (char) (newcurrent+'a'); to get it back into its true ascii value.
    4)you're basically finished. All you have to do after that is append the result to a global string, and print it out after.

    Spaces are similar but instead just return a $ string automatically.

    Hope you find the courage to refactor everything, I think you'll get a lot more out of the project.

  11. #11
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Thanks a ton furt, I have to work on another project atm (battle ship ><) but as soon as i get time i think im going to redo the code and try to get better at it and learn the ASCII

  12. #12
    The Wang
    Join Date
    Jun 2006
    Posts
    1,343
    BG Level
    6
    FFXIV Character
    Furt Wangler
    FFXIV Server
    Coeurl
    FFXI Server
    Sylph

    send me a message on aim and I can help walk you through what you need to do(or any other java stuff anyways). It's not as much about learning ascii(there isn't really much to learn itsjust a data table) rather using it to make this 10x easier.

Similar Threads

  1. c++ code help
    By Skie in forum Tech
    Replies: 19
    Last Post: 2010-02-17, 21:27
  2. c++ code help version 3.
    By Skie in forum Tech
    Replies: 3
    Last Post: 2009-12-04, 17:56
  3. CS Code help, number program
    By Skie in forum Tech
    Replies: 8
    Last Post: 2009-11-13, 13:57
  4. Windows XP code help
    By Etice in forum Tech
    Replies: 3
    Last Post: 2009-07-16, 14:25