-
Logic Error That has escaped me
There is an error in my code thus far, which has managed to escape me, so I pose it to you guys. It is supposed to take a long number and count how many of the same there are. What happens though is you need to eneter 1+ the numbers. Ex. 3892478 it would print out the any numbers to 6. So for all 9 to be accounted for, one would need to put in 10 numbers.
public class Parser3 {
public static void main(String [] args) {
//declare variables
int [] frequencyArray = new int [10];
String userInput;
String [] numbers = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
// ask user for input
System.out.print("Please input your student number: ");
userInput= MyInput.readString();
//Take the user input and count the number of occurences and put the occurences into an array
for(int i = 0; i < userInput.length(); i ++) {
if (userInput.charAt(i) == '0') {
frequencyArray[0] ++;
continue;
} else if (userInput.charAt(i) == '1') {
frequencyArray[1] ++;
continue;
} else if (userInput.charAt(i) == '2') {
frequencyArray[2] ++;
continue;
} else if (userInput.charAt(i) == '3') {
frequencyArray[3] ++;
continue;
} else if (userInput.charAt(i) == '4') {
frequencyArray[4] ++;
continue;
} else if (userInput.charAt(i) == '5') {
frequencyArray[5] ++;
continue;
} else if (userInput.charAt(i) == '6') {
frequencyArray[6] ++;
continue;
} else if (userInput.charAt(i) == '7') {
frequencyArray[7] ++;
continue;
} else if (userInput.charAt(i) == '8') {
frequencyArray[8] ++;
continue;
} else if (userInput.charAt(i) == '9') {
frequencyArray[9] ++;
continue;
}//end if statements
}//end for
System.out.println("In the number " + userInput + " there are: ");
//Print out the values in the array with the appropriate ending
for(int j = 0; j <userInput.length(); j ++) {
if (frequencyArray[j] > 0) {
System.out.println(frequencyArray[j] + " " + numbers[j] + "(s)");
}//end if
}//end for
// Print the highest value in the array
//Print out the word equivalent of the numbers
}//end main
I can also use suggestions on how to find the value that is enetered most often, and how to print out the word equivalent (ex. 37263 = Three, Seven, Two, Six, Three)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks