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

Thread: Can Anyone Help With Arrays?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can Anyone Help With Arrays?

    I am attempting to design and implement a program with a Java class that will ask the user to input a non-null string. First, I should convertthis string to lower case (for simplicity) and store each character of the string into a char[] (an array of type char) (“helloworld”→|h|e|l|l|o|w|o|r|l|d|). Then print the array in forward and reverse order, unless the input string is a palindrome (the same spelling forward and backward, ex: "noon”). In that case, only print the array once and state that the inputted string is a planindrome. (see sample output). Next, store the string into a different char[], putting only 1 instance of each character into the array (“helloworld”→|h|e|l|o|w|r|d|). Then printthe new array in forward and reverse order (see sample output). Finally, I should sort the new array containing the non-repeated characters in ascending alphabetical order (|h|e|l|o|w|r|d|→|d|e|h|l|o|r|w|). Then print the sorted array in forward and reverse order ( see sampleoutput).

    P.S. - I cannot touch the main. PLEASE HELP, 4 days on the same code.
    ]
    import java.lang.reflect.Array;
    import java.util.Arrays;
     
    import javax.swing.*;
     
    public class Program6
    {
        // Modify the constant below to have your name instead of CIS Faculty
        public final static String TITLE_BAR = "Program 6 (Me)";
     
        // Write your class methods below
     
        // Passed the user input string and an array (size = length of the string)
        // for storing the chars of that string. Stores each char of the string to
        // the array.
        private static void storeAllCharacters(String input, char[] allCharacters)
        {
            String strOrig = "";
            allCharacters = strOrig.toCharArray();
        }
     
        private static int storeNoRepeatCharacters(String input,
                char[] noRepeatCharacters)
        {
     
            int stored = noRepeatCharacters.length;
            char[] nonDup;
            nonDup = new char[stored];
     
            for(int i = 0; i <= stored; i++)
            {
                if(input.charAt(i) != input.charAt(stored))                   
                {
                    return nonDup[i];
                }
     
            }
            return 0;
     
    //        int index = 1;
    //        for (int i = 1; i < length; ++i)
    //        {
    //            int j;
    //            for (j = 0; j < index; ++j)
    //            {
    //                if (noRepeatCharacters[i] == noRepeatCharacters[j])
    //                {
    //                    break;
    //                }
    //            }
    //            if (j == index)
    //            {
    //                noRepeatCharacters[index] = noRepeatCharacters[i];
    //                ++index;
    //            }
    //        }
    //        for (; index < length; index++)
    //        {
    //            noRepeatCharacters[index] = 0;
    //        }
    //        return index;
        }
     
        private static boolean isCharacterPresent(char repeat, char[] noRepeat, int index)
        {
            for(int i = 0; i <= noRepeat.length; i++)
            {
                if(noRepeat[i] == repeat)
                {
                    return true;
                }
            }
            return false;
        }
     
        private static void sortArray(char[] noRepeatCharacters, int lastIndexStored)
        {
            Arrays.sort(noRepeatCharacters);
        }
     
        private static void printArray(char[] allCharacters)
        {
            int j = 0;
            char[] temparray = allCharacters;
            boolean isPalindrome = false;
            if (isPalindrome)
            {
                System.out.print(temparray);
     
            }
            for (int i = allCharacters.length - 1; i >= 0; i--)
            {
                temparray[j++] = allCharacters[i];
            }
     
            for (int i = 0; i < allCharacters.length; i++)
            {
                allCharacters[i] = temparray[i];
            }
            System.out.print(temparray);
        }
     
        private static void printArray(char[] noRepeatCharacters,
                int lastIndexStored)
        {
            for(int i = 0; i <= lastIndexStored; i++)
            {
                System.out.print(noRepeatCharacters[i] + " ");
            }
            System.out.print("\n");
            for(int i = noRepeatCharacters.length -1; i <= 0; i++)
            {
                System.out.print(noRepeatCharacters[i]);
            }
            // Start from the size of the array (minus 1 to get the highest subscript)
            // and go down to zero
            for (int i = noRepeatCharacters.length - 1; i >= 0; i--)
            {
                System.out.println(noRepeatCharacters[i]);
            }
     
        }
     
        private static boolean isPalindrome(String input)
        {
            int lowNum = 0;
            int highNum = input.length() - 1;
     
            while (lowNum < highNum)
            {
                if (input.charAt(lowNum) != input.charAt(highNum))
                    return false;
     
                lowNum++;
                highNum--;
            }
            return true;
        }
     
        // Main method provided
        // Do not modify the main method
        public static void main(String[] args)
        {
     
            // Local Variables.
            String input = "";
            char[] allCharacters;
            char[] noRepeatCharacters;
            int lastIndexStored;
     
            do
            {
                input = JOptionPane.showInputDialog("Enter a non-empty string");
                if (input == null)
                {
                    JOptionPane.showMessageDialog(null,
                            "User clicked cancel. The Program will exit",
                            TITLE_BAR, JOptionPane.PLAIN_MESSAGE);
                    System.exit(0);
                }
            } while (input.length() == 0);
     
            // Convert the string to all lower case
            input = input.toLowerCase();
     
            // Create the arrays using the length of the input string
            allCharacters = new char[input.length()];
            noRepeatCharacters = new char[input.length()];
     
            // Call the method to store the entire input string
            storeAllCharacters(input, allCharacters);
     
            // Call the method to print the entire allCharacters array
     
            // Call the method to store the characters without repeating
            // and assign its return value
            lastIndexStored = storeNoRepeatCharacters(input, noRepeatCharacters);
     
            // Call the method to print the noReapeatCharacters array
            // up to the lastIndexStored
            printArray(noRepeatCharacters, lastIndexStored);
     
            // Call the method to sort the noReapeatCharacters array
            // up to the lastIndexStored
            sortArray(noRepeatCharacters, lastIndexStored);
     
            // Call the method to print the sorted noReapeatCharacters array
            // up to the lastIndexStored
            printArray(noRepeatCharacters, lastIndexStored);
     
        } // end of main
     
    } // end of Program6
    Last edited by helloworld922; November 12th, 2011 at 12:46 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Can Anyone Help With Arrays?

    So what part specifically do you need help on?

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Can Anyone Help With Arrays?

    I have seen your code, it's not so small to have a look at all. Mention the part where you need help, ask questions in a smart way and get answers by Smart People here in Smart Way

  4. #4
    Member
    Join Date
    Nov 2011
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can Anyone Help With Arrays?

    Hi,
    Please just tell i f you are getting any exception or what is your specific issue.
    Debug your code by putting some breakpoints and let us know.

    Core java
    Last edited by mr.miku; January 11th, 2012 at 07:12 PM.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can Anyone Help With Arrays?

    I used the debugger and for some reason it is not passing the array. I cannot figure out the code for going through the array, using the boolean method isCharacterPresent, and store no repeat characters.

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Can Anyone Help With Arrays?

    Please provide SSCCE

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can Anyone Help With Arrays?

    Quote Originally Posted by metaleddie13 View Post
    I used the debugger and for some reason it is not passing the array. I cannot figure out the code for going through the array, using the boolean method isCharacterPresent, and store no repeat characters.
    try this
    first you have to create an int

    int x = -1;

    if(!isCharacterPresent(input.charAt(i), noRepeatCharacters, x))

    last++;
    noRepeatCharacters[x] = input.charAt(i);

    return x;

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can Anyone Help With Arrays?

    I have worked most of it out. But for some reason the string is converted into an array, but it is not held and printed. I don't know what's going on now.

     
    import java.util.Arrays;
    import javax.swing.*;
     
    public class Program6
    {
        // Modify the constant below to have your name instead of CIS Faculty
        public final static String TITLE_BAR = "Program 6 (ME)";
     
        // Write your class methods below
     
        // Passed the user input string and an array (size = length of the string)
        // for storing the chars of that string. Stores each char of the string to
        // the array.
        private static void storeAllCharacters(String input, char[] allCharacters)
        {
            for(int i = 0; i < input.length()-1; i++)
            {      
                allCharacters = input.toCharArray();
                //return;
            }   
        }
     
        private static int storeNoRepeatCharacters(String input,
                char[] noRepeatCharacters)
        {
            int stored = -1;
     
            for (int i = 0; i < input.lastIndexOf(input); i++)
            {
                if (isCharacterPresent(input.charAt(i), noRepeatCharacters, stored))
                {
                    noRepeatCharacters[++stored] = input.charAt(i);
                }
            }
            return stored;
        }
     
        private static boolean isCharacterPresent(char repeat, char[] noRepeat,
                int index)
        {
            for (int i = 0; i < index; i++)
            {
                if (noRepeat[i] == repeat)
                {
                    return true;
                }
            }
            return false;
        }
     
        private static void sortArray(char[] noRepeatCharacters, int lastIndexStored)
        {
            Arrays.sort(noRepeatCharacters, 0, lastIndexStored + 1);
        }
     
        private static void printArray(char[] allCharacters)
        {
            for (int i = allCharacters.length-1; i >= 0; i--)
            {
            if(!isPalindrome(""))
            {
                JOptionPane.showMessageDialog(null, "User input string is a palindrome " + "\n" + allCharacters[i]);
            }
            else
                JOptionPane.showMessageDialog(null, allCharacters[i], TITLE_BAR, 
                        JOptionPane.PLAIN_MESSAGE);
            }
         }
     
     
        private static void printArray(char[] noRepeatCharacters,
                int lastIndexStored)
        {
            String output = "";
            String output2 = "";
     
            for (int i = 0; i <= noRepeatCharacters.length - 1; i++)
            {
                output += noRepeatCharacters[i];
            }
     
            for (int i = noRepeatCharacters.length - 1; i <= lastIndexStored; i--)
            {
                output2 += noRepeatCharacters[i];
            }
            JOptionPane.showMessageDialog(null, output + "\n" + output2, TITLE_BAR, 
                    JOptionPane.PLAIN_MESSAGE); 
        }
     
        private static boolean isPalindrome(String input)
        {
            int lowNum = 0;
            int highNum = input.length() - 1;
     
            while (lowNum < highNum)
            {
                if (input.charAt(lowNum) != input.charAt(highNum))
                    return false;
     
                lowNum++;
                highNum--;
            }
            return true;
        }
     
        // Main method provided
        // Do not modify the main method
        public static void main(String[] args)
        {
     
            // Local Variables.
            String input = "";
            char[] allCharacters;
            char[] noRepeatCharacters;
            int lastIndexStored;
     
            do
            {
                input = JOptionPane.showInputDialog("Enter a non-empty string");
                if (input == null)
                {
                    JOptionPane.showMessageDialog(null,
                            "User clicked cancel. The Program will exit",
                            TITLE_BAR, JOptionPane.PLAIN_MESSAGE);
                    System.exit(0);
                }
            } while (input.length() == 0);
     
            // Convert the string to all lower case
            input = input.toLowerCase();
     
            // Create the arrays using the length of the input string
            allCharacters = new char[input.length()];
            noRepeatCharacters = new char[input.length()];
     
            // Call the method to store the entire input string
            storeAllCharacters(input, allCharacters);
     
            // Call the method to print the entire allCharacters array
            printArray(allCharacters);
     
            // Call the method to store the characters without repeating
            // and assign its return value
            lastIndexStored = storeNoRepeatCharacters(input, noRepeatCharacters);
     
            // Call the method to print the noReapeatCharacters array
            // up to the lastIndexStored
            printArray(noRepeatCharacters, lastIndexStored);
     
            // Call the method to sort the noReapeatCharacters array
            // up to the lastIndexStored
            sortArray(noRepeatCharacters, lastIndexStored);
     
            // Call the method to print the sorted noReapeatCharacters array
            // up to the lastIndexStored
            printArray(noRepeatCharacters, lastIndexStored);
     
        } // end of main
     
    } // end of Program6
    Last edited by JavaPF; November 15th, 2011 at 05:03 PM. Reason: Use highlight tags

  9. #9
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Can Anyone Help With Arrays?

    add java tags please

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Can Anyone Help With Arrays?

    Your string is successfully printed after you input it.
    Debug it or use System.out.println(input), right after getting it from user. Your other function seems to be creating problem with it.
    Place System.out.println() after every line and try to find where problem lies.

Similar Threads

  1. Arrays
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 7th, 2011, 11:18 PM
  2. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  3. arrays
    By dwamalwa in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2010, 01:44 AM
  4. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM
  5. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM