How can I read user input from the console?
I'm not sure what your problem is (since you haven't told us), but I'm guessing at
a = Console.Read();
This will only read one character from your Console.
You can change your program to this. To make it more robust, accept more than 1 char input, and validate that the input is actually a number:
double a, b;
Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz");
if (double.TryParse(Console.ReadLine(), out a)) {
b = a * Math.PI;
Console.WriteLine("Sonuç " + b);
} else {
//user gave an illegal input. Handle it here.
}
How to read user input in c# console
One variable named name
is not enough if you want to split it up into first and last name as provided in your example.
Console.Write("First name:");
var firstName = Console.ReadLine();
Console.Write("Last name:");
var lastName = Console.ReadLine();
Pacient John = new Pacient(firstName, lastName, new DateTime(1992,12,12) , " 045-999-333", " example@example.com");
John.Email = "example@example.com";
To print it:
Console.WriteLine("Name: {0} {1}",firstName,lastName);
P.S. Patient is spelled with T in English.
How can I read input from the console using the Scanner class in Java?
A simple example to illustrate how java.util.Scanner
works would be reading a single integer from System.in
. It's really quite simple.
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
To retrieve a username I would probably use sc.nextLine()
.
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("Your username is " + username);
You could also use next(String pattern)
if you want more control over the input, or just validate the username
variable.
You'll find more information on their implementation in the API Documentation for java.util.Scanner
Proper way to get User Input from Console
The proper way is always illustrated in Oracle Java Tutorial itself.
Read it here I/O from the Command Line and find the sample code as well.
EDIT
Some points:
- calling
main()
method again will initialize all the things again. - try with
switch-case
in case of multiple checks. - you can achieve the same functionality using
do-while
loop.
Sample code:
public static void main(String args[]) throws IOException {
Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
boolean isValidResponse = false;
do {
String response = c.readLine("Are you ready to continue? (Enter \"YES\" or \"NO\"): ");
switch (response.toUpperCase()) {
case "YES":
isValidResponse = true;
System.out.println("Let's continue!");
break;
case "NO":
isValidResponse = true;
System.out.println("Goodbye!");
break;
default:
isValidResponse = false;
System.out.println("Please input a correct response!");
break;
}
} while (!isValidResponse);
}
C# console application reading user input
There is a character limit for Console.ReadLine()
. It should be 256 characters if I am not mistaken. So, you can increase the limit like that;
Console.SetIn(new StreamReader(Console.OpenStandardInput(),
Console.InputEncoding,
false,
bufferSize: 1024));
string[] temp = Console.ReadLine().Split(' ');
int[] height = Array.ConvertAll(temp, Int32.Parse);
Reading an integer from user input
You can convert the string to integer using Convert.ToInt32() function
int intTemp = Convert.ToInt32(Console.ReadLine());
How to read user input directly in Node.js
To achieve that you can use the following:
process.argv
process.argv is an array containing all arguments that were used when invoking you programm.
Image you called your programm like that: node index.js "John"
, process.argv would look like this: ["node", "path/to/index.js", "John"]
.
Reading the user input from the console & writing it to JSON
**Try this just copy and paste it in your public class**
public static void main(String... args)
{
Map<String,String> myMap = new HashMap<String,String>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name;");
String firstName = sc.next();
myMap.put("firstName",firstName);
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter your lastName;");
String lastName = sc1.next();
myMap.put("lastName",lastName);
Scanner sc2 = new Scanner(System.in);
System.out.println("Enter your email;");
String email = sc2.next();
myMap.put("email",email);
Scanner sc3 = new Scanner(System.in);
System.out.println("Enter your mobile;");
String mobile = sc3.next();
myMap.put("mobile",mobile);
try {
JSONObject jsonObject = new JSONObject(myMap.toString());
System.out.println(jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
You have to import
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
Related Topics
Getting HTML Body Content in Winforms Webbrowser After Body Onload Event Executes
Return HTML from ASP.NET Web API
Prevent Property from Being Serialized in Web API
How to Make Smtp Authenticated in C#
Environment.Tickcount VS Datetime.Now
How to Represent a Time Only Value in .Net
Get Transactionscope to Work with Async/Await
What Is the Purpose of Anonymous { } Blocks in C Style Languages
Passing a Vector/Array from Unmanaged C++ to C#
How to Stream Webcam Video with C#
How to Get the HTML Output of a Usercontrol in .Net (C#)
Creating a PDF from a Rdlc Report in the Background
How to Create an Immutable Class