I'm trying to write a code that validates that an integer is between 1 and 9. Then it prints out that integer as a word. User inputs 0 to exit program.

Example:

Enter an integer between 1 and 9
5
Integer entered, five

Enter an integer between 1 and 9
10
Integer is not beween 1 and 9

Enter an integer between 1 and 9
0
Goodbye

I am having an issue with the validating the integer is between 1 and 9 with if statements. As well as how to print out the integer as words. I am extremely new to java (started 6 weeks ago) so even this is way over my head, but here is what I have so far.

mport java.util.Scanner;

public class Validate {

	//Engine that runs main program.
  public static void main(String... Args){    
   	
	 Scanner input = new Scanner(System.in);
	 
	 //Prompts user to enter an integer between 1 and 9.
	 System.out.println("Enter an integer between 1 and 9! " + " Enter 0 to exit application. ");
	 System.out.print(" What is you integer? ");
	 
	 int userNumber = input.nextInt();	 
	
	while (userNumber >= 0) {
	
         // User inputs 0 to break the loop
	if (userNumber == 0) {
	System.out.print( "Thank you for playing! " + "Good bye! ");	
    break;    }
	
	else if (userNumber > 0 && < 9)
	//then print out numbers as words using convertIntegerToWords(int numWord)	
         
               else if (userNumber < 0 || > 9)
               System.out.print( " The integer you entered is not between 1 and 9")  

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

Thanks for taking the time to look at this.

Recommended Answers

All 13 Replies

ur convertIntegerToWords(int number) function should return numWordString. (if number is 1 nuwWordString would be one. also for each of the case add break so that it would break as soon as numWordString as a value
eg:

case 1:
  numWordString = "one;
  break;
case 2:
  numWordString = "two";
  break;
//ans so on

make these changes and call to ur function after this line and pass it userNumber

else if (userNumber > 0 && userNumber <= 9) //also change this to else if userNumber > 0 && <= 9) to include 9
convertIntegerToWords(userNumber)//call to ur function like this. u do not need to include int. coz uve already defined the variable and ur passing the value assigned to that variable to ur function

also, to keep the program running until the user enters 0, inside the while loop, after the number word is printed out, repeat the line

userNumber = input.nextInt();

hope this helps.

Edit:
note that the else if conditions must be like this
else if (userNumber > 0 && userNumber <= 9)

I got a lilttle further

Import java.util.Scanner;

public class Validate {

	//Engine that runs main program.
        public static void main(String... Args){    
   	
	Scanner input = new Scanner(System.in);
	 
	//Prompts user to enter an integer between 1 and 9.
	System.out.println("Enter an integer between 1 and 9! " + " Enter 0 to exit application. ");
	System.out.print(" What is you integer? ");
	 
	int userNumber = input.nextInt();	 
	
	while (userNumber >= 0) {
	
              // User inputs 0 to break the loop
	      if (userNumber == 0)
	         System.out.print( "Thank you for playing! " + "Good bye! ");	
                 break;
	
	      else if (userNumber > 0 && userNumber< 10)
	         //then print out numbers as words using convertIntegerToWords(int numWord)	
                 (textnumber = convertIntegerToWords(userNumber))
                 System.out.print(" Your integer is " + textnumber)
             
              else if (userNumber < 0 || userNumber > 9)
                 System.out.print( " The integer you entered is not between 1 and 9")  
                 
              int userNumber = input.nextInt();
       }
}
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 numWordString;
}

If you already learned about Array you can substitute that long switch statement with String[] numbers = {"zero", "one", "two"}; and then just call System.out.println(numbers[YOUR_INPUTED_NUMBER]);

1. im assuming that ur main class is JUST like the way u posted. i.e

public static void main(String... Args)//if so this is incorrect. it must be

public static void main(String[] args) //an array of arguments u shud pass

2. this is ur code

if (userNumber == 0)
	         System.out.print( "Thank you for playing! " + "Good bye! ");	
                 break;
//change it to 
if (userNumber == 0){
	         System.out.print( "Thank you for playing! " + "Good bye! ");	
                 break;
}// u had this correct the first time

3. chk below

else if (userNumber > 0 && userNumber< 10){
	         //then print out numbers as words using convertIntegerToWords(int numWord)	
                 textnumber = convertIntegerToWords(userNumber);//notice the change. use ';' to mark the end
                 System.out.print(" Your integer is " + textnumber);// semi-colon
 }//end of else-if
              else if (userNumber < 0 || userNumber > 9){ //
                 System.out.print( " The integer you entered is not between 1 and 9"); //semi-colon
 } //try to use parenthesis {} to enclose if blocks and else if blocks. 
              userNumber = input.nextInt(); //userNumber without int

btw, peter gives good advice.

commented: This was well laid out for a beginner to understand. TY for your time. +1

one more thing i just rmbrd to check. its if uve define textnumber before using it. it seems not so u have to make the below changes.

//CHANGE
textnumber = convertIntegerToWords(userNumber);
//TO
String textnumber = convertIntegerToWords(userNumber);

So thank you for all the help. I really didn't see how I was missing brackets and semi-colons so that cleared up a lot of my errors. As well as when I used too many () at line 25. But I have one last pesky error, and I don't understand why.

C:\Users\javac Validate8.java
Validate8.java:50: variable numWordString might not have been initialized
return numWordString;
^
1 error

import java.util.Scanner;

public class Validate8 {

	//Engine that runs main program.
        public static void main(String[] args){    
   	
	Scanner input = new Scanner(System.in);
	 
	//Prompts user to enter an integer between 1 and 9.
	System.out.println("Enter an integer between 1 and 9! " + " Enter 0 to exit application. ");
	System.out.print(" What is you integer? ");
	 
	int userNumber = input.nextInt();	 
	
	while (userNumber >= 0) {
	
              // User inputs 0 to break the loop
	      if (userNumber == 0){
	         System.out.print( "Thank you for playing! " + "Good bye! ");	
                 break;
	}
	      else if (userNumber > 0 && userNumber < 10){
	         //then print out numbers as words using convertIntegerToWords(int numWord)	
                String textnumber = convertIntegerToWords(userNumber);
                 System.out.print(" Your integer is " + textnumber);
             }
              else if (userNumber < 0 || userNumber > 9){
                 System.out.print( " The integer you entered is not between 1 and 9");  
}
          
              int userNumber = input.nextInt();       
       }
}
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 numWordString;
}
}

Use default: numWordString =""; Also you are missing break points after cases break; If not sure what I mean by that have look on Oracle Switch Statement tutorial

Thanks peter_budo I will take a look at that tonight when I can get to a computer with JDK. My professor also suggested the same thing about the default, as well as or an else without an if for “all others” as a just in case.

You could also initialize the variable to your default before the switch.

String numWordString="";

So it works! This is awesome! Now I'm wondering how to get this code to continue asking for integers until the integer 0 is input. As it runs now the program exits when you type in 0 or anything 1-9. If I want it to keep asking for integers after I plug in an integer 1-9

import java.util.Scanner;

public class Validate10 {
public static void main(String[] args){    
   	
Scanner input = new Scanner(System.in);
	 

System.out.println("Enter an integer between 1 and 9! " + " 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. " + "Good bye. ");	
break;
}
else if (userNumber > 0 && userNumber < 10){
String textnumber = convertIntegerToWords(userNumber);
System.out.print(" Your integer is " + textnumber); break;
}
else if (userNumber < 0 || userNumber > 9){
System.out.print( " The integer you entered is not between 1 and 9");  
}
}
}
public static String convertIntegerToWords(int numWord) {
	
	String numWordString;
	
	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: numWordString = ""; break;
}
return numWordString;
}
}

