Total members 10261 | Gratitudes |It is currently Wed May 23, 2012 10:11 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 4 posts ] 
Author Question
 Question subject: What's wrong with my code?
PostPosted: 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
;
    }
}
 


TOP
  
Reply with quote  
 Question subject: Re: What's wrong with my code?
PostPosted: Tue Nov 29, 2011 1:08 am 
Offline
Mastermind
User avatar

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 )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: What's wrong with my code?
PostPosted: 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:


TOP
  
Reply with quote  
 Question subject: Re: What's wrong with my code?
PostPosted: 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;
}
}


TOP
  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 4 posts ] 
Quick reply


  

 Similar topics
 Read your gmail using Java code
 Code to open multiple links.
 I need help with checkers code
 java code for listing folder contents from remote folder
 code for online payment system in java
 apriori algorithm java code
 playfair cipher assembly code
 code
 connecting PC with mobile using bluetooth code in java
 code hill cipher C++ Decrypt

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: Google [Bot] and 3 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team