-4

How do I make a java method that accepts only positive integer and if user enters negative integer then it re-prompts to enter from main method or go back to main method to input... This was all that I was able to figure out:

  public class error5
{
public void error5(int quantity){
   if (quantity <=0){
       System.out.println("**Error5**- Quantity on hand is negative value");
       System.out.print("Enter quantity on hand: ");
       break;
    }
   else {
       System.out.print("");
       break;
    }
 }
}   

What to do next?

Daksh Shah
  • 2,997
  • 6
  • 37
  • 71

2 Answers2

1

You will just need to convert the if to while

while(quantity <=0){
       System.out.println("**Error5**- Quantity on hand is negative value");
       System.out.print("Enter quantity on hand: ");
       //and accept the new value here
    }

Update You will need to do something like this. Pseudo code:

while(1){
Take the user input quantity
if quantity>0 continue;
else
print "please input a positive value"
}

Update 2 If you wanna make it shorter then do:

int quantity=-2
while(quantity<0){
take user input
if(quantity<0)  print"enter greater value"}
Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
  • 2
    your answer is fine, +1 if it can compensate the anonymous downvoter's activity – AlexR May 01 '14 at 09:54
  • yeah i got the method now how can make user to re input positive integer – user3591952 May 01 '14 at 09:58
  • By simple replacement of while to if is not good idea. Plz checkout my answer.. below. – emphywork May 01 '14 at 10:06
  • yes but how can i reprompt the user to input positive value – user3591952 May 01 '14 at 10:09
  • plz check my answer to know...its posted now – emphywork May 01 '14 at 10:12
  • i know that code but can u please say me what to do next how can i reprompt for input – user3591952 May 01 '14 at 10:19
  • @user3591952 Kindly keep on deleting un-useful comments once their purpose is finished because it should be presentable and short, precise for some other person to read it later – Daksh Shah May 01 '14 at 10:22
  • can u please tell me how to reprompt for input and it is method not main – user3591952 May 01 '14 at 10:24
  • @user3591952 See http://stackoverflow.com/questions/5769024/in-java-how-can-i-take-a-variable-obtained-from-a-user-input-from-one-method-an once, in there it is shown how to take input from user, check it try to code it yourself if you get an error (search for answer on google but if you cannot find it) put the code here and then tell what is the error and then we can help you. – Daksh Shah May 01 '14 at 12:11
-1

By simple replacement of while to if is not good idea. what you can do is put if statement to check for negative value, and in else part you can do what you ought to do with positive value.

if(quantity <= 0){
System.out.println("**Error5**- Quantity on hand is negative value");
System.out.print("Enter quantity on hand: ");
}else{
System.out.print("");
//Do Something with Positive value
}

No need to have break statements because if..else statement will do the work by evaluating condition. If condition evaluates true than it will execute code in if block otherwise else block will be executed.

Update:

This will only be used for taking input once, and checking and re-prompting user. For looping you need to use do..while or while loop.

halfer
  • 19,824
  • 17
  • 99
  • 186
emphywork
  • 448
  • 1
  • 4
  • 15
  • 1
    And if the user inputs a negative value twice? or maybe more times then? Therefore it is better to put it in a loop, see my update – Daksh Shah May 01 '14 at 10:12
  • @DakshShah Ok I see the part where Question is about Loop...!!!! your answer justifies the question – emphywork May 01 '14 at 10:19
  • i know that code but can u please say me what to do next how can i reprompt for input – user3591952 May 01 '14 at 10:19
  • but according to java script i cannot use prompt for user input in method – user3591952 May 01 '14 at 10:28
  • @user3591952 you are mixing Java with Java Script..!!!! If you are using Java as shown in mine and Daksh's answer... you are re-prompting user for input when quantity is 0 or negative – emphywork May 01 '14 at 10:30
  • i compiled and its just printing is not prompting for userinput – user3591952 May 01 '14 at 10:34
  • @user3591952 you can use one of the way like Scanner or inputstream to read user output.... Because System.out.println is used for only printing out to user not for getting input from user. You can google around for it. It is pretty easy to find and understand – emphywork May 01 '14 at 10:38
  • can i use scanner input in java methods – user3591952 May 01 '14 at 10:40
  • For Getting input from User Read http://stackoverflow.com/questions/5287538/how-to-get-basic-user-input-for-java – emphywork May 01 '14 at 10:40
  • 1
    Hi @JavaRocks. I've edited your post to improve case issues, and to add some `inline formatting`. We're not too keen on all-caps sections here, so if you could take care with your answers, it will save editors future work. Thanks! – halfer May 01 '14 at 17:15