31
\$\begingroup\$

At runtime, keep prompting for a line of input until the user inputs something (other than an empty newline), i.e. does not just press Enter or OK. Output or result is neither required nor prohibited.

Pseudo-code 1

myform = new form("GUI")
myform.mytxt = new editfield("")
myform.ok = new button("OK")
repeat
  waitfor(myform.ok,"click")
until myform.mytxt.content <> ""

Pseudo-code 2

LET TEXT = ""
WHILE TEXT = "" DO
  TEXT = PROMPT("")
ENDWHILE

Example 1

Program runs and immediately pops up a form with a single text field and an OK button.
User clicks the OK button.
Nothing happens.
User pastes "hello world" into the text field and clicks the OK button.
Program terminates.

Example 2

Function is called and immediately displays a blank line and a blinking cursor.
User presses Enter.
Cursor moves down one line.
User presses Enter.
Cursor moves down one line.
User presses PPCGEnter
Function returns.

\$\endgroup\$
  • \$\begingroup\$ is it allowed to count space-only lines as empty? \$\endgroup\$ – 12431234123412341234123 May 4 '17 at 9:25
  • \$\begingroup\$ @12431234123412341234123 No. \$\endgroup\$ – Adám May 4 '17 at 9:29
  • \$\begingroup\$ Is it necessary to wait for the full line once the user starts to input data? \$\endgroup\$ – Matteo Italia May 7 '17 at 13:22
  • \$\begingroup\$ Ok, from a comment in @CodyGray's deleted answer it seems like it's not allowed. \$\endgroup\$ – Matteo Italia May 7 '17 at 14:54
  • \$\begingroup\$ @MatteoItalia Yes, correct; a line of input. \$\endgroup\$ – Adám May 7 '17 at 15:17

61 Answers 61

16
\$\begingroup\$

TI-BASIC, 2 bytes

:Prompt X

TI-BASIC does this automatically. Once input is given, it will quit.

Here is a GIF:

enter image description here

Watch the count on the enter key in the Key Press History. Created with TI-SmartView CE and ezgif.com.

\$\endgroup\$
  • 1
    \$\begingroup\$ Hmm.. this is 2 bytes? \$\endgroup\$ – Geeky I May 4 '17 at 16:06
  • 9
    \$\begingroup\$ TI-BASIC is token based. \$\endgroup\$ – Scott Milner May 4 '17 at 16:11
  • \$\begingroup\$ And it will not take Enter for an answer? Is there anywhere to test this online? \$\endgroup\$ – Adám May 5 '17 at 5:47
  • \$\begingroup\$ @Adám It won't take enter for an answer. As far as I am aware, there isn't any place you can go to online without already having a TI ROM image. I can post a .gif when I get home, though. \$\endgroup\$ – Scott Milner May 5 '17 at 15:16
  • 1
    \$\begingroup\$ @Adám It's illegal to test TI-BASIC online without owning a ROM yourself. \$\endgroup\$ – Erik the Outgolfer May 7 '17 at 14:12
15
\$\begingroup\$

Python 3, 18 bytes

while''==input():0

Try it online!

\$\endgroup\$
  • 19
    \$\begingroup\$ So, you'll keep adding another language every 9 minutes? \$\endgroup\$ – Adám May 4 '17 at 4:58
15
\$\begingroup\$

sed, 4

/./q

Waits for a line of input that has 1 or more characters, then quits.

Try it online. But it works better on a live shell:

sed '/./q'
\$\endgroup\$
15
\$\begingroup\$

JavaScript, 37 22 17 bytes

while(!prompt());

while(!prompt());

Explanation

The while keyword starts the while loop. In the condition of the loop, !prompt() asks for input and checks whether it is given or not. If it isn't given, the body of the loop is executed, which in our case is empty, then the interpreter goes back to the loop condition. The same process happens over and over again until the user gives the input.

\$\endgroup\$
  • \$\begingroup\$ while(""==prompt("")); \$\endgroup\$ – Adám May 4 '17 at 5:01
  • \$\begingroup\$ @Adám Wow. I was just thinking about how to make the solution smaller, and you came up with an answer thanks! \$\endgroup\$ – Arjun May 4 '17 at 5:12
  • \$\begingroup\$ It seemed obvious. However, my browser console refuses to execute without a trailing ;, while your initial expression worked fine without ;. Any idea why? \$\endgroup\$ – Adám May 4 '17 at 5:13
  • \$\begingroup\$ No, Chrome. So if any JavaScript engine can handle omission of the current expression's ;, you can save that byte :-) \$\endgroup\$ – Adám May 4 '17 at 5:18
  • 1
    \$\begingroup\$ @Adám Just figured out the real cause of the error produced after the removal of semicolon from the source code. The reason why that happens is that while loop expects a statement (or a group of statements) as the loop body after the loop condition. Adding the semicolon makes it think that the loop body is over. However, removing the semicolon causes it to expect the next statement as the loop body. But in our case, no new statements comes after the loop condition. So, it throws an error. I hope it is clear now. I am terrible at explaining stuff!! :p \$\endgroup\$ – Arjun May 4 '17 at 6:00
