5 Replies - 385 Views - Last Post: 09 October 2012 - 10:30 PM Rate Topic: -----

#1 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 154
  • Joined: 08-September 11

3 digit numner?

Posted 09 October 2012 - 07:06 PM

Hey guys,

I'm writing a program that does a lot of things, but one of them is to check if the user input a 3 digit number then do stuff, but if a user input 2 or 4+ digit numbers then the program don't do any thing.

I wrote this chunk of code
do{
			System.out.println("Enter a 3-digit number: ");
			int input = scan.nextInt();
		}while (input >= 100 || input < 1000);

but it doesn't check if the input is 3 digits.

So some help please.

This post has been edited by amture106: 09 October 2012 - 07:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: 3 digit numner?

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: 3 digit numner?

Posted 09 October 2012 - 07:26 PM

Well first this will not even compile. How can you check a condition on a variable that will only be created if the condition is true. You need to make input outside of the do/while loop. You do not have to instantiate it outside of the loop, just make the variable outside the loop. Second of all, you're testing if input IS a 3 digit number, and as long as it is keep getting input.
Was This Post Helpful? 0
  • +
  • -

#3 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 154
  • Joined: 08-September 11

Re: 3 digit numner?

Posted 09 October 2012 - 07:48 PM

sorry my bad I meant
int input;
do{
			System.out.println("Enter a 3-digit number: ");
		 input = scan.nextInt();
		}while (input >= 100 || input < 1000);


now it will compile and still won't work
Was This Post Helpful? 0
  • +
  • -

#4 Zoquo  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 45
  • Joined: 16-May 12

Re: 3 digit numner?

Posted 09 October 2012 - 08:10 PM

If you are trying to check to see if it is a three-digit number, the condition would be AND not OR.

But if you are checking to see if it is NOT, then just change the OR to an AND and throw a NOT before it.

This post has been edited by Zoquo: 09 October 2012 - 08:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: 3 digit numner?

Posted 09 October 2012 - 08:13 PM

import java.util.Scanner;

public class Digit {
	public static void main(String...h){
	int input=333;
	Scanner scan=new Scanner(System.in);
	boolean b=true;
	while ( b ){
		
	System.out.println("Enter a 3-digit number: ");
	input = scan.nextInt();
		b =!(input >= 100 && input < 1000);
	}
//execution block goes here !
									}
}


This post has been edited by burakaltr: 09 October 2012 - 08:23 PM

Was This Post Helpful? 1
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: 3 digit numner?

Posted 09 October 2012 - 10:30 PM

I like that both of you suggested putting a not around it

Why not just
do {
    //Ask for input
}
while (input < 100 || input > 1000);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1