Hey guys!

I have a school project and I cant figure out the coding to make it work correctly. This is what I am supposed to do:
Write a class with a main() method and two static methods. Your program should prompt the user for an integer that is no longer than five digits and no shorter than two digits. Your program should print out to the screen the integer written out as words. This should loop until the user enters the integer 0. For example:

What is your integer? 475

Your integer 475 is written out as four seven five.

What is your integer? 4755743

Your integer 4755743 is not between two and five digits in length.

What is your integer? 4

Your integer 4 is not between two and five digits in length.

What is your integer? 0

Quitting. Thank you for playing.

I currently have this but I dont think its correct:

import java.util.Scanner;

public class Project2 {

	//Engine that runs main program.
  public static void main(String... Args){    
   	
	//Prompts user to enter two to five random integers.
	 Scanner input = new Scanner(System.in);
	 
	 System.out.println("Enter a number that has two to five digits! " + " Enter 0 to exit application. ");
	 System.out.print(" What is you integer? ");
	 int userNumber = input.nextInt();	 
	
	while (true) {
	
	validateLength(userNumber);
    }
	
	
	}
	public static boolean validateLength(int userNumber); {
	
	if (userNumber < 2); {
	System.out.print("The number you entered is too short!");
	return false;}
	else if (userNumber > 5);{
	System.out.print("The number you entered is too long!");
	return false;}  
	else
	return true;
	}
  }

First of all, your code has syntax problems.. i don't think it'd even compile.
You put a semi-colon after instructions and not after loops or methods. Semi-colon means the end of an instruction. Line 22,24 and 27 of your code have semi-colons and they shouldn't be there.
Secondly, you need to make another method to actually write the numbers in words. Try to put some code about this method. Your method "validateLength" just checks if the integer is no longer than five digits and no shorter than two digits

Your method "validate length" doesn't actually check the length of your number, it only checks if the value is less than two, or greater than 5!

What you need to do is something like this:

public static boolean validateLength(int userNum) {
 String userNumber = userNum.toString();
	if (userNumber.length < 2) {
	System.out.print("The number you entered is too short!");
	return false;
        }
	else if (userNumber.length > 5) {
	System.out.print("The number you entered is too long!");
	return false;}  
	else
	return true;
	}

Finally, don't put semicolons after if statements, it breaks things.

Hey Pokenerd I tried the code you submitted and it is giving me a compile error saying

C:\JAVA>javac Project2.java
Project2.java:39: int cannot be dereferenced
          String userNumber = userNum.toString();
                                     ^