Look at your while clause:

while (userNumber >= 0)

Under what conditions will that continue to loop?

So why doesn't it loop when you want it to? Look at the break at line 22 - what does that mean?

There are two idiomatic ways to do a while loop. One is to state the conditions under which to keep going around - ie, while (profit > 0) {} . The other is to put in a permanent loop - ie, while (true) {} - and then put break statements to jump out of the loop when conditions are met.
You're sort of doing both here.

Also, in your if ...else section, the final clause doesn't need to be stated. If it's not zero, and it's not (between zero and ten) then it's necessarily less than zero or greater than nine - so your last else can be just an else, without the if (userNumber < 0 || userNumber > 9)

How does this look? I mean it works, but is this how you would do it?

import java.util.Scanner;
public class Validate11 { 
public static void main(String[] args){

Scanner input = new Scanner(System.in); 

System.out.println("Enter an integer between 1 and 9! " + " Enter 0 to exit application. ");

boolean continueLoop = true;

while (continueLoop) { 

System.out.println(" What is you integer? "); 

int userNumber = input.nextInt(); 

if (userNumber == 0) { 
System.out.println("Thank you. " + "Good bye. "); 
continueLoop = false; 
} 
else if (userNumber > 0 && userNumber < 10) { 
String textnumber = convertIntegerToWords(userNumber); 
System.out.println(" Your integer is " + textnumber); 
} 
else { System.out.println(" The integer you entered is not between 1 and 9"); 
} 
}
}
public static String convertIntegerToWords(int numWord) {

	String numWordString;
	
	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: numWordString = ""; break; 
} 
return numWordString; 
}
}
while (continueLoop) { 

  System.out.println(" What is you integer? "); 

  int userNumber = input.nextInt(); 

  if (userNumber == 0) { 
    System.out.println("Thank you. " + "Good bye. "); 
    continueLoop = false; 
  }
  ...
}

That looks about right to me.

else 
  {  
    System.out.println(" The integer you entered is not between 1 and 9"); 
  }

That too. Good work.

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.