9
\$\begingroup\$

Java, 55 bytes

void f()throws Exception{while(System.in.read()==10);}}

If I remember correctly (it's been a while since I've been active on PPCG), my program can just be a function.

This is system specific; it only works on systems where the end-of-line character is a single newline. If the end-of-line character is instead a carriage return, replace the 10 with 13. On Windows, this doesn't work, as end-of-line on windows is \r\n.

This makes use of the fact that I can read directly from System.in.

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome back, if only for this answer! :D \$\endgroup\$ – Conor O'Brien May 4 '17 at 11:17
  • 3
    \$\begingroup\$ Welcome back @Justin! \$\endgroup\$ – Digital Trauma May 4 '17 at 14:45
  • 1
    \$\begingroup\$ Not planning on being active; just dropping by for a question here and there \$\endgroup\$ – Justin May 4 '17 at 14:48
  • \$\begingroup\$ To avoid the "doesn't work on Windows" problem, can't you just test the first character and see if it is \r, as opposed to testing the entire string, and then running into problems with there being 2 EOL characters? \$\endgroup\$ – Cody Gray May 5 '17 at 12:58
  • \$\begingroup\$ @CodyGray No. I'm reading 1 char at a time; that's all that System.in let's you do directly (well, you chan read an array of chars at a time). My loop body (empty) is run for every char in the input \$\endgroup\$ – Justin May 5 '17 at 15:03
9
\$\begingroup\$

HTML5, 33 22 bytes

<form><input required>

<form><input required>

Explanation

The required attribute on the <input> causes the browser to inform the user "this field is required"-sort-of-message, if they do not enter a value. However, when they enter a value, the value is sent to the URL of the action attribute of the <form> (which in our case is the current file itself, since we haven't specified any value explicitly).

This was tested on the latest version of Google Chrome (version 55.0). May work in other browsers and versions.

\$\endgroup\$
  • \$\begingroup\$ action=/ might work on some browsers. \$\endgroup\$ – Neil May 4 '17 at 8:05
  • 1
    \$\begingroup\$ You could probably ditch the action attribute altogether. \$\endgroup\$ – Shaggy May 4 '17 at 10:56
  • 1
    \$\begingroup\$ @Arjun The action=y.p isn't necessary, as most User Agents will submit to the same location if no action is specificed \$\endgroup\$ – MayorMonty May 4 '17 at 13:18
  • \$\begingroup\$ @Arjun I've tried <form><input required> on the latest Google Chrome version and works as intended. And if you're skeptical, just use action=#. That saves 2 bytes. \$\endgroup\$ – Ismael Miguel May 5 '17 at 4:04
  • 3
    \$\begingroup\$ @Arjun That is a terrible idea for code golfing. I can't say about every browser, since I don't own any Android 1.0, 1.6, 2.0, 2.1, 3.0, 3.1, 4.x.x, 5.x, 6.x, ..., iPhone 2 - 7, every single release of Google Chrome, Firefox 3.0+ and on and on and on and on... In fact, not all HTML5 browsers implement the required attribute! Also, # is a path available since HTML 1.0, if I'm correct. And adding no action to your form is the same as action=".", which is the file itself. \$\endgroup\$ – Ismael Miguel May 5 '17 at 7:38
8
\$\begingroup\$

Jelly, 3 bytes

ɠṆ¿

Not much to look at on TIO, I'm afraid.

Try it online!

How it works

This is a niladic program, meaning that it takes no input arguments. The implicit argument and return value are both 0 in this case.

¿ (while) is a quick that pops two links from the link stack: a condition () and a body.

is a monadic atom: flat logical NOT. If the previous return value is falsy (here, 0 or an empty string), it returns 1 and the body is called.

ɠ is a niladic atom; it reads a raw line from STDIN and returns the result.

Once ɠ reads a non-empty line, returns 0 and we break out of the loop.

\$\endgroup\$
  • \$\begingroup\$ That fast, eh? Ah, well. \$\endgroup\$ – Adám May 4 '17 at 4:50
7
\$\begingroup\$

Pyth, 3 bytes

W!w

Try it online!

Translated to Python:

while Pnot(input()):
 pass
\$\endgroup\$
6
\$\begingroup\$

brainfuck, 26 bytes

+[>,----------[[-]<[-]>]<]

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ That TIO link is all nice and well, but it is a bit hard to tell what happens when run… ;-) \$\endgroup\$ – Adám May 4 '17 at 5:59
  • 1
    \$\begingroup\$ @Adám you can append ,. to the end to verify. \$\endgroup\$ – Leaky Nun May 4 '17 at 6:00
