# input only a digit with 4 numbers or less

Page 1 of 1

## 6 Replies - 6081 Views - Last Post: 28 February 2012 - 04:56 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=268563&amp;s=c19337bac84eda636d1ff8f9b15138dd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 crownRoyal89

Reputation: 5
• Posts: 47
• Joined: 10-May 11

# input only a digit with 4 numbers or less

Posted 27 February 2012 - 03:36 PM

I'm trying to get a the user to input a number with 4 digits or less. How can I get this to work?

Heres what I have so far..

```import java.util.Scanner;

public class Test {

public static void main(String [] args){

int number;

Scanner input1;
input1 = new Scanner(System.in);

System.out.println("Please enter a number with atleast four digits...");
number = input1.nextInt();

}

}

```

Do I have to use the toString method?

Is This A Good Question/Topic? 0

## Replies To: input only a digit with 4 numbers or less

### #2 supercorey

Reputation: 119
• Posts: 207
• Joined: 15-February 09

## Re: input only a digit with 4 numbers or less

Posted 27 February 2012 - 03:48 PM

Firstly, I think the text at line 12 should be "Please enter a number with at most four digits...".

Onto your question, you could just check to see if number is less-than (<) 10,000, or less-than-or- equal (<=) to 9,999, considering how that would limit the range to four digits. You might also want to add a check for greater-than-or-equal-to 0.

Cheers!

### #3 g00se

• D.I.C Lover

Reputation: 3225
• Posts: 14,234
• Joined: 20-September 08

## Re: input only a digit with 4 numbers or less

Posted 27 February 2012 - 03:54 PM

```        Scanner in = new Scanner(System.in);
int i = 0;

while (i < 1000) {
System.out.print("Enter a number of at least 4 digits: ");

if (in.hasNextInt()) {
i = in.nextInt();
}
}

System.out.printf("You entered %d\n", i);

```

should do it

### #4 crownRoyal89

Reputation: 5
• Posts: 47
• Joined: 10-May 11

## Re: input only a digit with 4 numbers or less

Posted 27 February 2012 - 04:06 PM

Ok I think I got it.. So what Im trying to do is to make a calculator that takes the users input and outputs the single digit of the 100th digit. lets say the user input 1234 the program would output 2..

so for this to work the user has to input a number =>100 and =<9999.... if my logic is correct.

```import java.util.Scanner;

public class Test {

public static void main(String [] args){

int number;

Scanner input1;
input1 = new Scanner(System.in);

System.out.println("Please enter a number with at most four digits...");
number = input1.nextInt();

if (number <= 9999 & number >= 100  )
{
System.out.println("Thank you");
}
else
{
}

}

}

```

So as you can see I got the limitations down but I need to implement this into a loop. Any advice?

### #5 supercorey

Reputation: 119
• Posts: 207
• Joined: 15-February 09

## Re: input only a digit with 4 numbers or less

Posted 27 February 2012 - 04:09 PM

If you want advice about putting it into a loop, read g00se's post right above yours. You would just have to implement your if statement as the conditional in the loop.

### #6 g00se

• D.I.C Lover

Reputation: 3225
• Posts: 14,234
• Joined: 20-September 08

## Re: input only a digit with 4 numbers or less

Posted 27 February 2012 - 04:10 PM

Just use my code with your new conditions

### #7 crownRoyal89

Reputation: 5
• Posts: 47
• Joined: 10-May 11

## Re: input only a digit with 4 numbers or less

Posted 28 February 2012 - 04:56 AM

Thanks for your help. Much appreciated.