Total members 11894 |It is currently Thu Nov 21, 2024 2:08 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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





Author:

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

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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:


Author:

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


Author:
Post new topic Reply to topic  [ 4 posts ] 

  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     -  
 Set scope on main window     -  
 what will return by void main()     -  
 changing the background color for site main menu.     -  
 Show sidebar main menu with scroll down in the web page     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com