Can somebody please help me figure out where I went wrong?

This is the problem and I have attached my code.

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.

Your static methods will be:

public static boolean validateLength(int); // returns true if the length of the int parameter is 1 digit long. Returns false otherwise.

public static String covertIntegerToWords(int); // returns a string of the word that represent the integer passed as an argument.

Attachments
import java.util.Scanner;

public class ProjectTwo {
	// 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, 0 to quit
		System.out.println("Enter a single digit integer" + "Enter 0 to exit");
				int digit = input.nextInt();
				int number;
		
		while (digit >= 0) {
			//  Sentinal statement to exit loop
			if (digit == 0) {
			System.out.print( "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;
	}
}
import java.util.Scanner;

public class ProjectTwo {
	// 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, 0 to quit
		System.out.println("Enter a single digit integer" + "Enter 0 to exit");
				int digit = input.nextInt();
				int number;
		
		while (digit >= 0) {
			//  Sentinal statement to exit loop
			if (digit == 0) {
			System.out.print( "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;
	}
}

lines 24 and 70;

it says variable number might not have been initialized, and variable numberString might not have been initialized.

Everytime I make a correction, though, I end up with a bizillion other errors

I am usually pretty good at troubleshooting my code, so I didn't think I was stuck, until I was stuck...

"might not have been initialized" means exactly what it says. You declare number on line 12 but you don't assign any value to it before you use it on line 24. Maybe a confusion between number and digit?

numberString is a bit more subtle. You know from your overall design that your switch covers every possible value, but that's too hard for the Java compiler to work out, so it's worried about cases like 0 or 10 where none of your cases will be executed, and numberString will not have a value assigned before you get to line 70. A good fix would be to add a default case to your switch that sets numberString to "error" or somesuch. Taht way the compiler will know that numberString will always get a value assigned before it's returned.

THANK YOU! OK, moving along! I renamed my variables and set initial values. I also took out a couple lines after the loop that was causing it to repeat itself. Now, my only problem is that I can't seem to get it to return a word value, all I'm getting for output is 1.

import java.util.Scanner;

public class Help {
	// 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, 0 to quit
		System.out.println("Enter a single digit integer" + "Enter 0 to exit");
				int userDigit = input.nextInt();
				int numWord = 1;
		
		while (userDigit >= 0) {
			//  Sentinal statement to exit loop
			if (userDigit == 0) {
			System.out.print( "Good-bye!");
			break; }
			// Continue into loop
			else if (userDigit != 0)
				// Call public static boolean validateLength(userDigit) method to validate integer is only one digit
				if(validateLength(userDigit) == true){
				// Call public static String convertIntegerToWords(number) method to convert integer "digit" to word form
				convertIntegerToWords(numWord);
				// Output the word value for the integer entered
				System.out.println ("The number " + userDigit + " is spelled " + numWord );
				// 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.");
				userDigit = input.nextInt();
			}
		}
}	
	// Method to validate length of integer	
	public static boolean validateLength(int userDigit) {
 
			// Validate length of integer
			if ((userDigit > 0) && (userDigit < 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 numWord) {
		String numWordString;
		numWordString = "zero";
		switch (numWord) {
			case 1: numWordString = "one";
				break;
			case 2: numWordString = "two";
				break;
			case 3: numWordString = "three";
				break;
			case 4: numWordString = "four";
				break;
			case 5: numWordString = "five";
				break;
			case 6: numWordString = "six";
				break;
			case 7: numWordString = "seven";
				break;
			case 8: numWordString = "eight";
				break;
			case 9: numWordString = "nine";							
				break;
			default: System.out.println("Errors; invalid status");
				System.exit (0);
		}	
		return numWordString;
	}
}

I see that this is because of the value that I assigned numWord at the beginning, and because numWord does not change. I need to make numWord take on the value of userDigit as an argument. How do I make this happen? My brain cells aren't working very well at this point.

convertIntegerToWords returns a String, so you just need to assign that to a String variable:
String theAnswer = convertIntegerToWords(...)

I can't seem to figure out how I've created an infinite loop. Only when I enter an integer greater than 9 and when I assign convertIntegerToWords(...) to a String variable. UGH! I know this should be simple....

Well, you're right, this should be simple! Without the latest version of your code all I can do is advise you to add more debugging print statements so you can follow the values of your variables and the flow of execution.

import java.util.Scanner;

public class Help {
	// 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, 0 to quit
		System.out.println("Enter a single digit integer." + " Enter 0 to exit");
				int userDigit = input.nextInt();
				int numWord = userDigit;
		
		while (userDigit >= 0) {
			//  Sentinal statement to exit loop
			if (userDigit == 0) {
			System.out.print( "Good-bye!");
			break; }
			// Continue into loop
			else if (userDigit != 0)
				// Call public static boolean validateLength(userDigit) method to validate integer is only one digit
				if(validateLength(userDigit) == true){
				// Call public static String convertIntegerToWords(numWord) method to convert integer "userDigit" to word form
				convertIntegerToWords(numWord);
				// Output the word value for the integer entered
				System.out.println("The number " + userDigit + " is spelled " + convertIntegerToWords(numWord));
				// Prompt user to enter another integer or press 0 to quit
				System.out.print("Enter a number or press 0 to quit.");
				userDigit = input.nextInt();	

				// Ensures loop will be finite
				if(validateLength(userDigit) == false) 
				System.out.println("Enter a number or press 0 to quit.");
				userDigit = input.nextInt();
				
			}
				
		}
}	
	// Method to validate length of integer	
	public static boolean validateLength(int userDigit) {
 
			// Validate length of integer
			if ((userDigit > 0) && (userDigit < 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 numWord) {
		String numWordString;
		numWordString = "Error";
		switch(numWord) {
			case 1: numWordString = ("one"); 
				break;
			case 2: numWordString = ("two"); 
				break;
			case 3: numWordString = ("three"); 
				break;
			case 4: numWordString = ("four"); 
				break;
			case 5: numWordString = ("five"); 
				break;
			case 6: numWordString = ("six"); 
				break;
			case 7: numWordString = ("seven"); 
				break;
			case 8: numWordString = ("eight"); 
				break;
			case 9: numWordString = ("nine"); 
				break;
			default: System.out.println("Errors; invalid status");
				System.exit(0);
		}	
		return numWordString;
	}
}

I have a problem in my loop,
1-I can exit with 0 on the fist pass, but as a subsequent pass, the exit command is getting stuck in my validateLength method, and I can't seem to change it
2-if I enter an integer larger than 9, I get in an infinite loop
3-I whatever integer I enter, returns the correct word, the first time, all other entries return that word as well 7 outputs seven, 8 outputs seven, etc.

I changed the boolean value to >= and now the 0 to exit doesn't get trapped by the too long message, but I still have to input 0[enter] twice in order to quit

// Validate length of integer
			if ((userDigit >=0) && (userDigit < 10))
				return true;

I fixed the exit command. and got rid of the infinite loop, however still have the probs with the number names still saying the value of the first input integer, and now, there is no more output when too large of a value is input.

This article has been dead for over six months. Start a new discussion instead.