---
title: "Trying to use a while loop with input."  
description: "Trying to use a while loop with input."  
author: "Samuel Fernandes"  
published: 2015-04-28  
updated: 2015-04-28  
canonical: https://www.mindstick.com/forum/23151/trying-to-use-a-while-loop-with-input  
category: "java"  
tags: ["java", "loop"]  
reading_time: 2 minutes  

---

# Trying to use a while loop with input.

Here is my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) so far. I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to get the [WHILE LOOP](https://www.mindstick.com/forum/34579/while-loop-in-python) to work so the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) inputs a number, the [if statement](https://www.mindstick.com/forum/12682/jquery-toggle-if-statement) prints the [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) and then it returns to ask for another number and goes again and again looping : \

```
import java.util.Scanner;public class ifwhileloop {         static Scanner sc = new Scanner(System.in);    public static void main(String[] args)    {    double nmbr;         *********** while(nmbr < 101){     ---- this gives me the error!!!             System.out.print("Enter the number:");               nmbr = sc.nextDouble();                                                      if(nmbr <= 5){    System.out.println("the number is between 0 and 5 and is: " + nmbr);                    }        else if(nmbr >= 6 && nmbr <=9) {            System.out.println("the number is between 6 and 9 and is: " + nmbr);        }        else if(nmbr >= 10 && nmbr <= 100 ) {            System.out.println("the number is between 10 and 100 and is: " + nmbr);             }        else            System.out.println("the number is bigger than 100:" + nmbr);//nmbr ++;  }}}
```

\
\

## Replies

### Reply by Anonymous User

Java requires that local variables be initialized before it can be used. At this point, the **nmbr** variable has not been initialized (set) \
that just declares the variable, but it does not initialize it to any value yet. You have to assign a value to it, for example:

```
double nmbr = 0.0;
```

Note that there is a difference between local variables (inside a method) and member variables (at class level). Local variables must be initialized explicitly, but member variables are automatically initialized with a default value when not initialized explicitly.\
\


---

Original Source: https://www.mindstick.com/forum/23151/trying-to-use-a-while-loop-with-input

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
