Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

main method and two static methods and loop until input zero

Mon Nov 28, 2011 11:38 am

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
;
    }
}
 




Re: What's wrong with my code?

Tue Nov 29, 2011 1:08 am

i can't find any problem with your code !! it looks nice.

Re: What's wrong with my code?

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.. :gOOd:

Re: What's wrong with my code?

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;
}
}

Post a reply
  Related Posts  to : main method and two static methods and loop until input zero
 Static Methods and Variables     -  
 Can we override static methods in java     -  
 What is a static method     -  
 argument type of a program's main() method     -  
 pass parameters to main method args[] variables     -  
 difference between a static and a non-static inner class     -  
 what will return by void main()     -  
 Set scope on main window     -  
 Show sidebar main menu with scroll down in the web page     -  
 changing the background color for site main menu.     -