User Input: Difference between revisions
Content added Content deleted
m (→{{header|Java}}: langtag) |
m (massive (but not total) +langtag (hopefully errorless)) |
||
Line 95: | Line 95: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
using System; |
<lang csharp> using System; |
||
namespace C_Sharp_Console { |
namespace C_Sharp_Console { |
||
Line 111: | Line 111: | ||
} |
} |
||
} |
} |
||
} |
}</lang> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<lang erlang> {ok, [String]} = io:fread("Enter a string: ","~s"). |
|||
{ok, [Number]} = io:fread("Enter a number: ","~d"). |
{ok, [Number]} = io:fread("Enter a number: ","~d").</lang> |
||
Alternatively, you could use io:get_line to get a string: |
Alternatively, you could use io:get_line to get a string: |
||
<lang erlang> String = io:get_line("Enter a string: ").</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
===Input a string=== |
===Input a string=== |
||
<lang forth> : INPUT$ ( n -- addr n ) |
|||
PAD SWAP ACCEPT |
PAD SWAP ACCEPT |
||
PAD SWAP ; |
PAD SWAP ;</lang> |
||
===Input a number=== |
===Input a number=== |
||
The only ANS standard number interpretation word is >NUMBER ( ud str len -- ud str len ), which is meant to be the base factor for more convenient (but non-standard) parsing words. |
The only ANS standard number interpretation word is >NUMBER ( ud str len -- ud str len ), which is meant to be the base factor for more convenient (but non-standard) parsing words. |
||
<lang forth> : INPUT# ( -- u true | false ) |
|||
0. 16 INPUT$ DUP >R |
0. 16 INPUT$ DUP >R |
||
>NUMBER NIP NIP |
>NUMBER NIP NIP |
||
R> <> DUP 0= IF NIP THEN ; |
R> <> DUP 0= IF NIP THEN ;</lang> |
||
{{works with|GNU Forth}} |
{{works with|GNU Forth}} |
||
<lang forth> : INPUT# ( -- n true | d 1 | false ) |
|||
16 INPUT$ SNUMBER? ; |
16 INPUT$ SNUMBER? ;</lang> |
||
{{works with|Win32Forth}} |
{{works with|Win32Forth}} |
||
<lang forth> : INPUT# ( -- n true | false ) |
|||
16 INPUT$ NUMBER? NIP |
16 INPUT$ NUMBER? NIP |
||
DUP 0= IF NIP THEN ; |
DUP 0= IF NIP THEN ;</lang> |
||
Note that NUMBER? always leaves a double result on the stack. |
Note that NUMBER? always leaves a double result on the stack. |
||
Line 148: | Line 148: | ||
Here is an example that puts it all together: |
Here is an example that puts it all together: |
||
<lang forth> : TEST |
|||
." Enter your name: " 80 INPUT$ CR |
." Enter your name: " 80 INPUT$ CR |
||
." Hello there, " TYPE CR |
." Hello there, " TYPE CR |
||
." Enter a number: " INPUT# CR |
." Enter a number: " INPUT# CR |
||
IF ." Your number is " . |
IF ." Your number is " . |
||
ELSE ." That's not a number!" THEN CR ; |
ELSE ." That's not a number!" THEN CR ;</lang> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
<lang groovy> word = System.in.readLine() |
|||
num = System.in.readLine().toInteger() |
num = System.in.readLine().toInteger()</lang> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell> main = do |
|||
putStr "Enter a string: " |
putStr "Enter a string: " |
||
str <- getLine |
str <- getLine |
||
putStr "Enter an integer: " |
putStr "Enter an integer: " |
||
num <- readLn :: IO Int |
num <- readLn :: IO Int |
||
putStrLn $ str ++ (show num) |
putStrLn $ str ++ (show num)</lang> |
||
Note: <tt>:: IO Int</tt> is only there to disambiguate what type we wanted from <tt>read</tt>. If <tt>num</tt> were used in a numerical context, its type would have been inferred by the interpreter/compiler. |
Note: <tt>:: IO Int</tt> is only there to disambiguate what type we wanted from <tt>read</tt>. If <tt>num</tt> were used in a numerical context, its type would have been inferred by the interpreter/compiler. |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<lang java> import java.io.BufferedReader; |
|||
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
||
Line 179: | Line 179: | ||
String string = sysin.readLine(); |
String string = sysin.readLine(); |
||
} |
} |
||
} |
}</lang> |
||
or |
or |
||
{{works with|Java|1.5/5.0+}} |
{{works with|Java|1.5/5.0+}} |
||
<lang java> import java.util.Scanner; |
|||
Scanner stdin = new Scanner(System.in); |
Scanner stdin = new Scanner(System.in); |
||
String string = stdin.nextLine(); |
String string = stdin.nextLine(); |
||
int number = stdin.nextInt(); |
int number = stdin.nextInt();</lang> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Logo literals may be read from a line of input from stdin as either a list or a single word. |
Logo literals may be read from a line of input from stdin as either a list or a single word. |
||
<lang logo> make "input readlist ; in: string 75000 |
|||
show map "number? :input ; [false true] |
show map "number? :input ; [false true] |
||
Line 198: | Line 198: | ||
show :input + 123 ; 75123 |
show :input + 123 ; 75123 |
||
make "input readword ; in: string 75000 |
make "input readword ; in: string 75000 |
||
show :input ; string 75000 |
show :input ; string 75000</lang> |
||
=={{header|mIRC Scripting Language}}== |
=={{header|mIRC Scripting Language}}== |
||
Line 206: | Line 206: | ||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
<lang modula3> |
|||
<pre> |
|||
MODULE Input EXPORTS Main; |
MODULE Input EXPORTS Main; |
||
Line 221: | Line 221: | ||
IO.Put("You entered: " & string & " and " & Fmt.Int(number) & "\n"); |
IO.Put("You entered: " & string & " and " & Fmt.Int(number) & "\n"); |
||
END Input. |
END Input. |
||
</ |
</lang> |
||
=={{header|newLISP}}== |
=={{header|newLISP}}== |
||
Line 239: | Line 239: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
<lang pascal> program UserInput(input, output); |
|||
var i : Integer; |
var i : Integer; |
||
s : String; |
s : String; |
||
Line 247: | Line 247: | ||
write('Enter a string: '); |
write('Enter a string: '); |
||
readln(s) |
readln(s) |
||
end. |
end.</lang> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{works with|Perl|5.8.8}} |
{{works with|Perl|5.8.8}} |
||
<lang perl> #!/usr/bin/perl |
|||
my $string = <>; # equivalent to readline(*STDIN) |
my $string = <>; # equivalent to readline(*STDIN) |
||
my $integer = <>; |
my $integer = <>;</lang> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
{{works with|CLI SAPI}} |
{{works with|CLI SAPI}} |
||
<lang php> #!/usr/bin/php |
|||
<?php |
<?php |
||
$string = fgets(STDIN); |
$string = fgets(STDIN); |
||
$integer = (int) fgets(STDIN); |
$integer = (int) fgets(STDIN);</lang> |
||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
<lang pop11> ;;; Setup item reader |
|||
lvars itemrep = incharitem(charin); |
lvars itemrep = incharitem(charin); |
||
lvars s, c, j = 0; |
lvars s, c, j = 0; |
||
Line 272: | Line 272: | ||
consstring(j) -> s; |
consstring(j) -> s; |
||
;;; read the integer |
;;; read the integer |
||
lvars i = itemrep(); |
lvars i = itemrep();</lang> |
||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
{{works with|PostScript|level-2}} |
{{works with|PostScript|level-2}} |
||
<lang postscript> %open stdin for reading (and name the channel "kbd"): |
|||
/kbd (%stdin) (r) file def |
/kbd (%stdin) (r) file def |
||
%make ten-char buffer to read string into: |
%make ten-char buffer to read string into: |
||
/buf (..........) def |
/buf (..........) def |
||
%read string into buffer: |
%read string into buffer: |
||
kbd buf readline |
kbd buf readline</lang> |
||
At this point there will be two items on the stack: a boolean which is "true" if the read was successful and the string that was read from the kbd (input terminates on a <return>). If the length of the string exceeds the buffer length, an error condition occurs (rangecheck). For the second part, the above could be followed by this: |
At this point there will be two items on the stack: a boolean which is "true" if the read was successful and the string that was read from the kbd (input terminates on a <return>). If the length of the string exceeds the buffer length, an error condition occurs (rangecheck). For the second part, the above could be followed by this: |
||
<lang postscript> %if the read was successful, convert the string to integer: |
|||
{cvi} if |
{cvi} if</lang> |
||
which will read the conversion operator 'cvi' (convert to integer) and the boolean and execute the former if the latter is true. |
which will read the conversion operator 'cvi' (convert to integer) and the boolean and execute the former if the latter is true. |
||
Line 297: | Line 297: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Input a string=== |
===Input a string=== |
||
string = raw_input("Input a string: ") |
<lang python> string = raw_input("Input a string: ")</lang> |
||
In Python 3.0, raw_input will be renamed to input(). The Python 3.0 equivalent would be |
In Python 3.0, raw_input will be renamed to input(). The Python 3.0 equivalent would be |
||
string = input("Input a string: ") |
<lang python> string = input("Input a string: ")</lang> |
||
===Input a number=== |
===Input a number=== |
||
While input() gets a string in Python 3.0, in 2.x it is the equivalent of eval(raw_input(...)). Because this runs arbitrary code, and just isn't nice, it is being removed in Python 3.0. raw_input() is being changed to input() because there will be no other kind of input function in Python 3.0. |
While input() gets a string in Python 3.0, in 2.x it is the equivalent of eval(raw_input(...)). Because this runs arbitrary code, and just isn't nice, it is being removed in Python 3.0. raw_input() is being changed to input() because there will be no other kind of input function in Python 3.0. |
||
number = input("Input a number: ") # Deprecated, please don't use. |
<lang python> number = input("Input a number: ") # Deprecated, please don't use.</lang> |
||
Python 3.0 equivalent: |
Python 3.0 equivalent: |
||
number = eval(input("Input a number: ")) # Evil, please don't use. |
<lang python> number = eval(input("Input a number: ")) # Evil, please don't use.</lang> |
||
The preferred way of getting numbers from the user is to take the input as a string, and pass it to any one of the numeric types to create an instance of the appropriate number. |
The preferred way of getting numbers from the user is to take the input as a string, and pass it to any one of the numeric types to create an instance of the appropriate number. |
||
number = float(raw_input("Input a number: ")) |
<lang python> number = float(raw_input("Input a number: "))</lang> |
||
Python 3.0 equivalent: |
Python 3.0 equivalent: |
||
number = float(input("Input a number: ")) |
<lang python> number = float(input("Input a number: "))</lang> |
||
float may be replaced by any numeric type, such as int, complex, or decimal.Decimal. Each one varies in expected input. |
float may be replaced by any numeric type, such as int, complex, or decimal.Decimal. Each one varies in expected input. |
||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
<lang raven> 'Input a string: ' print expect as str |
|||
'Input an integer: ' print expect 0 prefer as num |
'Input an integer: ' print expect 0 prefer as num</lang> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{works with|Ruby|1.8.4}} |
{{works with|Ruby|1.8.4}} |
||
<lang ruby> print "Enter a string: " |
|||
s = gets |
s = gets |
||
print "Enter an integer: " |
print "Enter an integer: " |
||
i = gets.to_i # If string entered, will return zero |
i = gets.to_i # If string entered, will return zero |
||
puts "String = " + s |
puts "String = " + s |
||
puts "Integer = " + i.to_s |
puts "Integer = " + i.to_s</lang> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
The <tt>read</tt> procedure is R5RS standard, inputs a scheme representation so, in order to read a string, one must enter <tt>"hello world"</tt> |
The <tt>read</tt> procedure is R5RS standard, inputs a scheme representation so, in order to read a string, one must enter <tt>"hello world"</tt> |
||
(define str (read)) |
<lang scheme> (define str (read)) |
||
(define num (read)) |
(define num (read)) |
||
(display "String = ") (display str) |
(display "String = ") (display str) |
||
(display "Integer = ") (display num) |
(display "Integer = ") (display num)</lang> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Like LISP, there is no concept of a "number" in TCL - the only real variable type is a string (whether a string might represent a number is a matter of interpretation of the string in a mathematical expression at some later time). Thus the input is the same for both tasks: |
Like LISP, there is no concept of a "number" in TCL - the only real variable type is a string (whether a string might represent a number is a matter of interpretation of the string in a mathematical expression at some later time). Thus the input is the same for both tasks: |
||
set str [gets stdin] |
<lang tcl> set str [gets stdin] |
||
set num [gets stdin] |
set num [gets stdin]</lang> |
||
possibly followed by something like |
possibly followed by something like |
||
if {![string is integer $num]} then { ...do something here...} |
<lang tcl> if {![string is integer $num]} then { ...do something here...}</lang> |
||
=={{header|Toka}}== |
=={{header|Toka}}== |
||
Line 389: | Line 389: | ||
{{libheader|Tkinter}} |
{{libheader|Tkinter}} |
||
import tkSimpleDialog |
<lang python> import tkSimpleDialog |
||
number = tkSimpleDialog.askinteger("Integer", "Enter a Number") |
number = tkSimpleDialog.askinteger("Integer", "Enter a Number") |
||
string = tkSimpleDialog.askstring("String", "Enter a String") |
string = tkSimpleDialog.askstring("String", "Enter a String")</lang> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
# create entry widget: |
<lang tcl> # create entry widget: |
||
pack [entry .e1] |
pack [entry .e1] |
||
# read its content: |
# read its content: |
||
set input [.e get] |
set input [.e get]</lang> |
||
Alternatively, the content of the widget can be tied to a variable: |
Alternatively, the content of the widget can be tied to a variable: |
||
pack [entry .e1 -textvar input] |
<lang tcl> pack [entry .e1 -textvar input] |
||
# show the content at any time by |
# show the content at any time by |
||
puts $input |
puts $input</lang> |
||
The <tt>-validate</tt> option can be used to test the contents/edits of the widget at any time against any parameters (including testing <tt>string is integer</tt> when the user hits <Return> or such) |
The <tt>-validate</tt> option can be used to test the contents/edits of the widget at any time against any parameters (including testing <tt>string is integer</tt> when the user hits <Return> or such) |