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 4 of 4

Thread: Weird Characters printed

  1. #1
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Weird Characters printed

    My program is printing random weird characters.
     
    /**
     * Write a description of class LandonLoweCipher here.
     * 
     * @author (Landon) 
     * @version (a version number or a date)
     */
    import java.io.PrintWriter;
    import java.io.File;
    import java.util.*;
    public class LandonCipher
    {
        // instance variables - replace the example below with your own
        private static String phrase;
        private static String newPhrase;
        private static final char[] alpha = {'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'};
        private static char[] newAlpha;
       /**
         * Constructor for objects of class LandonLoweCipher
         */
        public LandonCipher(String p)
        {
            // initialise instance variables
            phrase = p;
            newPhrase = "";
            newAlpha = new char[26];
        }
        public static char[] newAlphabet()
        {
           Random rn = new Random();
           int ran = 0;
           // loop adding random int to array
           for (int i = 0; i < alpha.length; i++)
           {
             ran =  rn.nextInt(26);
             newAlpha[i] += alpha[ran];
             //tests whether the random letter is already in array
             for(int a = 0; a < newAlpha.length; a++)
             {
                 if(newAlpha[i] == newAlpha[a])
                 {
                     ran =  rn.nextInt(26);
                     newAlpha[i] += alpha[ran];
     
                    }
             }
           }
           return newAlpha;
        }
        public static String newPhrase()
        {
            phrase = phrase.toLowerCase();
            for (int i = 0; i < alpha.length; i++)
            {
                 for (int a = 0; a < phrase.length(); a++)
                      if(phrase.charAt(a) == alpha[i])
                         newPhrase += newAlpha[i];
            }
            return newPhrase;
        }
    }
    Here is tester
    public class LandonTester
    {
        public static void main( String[] args)
        {
            // put your code here
            LandonCipher llc = new LandonCipher("Hey");
            System.out.println(llc.newAlphabet());
        }
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Weird Characters printed

    Quote Originally Posted by llowe29 View Post
        // instance variables - replace the example below with your own
        private static String phrase;
        private static String newPhrase;
        private static final char[] alpha = {'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'};
        private static char[] newAlpha;
    First, these are not "instance" variables .... but "class" variables. And it's not correct for your intentions.
    The char[] alpha has sense that it is "static", not the others.

    Quote Originally Posted by llowe29 View Post
        public static char[] newAlphabet()
        {
           Random rn = new Random();
           int ran = 0;
           // loop adding random int to array
           for (int i = 0; i < alpha.length; i++)
           {
             ran =  rn.nextInt(26);
             newAlpha[i] += alpha[ran];
             //tests whether the random letter is already in array
             for(int a = 0; a < newAlpha.length; a++)
             {
                 if(newAlpha[i] == newAlpha[a])
                 {
                     ran =  rn.nextInt(26);
                     newAlpha[i] += alpha[ran];
     
                    }
             }
           }
           return newAlpha;
        }
    Second, this part is rather confusionary and not-so-good. Why += ? And are you trying to "shuffle" an array? It's possible but not in that way. You should not "tests whether the random letter is already in array", because it's not the right approach.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Weird Characters printed

    I made changes suggested though dont have any idea about array part. The weird characters(thanks are gone but letters are beig repeated in alphabet.
    /**
     * Write a description of class LandonCipher here.
     * 
     * @author (Landon) 
     * @version (a version number or a date)
     */
    import java.io.PrintWriter;
    import java.io.File;
    import java.util.*;
    public class LandonCipher
    {
        // instance variables - replace the example below with your own
        private String phrase;
        private String newPhrase;
        private final char[] alpha = {'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'};
        private char[] newAlpha;
       /**
         * Constructor for objects of class LandonLoweCipher
         */
        public LandonCipher(String p)
        {
            // initialise instance variables
            phrase = p;
            newPhrase = "";
            newAlpha = new char[26];
        }
        public char[] newAlphabet()
        {
           Random rn = new Random();
           int ran = 0;
           // loop adding random int to array
           for (int i = 0; i < alpha.length; i++)
           {
             ran =  rn.nextInt(26);
             newAlpha[i] = alpha[ran];
             //tests whether the random letter is already in array
             for(int a = 0; a < newAlpha.length; a++)
             {
                 if(newAlpha[i] == newAlpha[a])
                 {
                     ran =  rn.nextInt(26);
                     newAlpha[i] = alpha[ran];
     
                    }
             }
           }
           return newAlpha;
        }
        public String newPhrase()
        {
            phrase = phrase.toLowerCase();
            for (int i = 0; i < alpha.length; i++)
            {
                 for (int a = 0; a < phrase.length(); a++)
                      if(phrase.charAt(a) == alpha[i])
                         newPhrase += newAlpha[i];
            }
            return newPhrase;
        }
    }

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Weird Characters printed

    Quote Originally Posted by llowe29 View Post
    I made changes suggested though dont have any idea about array part. The weird characters(thanks are gone but letters are beig repeated in alphabet.
    I repeat the question: are you trying to "shuffle" an array? It's easy to do and there is no need to use strange/unuseful internal loops to verify if "is already in array".

    In general, given an array of N elements, do a for loop with an index that goes from N-1 to 1 (thus, decreasing). For each index in the loop, extract a random number k that is 0 <= k < index (this is exactly the result of rnd.nextInt(index) ), and swap values at [index] and [k].
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Eliminating Unicode Characters and Escape Characters from String
    By bilalonjavaforum in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 19th, 2013, 05:26 AM
  2. How to replace characters in Array with user input Characters
    By gdoggson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 4th, 2013, 05:53 AM
  3. [SOLVED] Arrays not printed? Error?
    By hemla in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 3rd, 2013, 02:13 PM
  4. [SOLVED] iam not beeing printed to screen
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2012, 02:06 PM
  5. The positioning and alignment of the text on the paper to be printed
    By java_fledgeling in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 8th, 2010, 08:54 PM