Can somebody please tell me what I've done wrong and how to fix it? I'm ready to bash my skull!
This is the problem: Write a class with a main() method and two static methods. Your program should prompt the user for a single-digit integer. Your program should print out to the screen the integer written out as a word. This should loop until the user enters the integer 0
This is my code:
Code:
import java.util.Scanner;
public class ProjectTwoFrost { // Main method public static void main(String[] args){ // Create a scanner Scanner input = new Scanner(System.in); // Prompt the user to enter a single digit integer System.out.println("Enter a single digit integer"+"Enter 0 to exit"); System.out.print("Integer?"); int digit = input.nextInt(); int number; while (digit >= 0){ // Sentinal statement to exit loop if (digit == 0){ System.out.print("Thank you for playing! "+"Good bye! "); break;} // Continue into loop else if (digit != 0) // Call public static boolean validateLength(digit) method to validate integer is only one digit if(validateLength(digit)== true){ // Call public static String convertIntegerToWords(number) method to convert integer "digit" to word form convertIntegerToWords(number); // Output the word value for the integer entered System.out.println ("Your number "+ digit +" in words is "+ number ); // Prompt user to enter another integer or press 0 to quit // Ensures loop will be finite System.out.print("Enter a number or press 0 to quit."); digit = input.nextInt(); } System.out.print("Enter a number or press 0 to quit."); digit = input.nextInt(); } } // Method to validate length of integer public static boolean validateLength(int digit){ // Validate length of integer if ((digit > 0)&&(digit < 10)) return true; else System.out.println("The integer is too long"); return false; } // Method to convert integer into words using a switch statement public static String convertIntegerToWords(int number){ String numberString; switch (number){ case 1: numberString ="one"; break; case 2: numberString ="two"; break; case 3: numberString ="three"; break; case 4: numberString ="four"; break; case 5: numberString ="five"; break; case 6: numberString ="six"; break; case 7: numberString ="seven"; break; case 8: numberString ="eight"; break; case 9: numberString ="nine"; break; } return numberString; } }
msi_333
Question subject: Re: What's wrong with my code?
Posted: Tue Nov 29, 2011 1:08 am
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2279 Location: Earth Has thanked: 39 time Have thanks: 61 time
i can't find any problem with your code !! it looks nice.
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
Achauhan
Question subject: Re: What's wrong with my code?
Posted: Fri Dec 02, 2011 8:07 am
Your code is fine.. except..below thing.. converttoString(digit); // here you are passing number which is not initialzed... so default values is 0 System.out.println("Your number " + digit + " in words is " + numString);// here u need to print numString. after making above changes it works fine.. atleast for me..
harikesavulu
Question subject: Re: What's wrong with my code?
Posted: Tue Jan 24, 2012 1:51 pm
package src;
import java.util.Scanner;
public class Exa {
static String numberString = null; // Main method public static void main(String[] args) {
Exa ex=new Exa(); // Create a scanner Scanner input = new Scanner(System.in);
// Prompt the user to enter a single digit integer System.out.println("Enter a single digit integer" + "Enter 0 to exit"); System.out.print("Integer?"); int digit = input.nextInt();
while (digit >= 0) { // Sentinal statement to exit loop if (digit == 0) { System.out.print( "Thank you for playing! " + "Good bye! "); break; } // Continue into loop else if (digit != 0) // Call public static boolean validateLength(digit) method to validate integer is only one digit if(validateLength(digit) == true){ // Call public static String convertIntegerToWords(number) method to convert integer "digit" to word form ex.convertIntegerToWords(digit); // Output the word value for the integer entered System.out.println ("Your number " + digit + " in words is " + numberString ); // Prompt user to enter another integer or press 0 to quit // Ensures loop will be finite System.out.print("Enter a number or press 0 to quit."); digit = input.nextInt(); } System.out.print("Enter a number or press 0 to quit."); digit = input.nextInt(); } } // Method to validate length of integer public static boolean validateLength(int digit) {
// Validate length of integer if ((digit > 0) && (digit < 10)) return true;
else System.out.println("The integer is too long"); return false; } // Method to convert integer into words using a switch statement public String convertIntegerToWords(int digit) {
switch (digit) { case 1: numberString = "one"; break; case 2: numberString = "two"; break; case 3: numberString = "three"; break; case 4: numberString = "four"; break; case 5: numberString = "five"; break; case 6: numberString = "six"; break; case 7: numberString = "seven"; break; case 8: numberString = "eight"; break; case 9: numberString = "nine"; break; } return numberString; } }