part of bigger code but i cant understand why its not working in the correct range

Scanner input3 = new Scanner (System.in);
                        System.out.println("Enter House Number;");
                        houseNumber = input3.nextInt();
                        do {
                            System.out.println("You must enter a house number between 1                            and 1000");
                            System.out.println("Enter House Number;");
                            houseNumber = input3.nextInt();
                        }while ((houseNumber <1) || (houseNumber >1000));

it says i.e. 999 is wrong basically were it should accept and move onto the next stage but its stuck in the loop as it think 999 is wrong were it should be anything below 1 and above 1000 that is invalid and only looped.

Dukane commented: use code tags next time -1

Recommended Answers

All 4 Replies

got it sorted, needed an if statment inside the do while that was the same as the while true to keep the loop going only for false invalid data which it exits correctly if data is in the correct range.

I copy-pasted your code and it works fine for me within the desired ranges. However you do realize that you have to enter a house number twice even if its right the first time?
Why not use a regular while loop?

I copy-pasted your code and it works fine for me within the desired ranges. However you do realize that you have to enter a house number twice even if its right the first time?
Why not use a regular while loop?

the code was incorrect, house number should only loop if outside a valid range, therefore it keeps looping asking for house number if its not between 1-1000. i fixed it and its 100% working as i needed an if statement inside this do while, the do while worked if i placed only 1 piece of data range in but did not work for an or with two different bits of data and anything between 1 and 1000 came back as invalid then it did this twice and then went to next stage in the code, cheers a million anyway having the time to look at the bit of code.
one question, how do you validate spaces as i have been trying with "" only the longer the gap then the more spaces it allows to validate as im lookin for infinity amount of spaces?

The code is working fine as everyone said. The problem is with the message you have given within the do while loop. When you enter 999 the loop is working fine and is giving you the desired output which is "You must enter a house number between 1 and 1000". This is the string which is given by you. When you enter the value less than 1 and greater than or equal to 1000 then the control will come out of the loop.

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.