Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Encoding Program

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Encoding Program

    Hi all, i am trying to make a program with arrays that encodes a word or phrase inputted by the user. The "encoding language" is shown under the array named "encode." What i have so far compiles and runs, but when i try inputting a phrase, nothing outputs. I was wondering if anyone could help me out! Thanks!

     
    import java.util.Arrays;
    import java.util.Scanner;
    /**
     * Write a description of class decoderProgram here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class decoderProgram
    {
        public static void main (String [] args)
        {
            System.out.println("\f");
            Scanner myScan=new Scanner (System.in);
            char[] alphabet= {' ', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
            char[] encode={' ', 'q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m'};
     
     
            System.out.println("Enter a phrase do be encoded");
            String userInput=myScan.nextLine();
     
            userInput=userInput.toUpperCase();
     
     
     
            String output= " ";
            char firstLetter=' ';
            for(int i = 0; i<userInput.length(); i++)
            {
     
                for (int x =0; x<27; x++)
                {
                    if (firstLetter == encode[x])
                    {
                        output= output+alphabet[x];
                    }
                }
     
            }
     
     
            System.out.println(output);
     
        }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Encoding Program

    Quote Originally Posted by geabus1043 View Post
    ...nothing outputs...
    Are you sure? Does it compile?

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Encoding Program

    oddly enough yes, im going to try it on my other computer and see what happens, could be a bug, ill let you know.

    --- Update ---

    ok so it actually technically does output something but that something is just a blank line.... not sure if that helps or not

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Encoding Program

    There seems to be an import missing
    After that it compiles but warns about not closing the scanner.
    But I do get output, just likely not the output desired.
    Fix the import and close the scanner, post the modified code and any error messages you may have with your question

    --- Update ---

    ok so it actually technically does output something but that something is just a blank line.... not sure if that helps or not
    Yes, now add some other printlns to determine what parts of the code are running and try to find where things seem to go unexpected. Are the loops running? Are characters being matched? Are characters being encoded?...
    See what you find

  5. The Following User Says Thank You to jps For This Useful Post:

    geabus1043 (May 22nd, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Encoding Program

    ok so i found out that having char firstLetter=' '; was my problem, and when i changed it to firstLetter= userInput.charAt(0); i got an output, but it would just be the first letter, which made sense. So after my first for loop i put

    firstLetter=userInput.charAt(i);

    and now it works! thanks for the help!!

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Encoding Program

    You are welcome, I am glad you got it working.
    Please mark your threads as solved when your questions have been answered.
    Directions can be found in the Announcements page if you need help.

Similar Threads

  1. Help with character encoding
    By IHeartProgramming in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 20th, 2012, 09:19 PM
  2. invalid encoding for signature
    By Silvery in forum JDBC & Databases
    Replies: 2
    Last Post: July 2nd, 2012, 03:20 PM
  3. Base64 encoding is not working.
    By samjna in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 28th, 2012, 11:50 PM
  4. Convert file from any encoding to UTF-8
    By efluvio in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 26th, 2011, 01:16 AM
  5. Problem with Japanese Encoding
    By sendhilpk in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 12th, 2010, 02:57 AM

Tags for this Thread