Project2.java:40: cannot find symbol
symbol  : variable length
location: class java.lang.String
        if (userNumber.length < 2) {
                      ^
Project2.java:43: cannot find symbol
symbol  : variable length
location: class java.lang.String
        else if (userNumber.length > 5) {
                           ^
3 errors

I know I have to do the class to change the integers to words but I have to get this part first before I can move to the next step.

Sorry about that, forgot i'm not working in C# ...

I believe you should use:

String.valueOf(yourintegerhere);
commented: Not quite. +0

if you want the string of the number its

String userNumber = "" + Usernum;
commented: Wrong. +0

for comparing whether a number is between two and five (inclusive) digits in length, you can also do a test on whether it is grater or equal to ten and less than 100.000. another advantage of this is that someone could give your app an input such as 01, and although its length is 2, it still is wrong because the output would be 'zero one' which should be same as 'one'... in case you proceed with testing input length as a string length.

I am still having compile problem with this part:

if (userNumber.length < 2) {
System.out.print("The number you entered is too short!");
return false;
    }
else if (userNumber.length > 5) {
System.out.print("The number you entered is too long!");
return false;}  
else
return true;
}

Make sure all of the capitalization is correct, in java it matters. also could you post the errors your getting and where your getting them.

thats what im getting!

C:\JAVA>javac Project2.java
Project2.java:44: cannot find symbol
symbol  : variable length
location: class java.lang.String
        if (userNumber.length < 2) {
                      ^
Project2.java:49: cannot find symbol
symbol  : variable length
location: class java.lang.String
        else if (userNumber.length > 5) {
                           ^
2 errors

thats what im getting!

C:\JAVA>javac Project2.java
Project2.java:44: cannot find symbol
symbol  : variable length
location: class java.lang.String
        if (userNumber.length < 2) {
                      ^
Project2.java:49: cannot find symbol
symbol  : variable length
location: class java.lang.String
        else if (userNumber.length > 5) {
                           ^
2 errors 

end quote.

i see you are using a command prompt, it would be much easier if you use a IDE like eclipse or netbeans becuase it tells you where your errors are before you compile it.

Is userNumber a String?(In the original code it isn't, but after following the posts it might be now)
If userNumber is a String, you have to put parenthesis after length because it is a method of the String class
Ex.

if(userNumber.length()<2){
//your code
}
else if(userNumber.length > 5){
//your code
}

I'm not sure what just happened, when I clicked Post Reply it posted in this thread instead of the one I was looking at.
Sorry

I'm not sure what just happened, when I clicked Post Reply it posted in this thread instead of the one I was looking at.
Sorry

no... this is the right thread, just the second page of posts

Oh, That really freaked me out.

I forgot to put the parenthesis in the else if statement

if(userNumber.length()<2){
//your code
}
else if(userNumber.length() > 5){
//your code
}

This should fix the compile time error.
And yes, Progr4mmer, is right an IDE can help out a lot especially when you are first learning.

Member Avatar for ztini

if you want the string of the number its

String userNumber = "" + Usernum;

Wrong.

String oneStr = "1";

int oneInt = Integer.parseInt(oneStr); // String to Integer
oneStr = Integer.toString(oneInt); // Integer to String

Likewise, you can use Double, Float, with the same syntax.

All right guys this is exactly what I have to do and what I have so far.

Write a class with a main() method and two static methods. Your program should prompt the user for an integer that is no longer than five digits and no shorter than two digits. Your program should print out to the screen the integer written out as words. This should loop until the user enters the integer 0. For example:

What is your integer? 475

Your integer 475 is written out as four seven five.

What is your integer? 4755743

Your integer 4755743 is not between two and five digits in length.

What is your integer? 4

Your integer 4 is not between two and five digits in length.

What is your integer? 0

Quitting. Thank you for playing.

Your static methods will be:

public static boolean validateLength(int); // returns true if the length of the int parameter is 2 digits or longer but 5 digits or shorter. Returns false otherwise.

public static String covertIntegerToWords(int); // returns a string of the words that represent the integers passed as arguments.

import java.util.Scanner;

public class Project2 {

	//Engine that runs main program.
  public static void main(String... Args){    
   	
	 Scanner input = new Scanner(System.in);
	 
	 //Prompts user to enter two to five random integers.
	 System.out.println("Enter a number that has two to five digits! " + " Enter 0 to exit application. ");
	 System.out.print(" What is you integer? ");
	 
	 int userNumber = input.nextInt();	 
	
	while (userNumber >= 0) {
	
	if (userNumber == 0) 
	System.out.print( "Thank you for playing! " + "Good bye! ");	
        
	
	else if (userNumber != 0)
	validateLength(userNumber0);
	   }
	}
	public static boolean validateLength(int userNum) {
		String userNumber = "" + userNum;
		
	if (userNumber.length() < 2) {
	System.out.print("The number you entered is too short!");
	  return false;
        }
		
	else if (userNumber.length() > 5) {
	System.out.print("The number you entered is too long!");
	  return false;} 
	
	else
	  return true;
	}
	public static String covertIntegerToWords(int);
	
	
	
  }

I know a lot of the stuff is wrong. I am not getting compile errors, but I cant seem to close the loops as they are all infite for some reason. Also I cannot think of a way to translate the integers to words, I think I have to use an array list underneath the String covertIntegerToWords. Can you guys please give me some feedback on what I should fix?

you might be able to use a case statement for convertIntegerToWords heres some info on that. Also it should be

public static String covertIntegerToWords(int a){}

Also to break out of the loop try this

while (userNumber >= 0) {
	
	if (userNumber == 0){
	System.out.print( "Thank you for playing! " + "Good bye! ");
        break;
        }
        
	
	else if (userNumber != 0)
	validateLength(userNumber0);
}

Your while loop is infinite because you do not as the user to input another value for userNumber.
This is just a general structure you should use for your while loop:

int userNumber = input.nextInt();
while(userNumber != 0)
{
   //do your calculations 
   //print out the results
   //prompt the user to enter another value for userNumber
   System.out.println("Enter a number or Enter 0 to quit");
   userNumber = input.nextInt();
}

Your while loop is infinite because the value of userNumber never changes inside the while loop. You should ask the user for the next number at the very bottom of the while loop, so that way when they enter 0 it will check the condition at the top of the while loop and exit the loop.

All right guys this is what I put togheter so far but It is giving me a this error when I compile:
C:\JAVA>javac Project2.java
Project2.java:73: incompatible types
found : boolean
required: java.lang.String
return false;

The thing is that I dont think the loop that I have is gonna make everything work correctly. Also I dont know if I am calling the method at the right time. Lastly, I dont understand well how to string the result and if I am doing the coding right for it. Please help I have to turn this in by the end of the week.

import java.util.Scanner;

public class Project2 {

	//Engine that runs main program.
  public static void main(String... Args){    
   	
	 Scanner input = new Scanner(System.in);
	 
	 //Prompts user to enter two to five random integers.
	 System.out.println("Enter a number that has two to five digits! " + " Enter 0 to exit application. ");
	 System.out.print(" What is you integer? ");
	 
	 int userNumber = input.nextInt();	 
	
	while (userNumber >= 0) {
	
	if (userNumber == 0) {
	System.out.print( "Thank you for playing! " + "Good bye! ");	
    break;    }
	
	else if (userNumber != 0)
	validateLength(userNumber);
	
	   }
	}
	public static boolean validateLength(int userNum) {
		String userNumber = "" + userNum;
		
	if (userNumber.length() < 2) {
	System.out.print("The number you entered is too short!");
	  return false;
        }
		
	else if (userNumber.length() > 5) {
	System.out.print("The number you entered is too long!");
	  return false;} 
	
	else
	  return true;
	}
	public static String convertIntegerToWords(int numWord) {
	
	String numWordString;
	
	switch (numWord) {
	
	case 1: numWordString = "One";
	case 2: numWordString = "Two";
	case 3: numWordString = "Three";
	case 4: numWordString = "Four";
	case 5: numWordString = "Five";
	case 6: numWordString = "Six";
	case 7: numWordString = "Seven";
	case 8: numWordString = "Eight";
	case 9: numWordString = "Nine";
	  }
	  return false;
	}
	
  }

instead of

return false;

its should be

return numWordString;

also add

break;

to each of the cases

for the loop it should be

while (userNumber >= 0) {
	
	if (userNumber == 0) {
	System.out.print( "Thank you for playing! " + "Good bye! ");	
    break;    }
	
	else if (userNumber != 0){
	     if(validateLength(userNumber) == true){
	          System.out.print(convertIntegerToWords(one place of the number) + convertIntegerToWords(another place of the number) + convertIntegerToWords(another place of the number));
             }
            }
	   }

All right its looking good now programmer, but I am kind of confused with this:
convertIntegerToWords(one place of the number)
What do you mean by one place of the number?

Member Avatar for ztini

I disagree with the overall logic of this solution. If I enter "256", the solution should not be "two five six", it should be "two hundred and fifty six". One way to solve this is with recursion. Notice in the example below how the method continually calls itself until completion.

import java.util.InputMismatchException;
import java.util.Scanner;

public class NumberConvertor {
	
	private final static int MAX = 99999, MIN = 1;
	
	public NumberConvertor() {
		System.out.println("Integer to String Conversion");
		System.out.println("Accepted Values: " + MIN + " to " + MAX);
		System.out.println();
		
		displayMenu();
	}
	
	private void displayMenu() {
		System.out.print("Integer: ");
		
		try {	
			int value = new Scanner(System.in).nextInt();
			System.out.print("English: " + toEnglish(value));
		}
		catch (InputMismatchException e) {
			System.out.print("English: Error");
		}
		
		System.out.println();
		System.out.println();
		
		System.out.print("Again? (Y/N) ");
		switch (new Scanner(System.in).next().charAt(0)) {
		case 'n':
		case 'N': System.out.println("\nGood-Bye!"); break;
		default: System.out.println(); displayMenu();
		}
	}

	private String toEnglish(int num) {
		String english = "";
		
		if (num > MAX || num < MIN)
			return "Error";
		
		if (num > 99) {
			switch (Integer.toString(num).length()) {
			case 3: 
				english += toEnglish(num / 100) + " Hundred ";
				if (num - (num / 100) * 100 > 0)
					english += "and ";
				english += toEnglish(num - (num / 100) * 100); 
				break;
			case 4:
			case 5:  english += toEnglish(num / 1000) + " Thousand " +
				toEnglish(num - (num / 1000) * 1000); break;
			}
		} 
		
		else if (num >= 20) {
			switch (num / 10) {
			case 2: english += "Twenty "; break;
			case 3: english += "Thirty "; break;
			case 4: english += "Forty "; break;
			case 5: english += "Fifty "; break;
			case 6: english += "Sixty "; break;
			case 7: english += "Seventy "; break;
			case 8: english += "Eighty "; break;
			case 9: english += "Ninety "; break;
			}
			english += toEnglish(num - (num / 10) * 10);
		}
		
		else if (num < 20) {
			switch(num) {
			case 1: return "One";
			case 2: return "Two";
			case 3: return "Three";
			case 4: return "Four";
			case 5: return "Five";
			case 6: return "Six";
			case 7: return "Seven";
			case 8: return "Eight";
			case 9: return "Nine";
			case 10: return "Ten";
			case 11: return "Eleven";
			case 12: return "Twelve";
			case 13: return "Thirteen";
			case 14: return "Fourteen";
			case 15: return "Fifteen";
			case 16: return "Sixteen";
			case 17: return "Seventeen";
			case 18: return "Eighteen";
			case 19: return "Nineteen";
			default: return "";
			}
		}
		
		return english;
	}
	
	public static void main(String[] args) {
		new NumberConvertor();
	}
}

Console:

Integer to String Conversion
Accepted Values: 1 to 99999

Integer: meh
English: Error

Again? (Y/N) y

Integer: -2
English: Error

Again? (Y/N) y

Integer: 3.5
English: Error

Again? (Y/N) y

Integer: 934823894823324234
English: Error

Again? (Y/N) y

Integer: 256
English: Two Hundred and Fifty Six

Again? (Y/N) n

Good-Bye!

Ztini, this is a school project and the way im writing it is how the professor wants it. Read all the posts so you can understand better what i need exactly.

Member Avatar for ztini

Wow... really? You can't modify this to run in the main method? ...

import java.util.Scanner;

public class NumberConvertor {
	
	private final static int MAX = 99999, MIN = 10;

	private static String toEnglish(int num) {
		String english = "";
		
		if (num > MAX || num < MIN)
			return "Error";
		
		if (num > 99) {
			switch (Integer.toString(num).length()) {
			case 3: 
				english += toEnglish(num / 100) + " Hundred ";
				if (num - (num / 100) * 100 > 0)
					english += "and ";
				english += toEnglish(num - (num / 100) * 100); 
				break;
			case 4:
			case 5:  english += toEnglish(num / 1000) + " Thousand " +
				toEnglish(num - (num / 1000) * 1000); break;
			}
		} 
		
		else if (num >= 20) {
			switch (num / 10) {
			case 2: english += "Twenty "; break;
			case 3: english += "Thirty "; break;
			case 4: english += "Forty "; break;
			case 5: english += "Fifty "; break;
			case 6: english += "Sixty "; break;
			case 7: english += "Seventy "; break;
			case 8: english += "Eighty "; break;
			case 9: english += "Ninety "; break;
			}
			english += toEnglish(num - (num / 10) * 10);
		}
		
		else if (num < 20) {
			switch(num) {
			case 1: return "One";
			case 2: return "Two";
			case 3: return "Three";
			case 4: return "Four";
			case 5: return "Five";
			case 6: return "Six";
			case 7: return "Seven";
			case 8: return "Eight";
			case 9: return "Nine";
			case 10: return "Ten";
			case 11: return "Eleven";
			case 12: return "Twelve";
			case 13: return "Thirteen";
			case 14: return "Fourteen";
			case 15: return "Fifteen";
			case 16: return "Sixteen";
			case 17: return "Seventeen";
			case 18: return "Eighteen";
			case 19: return "Nineteen";
			default: return "";
			}
		}
		
		return english;
	}
	
	public static void displayMenu() {
		int input;
		
		do {
			System.out.print("What is your integer? ");
			
			input = new Scanner(System.in).nextInt();
			
			System.out.println();
			System.out.print("Your integer " + input + " is ");
			
			if (input < MIN || input > MAX)
				System.out.println("not between two and five digits in length.");
			else
				System.out.println("written out as " + toEnglish(input) + ".");
			
			System.out.println();
			
		} while (input != 0);

		System.out.println("Quitting. Thank you for playing.");
	}
	
	public static void main(String[] args) {
		displayMenu();
	}
}
What is your integer? 475

Your integer 475 is written out as Error Hundred and Seventy Error.

What is your integer? 4755743

Your integer 4755743 is not between two and five digits in length.

What is your integer? 4

Your integer 4 is not between two and five digits in length.

What is your integer? 0

Your integer 0 is not between two and five digits in length.

Quitting. Thank you for playing.

Are those your examples in the original post or the professors? It says to read the integer as "words", which would be the above logic. If not, just read each char in the input and run against a simple switch.

Yeah manthose are the Proff examples, he wants it a very specific way. I have to have exactky what he asks for with the method names with th exact name he asked forr :(

then really, time to write this code by yoursefl, no idea...

very helpfull mKorbel!!!!
Next time reserve the comments.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.