0

firstly i created a boolean then set it to true so as to enable the first run... then subsequent runs would be based on the user input.

    boolean emptyLine = true;
          String name; int quantity; double price; 

Scanner scan = new Scanner(System.in);

      while(emptyLine != false) //repeatedly request user input if input is not empty
      { 
         //ask the user for the quantity of items being purchased.
         System.out.println("Enter name of item: Enter quantity of item purchased: Enter price of item being purchased: ");
         input = scan.nextLine();  //should read the line of input

         item =                         //name of item from the line of input.
         quantity = scan.nextInt();     //should get the quantity from the line of input
         price = scan.nextDouble();     //get the price from the line of input

         if(input.length() >= 0)
         {
            emptyLine = true;
         }
         else
         {
            emptyLine = false;
         }

      }

I am trying to write a code to check if user input is empty or not. such that if user input is empty, the user is requested to enter a valid input or exit.

for example, if the user enters a space, I want my program to accept it, but if the user presses the ENTER button then it should be rejected.

Atinuke
  • 161
  • 1
  • 3
  • 10
  • `item = quantity = scan.nextInt()` is it on purpose ? PS : `input.length() >= 0` give you the boolean you want, no need of an if-else – AxelH Nov 30 '16 at 09:20
  • Since you're reading the entire line into `input` you should parse that string, e.g. by using a tokenizer, regular expression etc. – Thomas Nov 30 '16 at 09:20
  • You should really look at `Scanner#hasNextLine()`. Also, you will probably end up with [this](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) issue – TheLostMind Nov 30 '16 at 09:20
  • Are you sure that you shouldn't be checking for input present for _each_ item which you read? – Tim Biegeleisen Nov 30 '16 at 09:21
  • @TimBiegeleisen if i am checking for each item input then my program would be repetitive.... but i realized i could ask for all input using a line then extract my data from the line of input. I just do not know how to code it. – Atinuke Nov 30 '16 at 09:26
  • One option would be to use `Scanner.nextLine()` for _all_ input, and then check to make sure that something is there, it parses correctly (e.g. in the case of needing a valid number), etc. – Tim Biegeleisen Nov 30 '16 at 09:29
  • @TimBiegeleiseni should be checking for each input. you are correct, but how to i code for each within the while loop?? – Atinuke Nov 30 '16 at 09:46
  • i still need help please – Atinuke Nov 30 '16 at 15:36

1 Answers1

0

One easy way:

while (scann.hasNextLine()) {
  String userInput = scan.nextLine();

Now userInput is a String; and you could manually check its content; for example by calling isEmpty(); and if it is not empty, you can try to convert to the next expected type.

TheLostMind
  • 34,842
  • 11
  • 64
  • 97
GhostCat
  • 127,190
  • 21
  • 146
  • 218