6 Replies - 3181 Views - Last Post: 28 February 2012 - 04:56 AM Rate Topic: -----

#1 crownRoyal89  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • 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!
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,992
  • 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
Was This Post Helpful? 1
  • +
  • -

#4 crownRoyal89  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • 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
		{
			System.out.println("Please enter another number");
		}
		
	}

}



So as you can see I got the limitations down but I need to implement this into a loop. Any advice?
Was This Post Helpful? 0
  • +
  • -

#5 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,992
  • 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
Was This Post Helpful? 1
  • +
  • -

#7 crownRoyal89  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1