5
\$\begingroup\$

Perl 5, 8+1 (-p or -n flag) bytes

/./&&die

Takes input from stdin and dies as soon as the regex matches anything except a newline.

\$\endgroup\$
5
\$\begingroup\$

C, 52 bytes, 33 bytes, 29 bytes

-19 bytes thanks to Justin

-4 bytes thanks to Christoph

main(){while(getchar()==10);}

10 is equal to '\n' - In case this isn't obvious.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can golf it by 1 char by using a ; instead of the {} for the while loop \$\endgroup\$ – Justin May 4 '17 at 7:07
  • 1
    \$\begingroup\$ Although not standards compliant, you can remove the #include<stdio.h> if you switch to while(getchar()==10): int main(){while(getchar()==10);}. \$\endgroup\$ – Justin May 4 '17 at 7:17
  • 2
    \$\begingroup\$ @Justin main(){while(getchar()==10);} is enough no need for default int. \$\endgroup\$ – Christoph May 4 '17 at 8:10
  • 1
    \$\begingroup\$ main(){scanf("%s");} would also work, if space-only lines can count as empty. \$\endgroup\$ – 12431234123412341234123 May 4 '17 at 9:33
  • 1
    \$\begingroup\$ @DanielEarwicker You are right. That didn't seem clear to me the first time I read the question, but now I see what you are saying. In that case, there are many non-conforming answers to this question. \$\endgroup\$ – Justin May 7 '17 at 7:54
5
\$\begingroup\$

Bash, 51 bytes (39 without "#!/bin/bash")

It's my first time participating in PPCG, so dont be to rude ;D

#!/bin/bash
read a
while [ -z "$a" ]
do
read a
done
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to PPCG. \$\endgroup\$ – Adám May 4 '17 at 8:22
  • 3
    \$\begingroup\$ Welcome to PPCG!, if you're planning on using Bash as a golfing language you might find This Post Interesting, it has a bunch of tips for making bash programs as small as humanly possible, there's also a More General Version you might want to skim through too. \$\endgroup\$ – colsw May 4 '17 at 8:25
  • \$\begingroup\$ @ConnorLSW Wow, thanks, that Post will help me a lot. I'll try to edit my Answer with this new tips ASAP. \$\endgroup\$ – Nicolas Fischer May 4 '17 at 9:14
  • 4
    \$\begingroup\$ you do not need #!/bin/bash \$\endgroup\$ – 12431234123412341234123 May 4 '17 at 9:35
  • \$\begingroup\$ Well, also the first time for me :) But you could perhaps use recursion: [ -z `line` ] && $0 or if the deprecated line is not on your system: [ -z `head -n1` ] && $0. That should either be 20 or 24 bytes. \$\endgroup\$ – Yeti May 4 '17 at 18:20
4
\$\begingroup\$

Java, 128 126 bytes

2 bytes thanks to Kevin Cruijssen.

import java.util.*;class a{public static void main(String[]a){for(Scanner s=new Scanner(System.in);s.nextLine().isEmpty(););}}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ You can golf it by two bytes: Change the while to a for and put the Scanner s=new Scanner(System.in); inside it. And change the .equals("") to .isEmpty(). \$\endgroup\$ – Kevin Cruijssen May 4 '17 at 7:01
  • \$\begingroup\$ I'm pretty sure you can remove the import if you inline the use of the scanner; pretty sure leaking a scanner works just fine: while(new java.util.Scanner(System.in).nextLine().isEmpty()); \$\endgroup\$ – Justin May 4 '17 at 14:52
4
\$\begingroup\$

C# (.NET Core), 66 bytes

class C{static void Main(){for(;System.Console.ReadLine()!="";);}}

Try it online!

