2
    //Input Number
    System.out.print("Enter a number from 1-10: ");
    words = input.next();

    //Processing String
    if(!words.equals(FLAG)){
        while(!words.equals("1") && !words.equals("2") && !words.equals("3") &&
            !words.equals("4") && !words.equals("5") && !words.equals("6") &&
            !words.equals("7") && !words.equals("8") && !words.equals("9") &&
            !words.equals("10")){
            System.out.print("Please enter a number in integer form from 1-10: ");
            words = input.next(); 
        }
    }
    //Close Scanner
    input.close();

    //String to Integer
    num = Integer.parseInt(words);

    //Output Number
    if(num >=1 || num <=10){
        System.out.println("\nYour number is: " + num);
    }

How could i change the while loop? What if the number range was from 1-100? IS there any way to shorten the processing string segment? The program needs to be able to handle string and integers.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Please explain exactly what your loop is suppose to do. I think I have an idea what it should do, but I want you to explain it; not only for clarity but because it will also help you understand what you are trying to do. – apxcode Apr 04 '14 at 02:55
  • parse it first, if it doesn't parse its not a number. If it does parse check between minimum and maximums - done – OJay Apr 04 '14 at 02:58
  • This will ask the user for a number between 1-10. If the user inputs a String, then the program will loop until a Integer is entered. Right now it is being inputed as string and is being converted to an integer. It will then display the number that the user entered. –  Apr 04 '14 at 02:59

6 Answers6

6

Parse it to an Integer

int number = Integer.parseInt(words);   

and loop :

while( !(number > 0 && number <= 10) ){ // loop the loop }    

N.B: Because you will be using parseInt(), you will need to learn to use try-catch blocks. Do look it up.

An SO User
  • 24,612
  • 35
  • 133
  • 221
2

You can also directly use nextInt() method of the Scanner class for an input of Integer data type.

So you can do,

num = input.nextInt();

and then directly,

//Output Number
if(num >=1 || num <=10){
    System.out.println("\nYour number is: " + num);
}
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • You can also validate this to ensure your correct input, see here http://www.tutorialspoint.com/java/util/scanner_nextint.htm – Atul O Holic Apr 04 '14 at 03:02
1

Make a min int and a max int.

int min =1;
int max =10; // change to 100 for if you wanted it bigger

Then when you get their value simply parse it(Parse turns it into an Interger)

int value= Integer.parseInt(words); 

The final while loop would look like this:

while( !(number > min && number <= max) ){
   //Your code
}
Lain
  • 2,166
  • 4
  • 23
  • 47
0

The easiest way to 'shorten' it would be to parse it before the while loop. You should be throwing NumberFormatException and putting your code in a try/catch block if you're going to be parsing strings to ints.

public void f() throws NumberFormatException{
    try{
        // other code

        num = Integer.parseInt(words);

        while( !(num>0 && num<=10) ){
            // magic
        }

        // other code
    }catch(NumberFormatException e){
        // handle it!
    }
}
domdomcodecode
  • 2,355
  • 4
  • 19
  • 27
0

Use a do while loop so that you don't have to ask the user for input twice, just let the loop do the work for you.

int number;
final int MIN = 0;
final int MAX = 100;

do
{
    System.out.print("Please enter a number in integer form from 1-10: ");
    words = input.next(); 

    number = Integer.parseInt(words);

}while(!(number > MIN && number <= MAX));
apxcode
  • 7,696
  • 7
  • 30
  • 41
0

Parse the input into an Integer before check validation.

But do remember to handle exception properly.

int number;

while(true){
    try{
        number = Integer.parseInt(words);
        if(number <= 10 && number >= 1) break;
        else
            System.out.print("Your number is out of range (1, 10):");
    }catch(NumberFormatException e){
        System.out.print("Please input valid number from 1 to 10:");
    }

    words = input.next();
}

System.out.println("Your number is " + number);
Linyes
  • 63
  • 8