0

all right this is what I have:

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)
				 if(validateLength(userNumber) == true){
				System.out.print(convertIntegerToWords(userNumber) + convertIntegerToWords(userNumber) + convertIntegerToWords(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 0: numWordString = "Zero"; break;
	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;
	  }
	  return numWordString;
	  
	}
	
  }

But i am still having loop problems when I in put less than two or more than five numbers. Also If I input 2-5 integers it get stuck and doesnt do anything. Please help!!!

0

You are not asking for the userNumber inside the loop, so the value of userNumber never changes inside the loop which cause the program to stay in the loop forever.

At line 28 add:

System.out.println("Enter a number or press 0 to quit.");
userNumber = input.nextInt();

I feel like I've said this already, maybe it was a different thread.

0

All right, that fixed the loop when I type the right number but its not showing me the conversion from integer to words.

0

Of course it isn't. Let's say I put in "2357" as my input number. What are you expecting this statement to do?

System.out.print(convertIntegerToWords(2357) + convertIntegerToWords(2357) + convertIntegerToWords(2357));

I don't see that number in your switch statement. Do you? And why make the exact same method call exactly three times?

0

yap, you should put a loop inside the convertIntegerToWords(int numWord)
so that it will loop "for each character" in the numWord.

String numString = ""+numWord;
String output = "";
for (char numChar: numString.toCharArray() ){
switch (numChar){
case '0': output += "ZERO "; break;
case '1': output += "ONE "; break;

}
}
return output;
0

All right devi, I did that code. What I am having problems now with is on how to string it back to the main method so it can be displayed in the console. This is what I have:

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)
				 if(validateLength(userNumber) == true){
				 convertIntegerToWords(userNumber);
				 System.out.println ("Your number " + userNumber + " in words is " + output );
				 System.out.print("Enter a number or press 0 to quit.");
				 userNumber = input.nextInt();
			
			}
 	   }
	}

Is that correct?

Also Im still having problems when I input a integer shorter than 2 and longer than 5, it goes to an infinite loop and I cant use break to close it.

Edited by Puertorro: n/a

0

try

String output = convertIntegerToWords(userNumber);
System.out.println ("Your number " + userNumber + " in words is " + output );

or u can have a short cut:

System.out.println ("Your number " + userNumber + " in words is " + convertIntegerToWords(userNumber) );

hmm u will have to put this in the last part of ur While loop

System.out.print("Enter a number or press 0 to quit.");
userNumber = input.nextInt();

do not put it inside the if statements

0

devi this is what the console is giving me after following your code:
C:\JAVA>java Project2
Enter a number that has two to five digits! Enter 0 to exit application.
What is you integer? 22
Your number 22 in words is
Enter a number or press 0 to quit.

0

Lol figured it out, I didnt put ' ' in the case numbers. Now the last thing I need is to fix the loop when the worng integers are the input.

1
import java.util.Scanner;


public class NumberConversion {
	
	private static final int MAX = 99999, MIN = 10;
	
	public static String toEnglish(int input) {
		String line;
		if (input < MIN || input > MAX)
			line = "not between two and five digits in length.";
		else {
			String words = "";
			for (char i : Integer.toString(input).toCharArray())
				words += getWord(Integer.parseInt(Character.toString(i))) + " ";
			line = "written out as " + words + ".";
		}
		return line;
	}
	
	private static String getWord(int value) {
		switch (value) {
		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";
		default: return null;
		}
	}
	
	public static void main(String[] args) {
		int input;
		do {
			System.out.print("What is your integer? ");
			input = new Scanner(System.in).nextInt();
			System.out.println();
			System.out.println("Your integer " + input + " is " + toEnglish(input));		
			System.out.println();
		} while (input != 0);
		System.out.println("Quitting. Thank you for playing.");
	}
}

Console:

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

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

Quitting. Thank you for playing.
0

:)
before that, ur while loop should be

while(userNumber > 0)

ur code was already fine and it was following the specific instructions :D
just for ur validateLength() method,

public static boolean validateLength(int userNum) {
if (userNum < 10) {
System.out.print("The number you entered is too short!");
return false;
}
else if (userNum > 99999) {
System.out.print("The number you entered is too long!");
return false;}
else
return true;
}
0

not to mention that it wasn't about programming, nor Java, but only about basic algebra

Edited by mKorbel: n/a

Votes + Comments
Wrong.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.