-6 bytes thanks to raznagul.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can save 6 bytes by writing System.Console.ReadLIne directly and drop the using-Statement. \$\endgroup\$ – raznagul May 4 '17 at 6:39
  • \$\begingroup\$ A while loop would be the same number of bytes, but methinks a more idiomatic way of writing the code than a for loop. \$\endgroup\$ – Cody Gray May 5 '17 at 13:00
  • \$\begingroup\$ you can save 24 bytes by writing it as a lambda in the form () => {} \$\endgroup\$ – Ceshion May 8 '17 at 17:08
  • \$\begingroup\$ or alternatively, 7 by making it an instance method \$\endgroup\$ – Ceshion May 8 '17 at 17:09
4
\$\begingroup\$

QBasic 4.5, 15 bytes

INPUT a$:IF a$=""THEN RUN

Asks for input, then checks if any was given. If not, RUN restarts the program.

\$\endgroup\$
  • 2
    \$\begingroup\$ Didn't know about the RUN. +1. (That rhymes too; RUN and PLUS ONE :) ) \$\endgroup\$ – Arjun May 4 '17 at 16:30
4
\$\begingroup\$

Ruby, 13 bytes

gets until/./

I wish gets took a Regexp argument.

\$\endgroup\$
4
\$\begingroup\$

R, 27 24 23 22 bytes

while(!sum(scan()^0))t

Takes input from stdin, and repeat as long as input is of length 0. Cut off some bytes due to Jarko Dubbeldam and MickyT. Replaced the {} with t to save another byte.

\$\endgroup\$
  • \$\begingroup\$ I think you can omit the ,'', since neither input (string or numeric) nor way of terminating was specified in the challenge. \$\endgroup\$ – JAD May 4 '17 at 12:32
  • \$\begingroup\$ @JarkoDubbeldam: good catch! I didn't realize errors were a valid termination. \$\endgroup\$ – rturnbull May 4 '17 at 18:53
  • \$\begingroup\$ would while(!sum(scan()^0)){} work as well? \$\endgroup\$ – MickyT May 4 '17 at 19:42
  • \$\begingroup\$ @MickyT Seems to work. Apparently even Inf^0 is 1. Nice catch with the sum(). Too bad the ^0 is need to handle 0 as input. \$\endgroup\$ – JAD May 5 '17 at 7:50
  • \$\begingroup\$ @MickyT That's a great find with the use of sum, thanks! I've updated the answer. \$\endgroup\$ – rturnbull May 5 '17 at 10:55
3
\$\begingroup\$

PHP, 18 bytes

while(!readline())
\$\endgroup\$
  • \$\begingroup\$ Does it check that the line of text is not empty? \$\endgroup\$ – Daniel Earwicker May 7 '17 at 6:49
  • 1
    \$\begingroup\$ Yes, as soon as the line is not empty readline() returns something else than an empty string. Non empty strings evaluate to true in php - atleast most of them do. "0" seems to be an exception as I just read in the docs. Well I guess my answer is wrong now. \$\endgroup\$ – Skynet May 8 '17 at 13:53
3
\$\begingroup\$

AWK, 8 11 bytes

NF{exit}

Wait for input. If the number of fields in input is more than 0, exit.

EDIT:

I've just realized that this doesn't work for input containing whitespace characters only. IFS needs to be set to empty string using -F "" command line option. Therefore +3 bytes.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Actually, you could replace NF with 1. Then you don't need to set IFS. And grumble you beat me to and AWK solution. \$\endgroup\$ – Robert Benson May 4 '17 at 13:55
  • 1
    \$\begingroup\$ @RobertBenson I tried replacing NF with 1. In that case the program exits given any input, including empty newline. \$\endgroup\$ – Maxim Mikhaylov May 4 '17 at 14:09
  • \$\begingroup\$ I missed that part of the spec. \$\endgroup\$ – Robert Benson May 4 '17 at 14:22
3
\$\begingroup\$

SpecBAS - 34 bytes

1 DO : INPUT a$: LOOP UNTIL a$<>""

Just loops until non-empty string is entered.

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 26 20 bytes

For[,Input[]==Null,]
\$\endgroup\$
  • 1
    \$\begingroup\$ Shave off six bytes with For[,Input[]==Null,]. Works just as well. \$\endgroup\$ – Kelly Lowder May 4 '17 at 19:10
3
\$\begingroup\$

Haskell, 19 17 bytes

f=do""<-getLine;f

Defines a function f that if it reads the empty line calls itself again. When reading a non-empty line an exception is raised and the recursion is stopped.

Edit: @Laikoni replaced the parameter with a pattern match and saved 2 bytes. Thanks!

