...

View Full Version : New Guy Programmer...



BLWebster
01-24-2011, 09:48 PM
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;
}
}
}
}

DistantJob
02-04-2011, 04:37 AM
I've corrected your project, here is the working and tested 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum