5 Replies - 8798 Views - Last Post: 27 March 2011 - 06:04 PM Rate Topic: -----

#1 GSIT88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 11

A loop to allow only positive numbers above 0 as input.

Posted 27 March 2011 - 05:40 PM

I am trying to write a loop that will first ask a user to input a number, then make sure the number is above zero and not negative. If it is zero or negative I want to display the message "Please enter a positive number above 0". And then ask the user to input another number.
Is This A Good Question/Topic? 0
  • +

Replies To: A loop to allow only positive numbers above 0 as input.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: A loop to allow only positive numbers above 0 as input.

Posted 27 March 2011 - 05:44 PM

int value = -1;
Scanner scanner = new Scanner(System.in);
while(value < 0) {
  System.out.print("Enter a value > 0: ");
  value = scanner.nextInt();
  if(value < 0)
     System.out.println("sorry write a value > 0")'
}


Was This Post Helpful? 1
  • +
  • -

#3 Manbearpig101  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 62
  • Joined: 17-June 10

Re: A loop to allow only positive numbers above 0 as input.

Posted 27 March 2011 - 05:44 PM

Something like this:

The whole concept basically is: read in a number, if it meets conditions, stop, if not, re-do until the conditions get met.

Scanner sc = new Scanner(System.in);
int num = 0;
while (num <= 0) {
    System.out.print("Please enter a number: ");
    num = sc.nextInt();
    if (num <= 0) {
        System.out.println("Please enter a number above 0!");
        continue;
    }
}


Was This Post Helpful? 1
  • +
  • -

#4 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: A loop to allow only positive numbers above 0 as input.

Posted 27 March 2011 - 05:46 PM

View Postpbl, on 27 March 2011 - 05:44 PM, said:

int value = -1;
Scanner scanner = new Scanner(System.in);
while(value < 0) {
  System.out.print("Enter a value > 0: ");
  value = scanner.nextInt();
  if(value < 0)
     System.out.println("sorry write a value > 0")'
}


pbl, value <= 0 or else you're allowing 0 as an input :)

This post has been edited by TFoSSDQ: 27 March 2011 - 05:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: A loop to allow only positive numbers above 0 as input.

Posted 27 March 2011 - 05:48 PM

Select < or <= the choice is yours
The concept remains the same
Was This Post Helpful? 0
  • +
  • -

#6 GSIT88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-March 11

Re: A loop to allow only positive numbers above 0 as input.

Posted 27 March 2011 - 06:04 PM

View PostManbearpig101, on 27 March 2011 - 05:44 PM, said:

Something like this:

The whole concept basically is: read in a number, if it meets conditions, stop, if not, re-do until the conditions get met.

Scanner sc = new Scanner(System.in);
int num = 0;
while (num <= 0) {
    System.out.print("Please enter a number: ");
    num = sc.nextInt();
    if (num <= 0) {
        System.out.println("Please enter a number above 0!");
        continue;
    }
}


Thanks for your help! I had basically had this but I forgot to declare an initial value for num so I could not figure out why it wasn't working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1