\$\endgroup\$
  • \$\begingroup\$ Didn't think Haskell IO code could be as short as this! \$\endgroup\$ – Geeky I May 4 '17 at 16:23
  • \$\begingroup\$ 17 bytes: f=do""<-getLine;f \$\endgroup\$ – Laikoni May 4 '17 at 22:15
  • \$\begingroup\$ @Laikoni: nice catch. Thanks! \$\endgroup\$ – nimi May 5 '17 at 12:51
3
\$\begingroup\$

Aceto, 9 5 4 bytes

read a value, negate it (!; implicitly casting to a boolean), and mirror horizontally if the value is truthy (|).

!
r|
\$\endgroup\$
  • \$\begingroup\$ Wait, Aceto runs **up**‽ And they say APL is strange for running from right to left… \$\endgroup\$ – Adám May 7 '17 at 15:43
  • 3
    \$\begingroup\$ @Adám Aceto runs along a 2D Hilbert curve. \$\endgroup\$ – L3viathan May 7 '17 at 15:44
3
\$\begingroup\$

Java, 66 64 60 bytes

int a(){return!System.console().readLine().isEmpty()?0:a();}

My first Java answer, would love some tips!

\$\endgroup\$
  • \$\begingroup\$ You can probably remove the space between return and !. .equals("") == .isEmpty(). You can return int instead of String. OTOH, return void but either while(System...) or if(System...)a(); \$\endgroup\$ – Justin May 5 '17 at 15:22
  • \$\begingroup\$ you can do int a(){..?0:a();}, saves 3 bytes \$\endgroup\$ – Khaled.K May 7 '17 at 16:23
  • \$\begingroup\$ @Khaled.K actually 4! \$\endgroup\$ – OldBunny2800 May 7 '17 at 17:05
  • \$\begingroup\$ To @Justin and Khaled.K, thank you for your advice! \$\endgroup\$ – OldBunny2800 May 7 '17 at 17:05
2
\$\begingroup\$

Braingolf, 8 bytes [non-competing]

1[{?:+|]

Non competing because {, which reads input from STDIN, was added to braingolf after this challenge's post date.

Explanation:

1         Pushes 1 to the stack
 [        Begins while loop
  {       Reads from STDIN, returns -1 if no input given
   ?      Checks if last item in stack is > 0
    :     Else
     +    Adds the last 2 items together, however there is only 1 item, so add it to itself
      |   Endif
       ]  End While, decrement first item in stack, or exit if first item is 0
\$\endgroup\$
  • \$\begingroup\$ Haha, you implemented input an hour after the challenge was posted \$\endgroup\$ – ASCII-only May 4 '17 at 7:03
  • \$\begingroup\$ @ASCII-only had to do it eventually, haha \$\endgroup\$ – Skidsdev May 4 '17 at 7:38
2
\$\begingroup\$

Charcoal, 4 bytes

W¬Sω

Explanation

W      While
  ¬S   Logical not of string input
     ω  Print ""
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 5 bytes

[IõÊ#

Explanation:

[     Start infinite loop
 I    One line of input
   Ê  Not equal to
  õ   Empty string
    # Break if true

Try it online!

\$\endgroup\$
  • \$\begingroup\$ These are 5 Unicode code points, not 5 bytes; they are 7 bytes in UTF-8, 10 bytes in UTF-16 or 20 bytes in UTF-32. \$\endgroup\$ – Matteo Italia May 7 '17 at 14:49
  • \$\begingroup\$ @MatteoItalia 05AB1E uses its own encoding, therefore each of these characters are 1 byte. \$\endgroup\$ – Okx May 7 '17 at 14:55
  • \$\begingroup\$ Uh, sorry, I stand corrected! \$\endgroup\$ – Matteo Italia May 7 '17 at 14:56
2
\$\begingroup\$

PowerShell, 20 Bytes

for(;!(read-host)){}

runs a for loop,

read-host prompts for input

if read-host returns nothing it evals to false, so we invert that !(...) and use that as the loop end check.

much shorter than any do{$a=read-host}while($a-eq"") type solution involving variables.

\$\endgroup\$
2
\$\begingroup\$

Swift, 22 bytes

while readLine()==""{}
\$\endgroup\$
2
\$\begingroup\$

CJam, 5 bytes

{l!}g

Try it online! (TIO doesn't really show the proper behaviour)

Explanation

{      e# Do:
 l     e#  Read next line of input
  !    e#  Boolean negate (empty strings are falsy)
   }g  e# Pop the top of stack, if truthy, repeat.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.