Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy New Guy Programmer...

    OK folks,
    Taking an intermediate JavaII and kickin around some code.
    This weeks assignment has got some Loop repeat stuck in it somewhere and I can't seem to find and irradicate it.

    Quik fix help would be appreciated as this is a supplement to a program I did really lousy on and I need the extra points!

    Thanks my wise Forum folks.

    import java.util.LinkedList;
    import java.util.ListIterator;
    import java.util.Arrays;

    public class BrianWebsterWeek2Lab {
    public static void main(String []args) {
    //Create LinkedList and display
    String fullname = "Brian Webster \n";
    System.out.print("\nMy Name is: ");

    LinkedList <String> me = new LinkedList <String>();
    me.add (fullname);
    ListIterator <String> iterator = me.listIterator();
    for (String letter: me) {
    System.out.print(iterator.next());
    }
    //Character sort of letters of fullname
    char charsort[] = fullname.toCharArray();
    Arrays.sort(charsort);
    System.out.print("\nLetters of my name in Sorted order: ");
    for(int index=0; index < charsort.length;index++) {
    System.out.print(" " + charsort[index]);}
    System.out.println();

    //Binary Search for Vowels
    String string = fullname.toLowerCase();
    char[] keys = {'a','e','i','o','u'};
    for (char check : keys){
    System.out.println("\nBinary Search Results: ");
    for (char key : keys){
    System.out.print("\nVowels: " + key);
    int index = Arrays.binarySearch(keys, key);
    if(index > 0){
    System.out.print(" Found");
    } else {
    System.out.print(" Not Found");
    }
    }
    //Linear Search for Vowels
    int a = 0, e = 0, i = 0, o = 0, u = 0;
    for (int j = 0; j < string.length(); j++){
    char test_vowel = fullname.charAt(j);
    if (test_vowel == 'a')
    a++;
    else if (test_vowel == 'e')
    e++;
    else if (test_vowel == 'i')
    i++;
    else if (test_vowel == 'o')
    o++;
    else if (test_vowel == 'u')
    u++;

    System.out.print("\n\nLinear Search Results: \n");
    System.out.print("\n A's: " + a);
    System.out.print("\n E's: " + e);
    System.out.print("\n I's: " + i);
    System.out.print("\n O's: " + o);
    System.out.print("\n U's: " + u + "\n");
    break;
    }
    }
    }
    }

  • #2
    New Coder
    Join Date
    Feb 2011
    Posts
    48
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I've corrected your project, here is the working and tested code:

    Code:
    import java.util.LinkedList;
    import java.util.ListIterator;
    import java.util.Arrays;
    
    public class BrianWebsterWeek2Lab {
    
        public static void main(String[] args) {
    //Create LinkedList and display
            String fullname = "Brian Webster \n";
            System.out.print("\nMy Name is: ");
    
            LinkedList<String> me = new LinkedList<String>();
            me.add(fullname);
            ListIterator<String> iterator = me.listIterator();
            for (String letter : me) {
                System.out.print(iterator.next());
            }
    //Character sort of letters of fullname
            char charsort[] = fullname.toCharArray();
            Arrays.sort(charsort);
            System.out.print("\nLetters of my name in Sorted order: ");
            for (int index = 0; index < charsort.length; index++) {
                System.out.print(" " + charsort[index]);
            }
            System.out.println();
    
    //Binary Search for Vowels
            String string = fullname.toLowerCase();
            char[] nameArray = fullname.toLowerCase().toCharArray();
            Arrays.sort(nameArray);
            char[] keys = {'a', 'e', 'i', 'o', 'u'};
            //for (char check : keys) {
                System.out.println("\nBinary Search Results: ");
    
                for (char key : keys) {
                    System.out.print("\nVowels: " + key);
                    int index = Arrays.binarySearch(nameArray, key);
                    if (index >= 0) {
                        System.out.print(" Found");
                    } else {
                        System.out.print(" Not Found");
                    }
                }
    //        }
    //Linear Search for Vowels
            int a = 0, e = 0, i = 0, o = 0, u = 0;
            for (int j = 0; j < string.length(); j++) {
                char test_vowel = fullname.charAt(j);
                if (test_vowel == 'a') {
                    a++;
                } else if (test_vowel == 'e') {
                    e++;
                } else if (test_vowel == 'i') {
                    i++;
                } else if (test_vowel == 'o') {
                    o++;
                } else if (test_vowel == 'u') {
                    u++;
                }
            }
    
            System.out.print("\n\nLinear Search Results: \n");
            System.out.print("\n A's: " + a);
            System.out.print("\n E's: " + e);
            System.out.print("\n I's: " + i);
            System.out.print("\n O's: " + o);
            System.out.print("\n U's: " + u + "\n");
    //}
        }
    }
    Good luck with the extra points.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •