Java has namespace Scanner class is here something similar in C#
Yes, there is Console.ReadLine
.
https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?redirectedfrom=MSDN&view=netframework-4.7.2#System_Console_ReadLine
Or, you can create your own Scanner class in C#
Stolen from Is there an equivalent to the Scanner class in C# for strings?
class Scanner : System.IO.StringReader
{
string currentWord;
public Scanner(string source) : base(source)
{
readNextWord();
}
private void readNextWord()
{
System.Text.StringBuilder sb = new StringBuilder();
char nextChar;
int next;
do
{
next = this.Read();
if (next < 0)
break;
nextChar = (char)next;
if (char.IsWhiteSpace(nextChar))
break;
sb.Append(nextChar);
} while (true);
while((this.Peek() >= 0) && (char.IsWhiteSpace((char)this.Peek())))
this.Read();
if (sb.Length > 0)
currentWord = sb.ToString();
else
currentWord = null;
}
public bool hasNextInt()
{
if (currentWord == null)
return false;
int dummy;
return int.TryParse(currentWord, out dummy);
}
public int nextInt()
{
try
{
return int.Parse(currentWord);
}
finally
{
readNextWord();
}
}
public bool hasNextDouble()
{
if (currentWord == null)
return false;
double dummy;
return double.TryParse(currentWord, out dummy);
}
public double nextDouble()
{
try
{
return double.Parse(currentWord);
}
finally
{
readNextWord();
}
}
public bool hasNext()
{
return currentWord != null;
}
}
Java scanner class - What to do in C#
string Str = txtMemberNr.Text.Trim();
double Num;
bool isNum = double.TryParse(Str, out Num);
if (isNum)
{
// CODE IS HERE
}
else
{
MessageBox.Show("Brugernavn skal kun indeholde tal, prøv igen!", "advarsel", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
This was the most simple way - checking whether or not there is an int/string in the textbox field.
I was hoping that you/or someone who have option for making this solved.
Thank you for your help - but i could not make it work as wanted - even though there was good use of the scanner.. thank you.
C# equivalent to Java's scn.nextInt( )
Is it equivalent to Java's scanner.nextInt() when I use int a = int.Parse(Console.ReadLine()); to receive int inputs?
Yes. Java's Scanner.nextInt()
throws an exception when no integer input has been received, as does .NET's int.Parse(Console.ReadLine())
.
Does C# have a String Tokenizer like Java's?
You could use String.Split method.
class ExampleClass
{
public ExampleClass()
{
string exampleString = "there is a cat";
// Split string on spaces. This will separate all the words in a string
string[] words = exampleString.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
// there
// is
// a
// cat
}
}
}
For more information see Sam Allen's article about splitting strings in c# (Performance, Regex)
How to get my scanner to recognize the words i have for it
try to test if contains the month list.contains()
and the days in an other methode just call it
Easiest way to read single int from front of string
You could easily write such a “library” yourself:
class Parser
{
private readonly Queue<string> m_parts;
public Parser(string s)
{
m_parts = new Queue<string>(s.Split(' '));
}
public string ReadString()
{
return m_parts.Dequeue();
}
public int ReadInt32()
{
return int.Parse(ReadString());
}
}
If the string could be large, or you are reading it from a stream, you have to do the splitting yourself:
class StreamParser
{
private readonly TextReader m_reader;
public StreamParser(string s)
: this(new StringReader(s))
{}
public StreamParser(TextReader reader)
{
m_reader = reader;
}
public string ReadString()
{
var result = new StringBuilder();
int c = m_reader.Read();
while (c != -1 && (char)c != ' ')
{
result.Append((char)c);
c = m_reader.Read();
}
if (result.Length > 0)
return result.ToString();
return null;
}
public int ReadInt32()
{
return int.Parse(ReadString());
}
}
Checking if a user inputed string is in an array
you almost had it, just move the userName
assignment up to its own line:
static void Main(string[] args)
{
string[] name = { "Bob", "Bob2", "Bob3", "Bob4" };
string userName = Console.ReadLine();
bool exists = name.Contains(userName);
if (exists == true)
Console.WriteLine("Hi " + userName);
Console.ReadLine();
}
here is the output:
Related Topics
How to Get My C# Program to Sleep for 50 Msec
Why Do Bcl Collections Use Struct Enumerators, Not Classes
How to Add CSS Class to HTML Generic Control Div
Copy Files from Resources/Streamingassets to Application.Persistentdatapath Upon Installation
Import a Public Key from Somewhere Else to Cngkey
How Does Creating a Instance of Class Inside of the Class Itself Works
Using Reflection in C# to Get Properties of a Nested Object
C# Tcpclient: Send Serialized Objects Using Separators
How to Test If Type Is Primitive
Visual Studio: Contextswitchdeadlock
.Net Core 2.1 Identity Get All Users with Their Associated Roles
Add CSS Class to a Div in Code Behind
Xamarin iOS Memory Leaks Everywhere
How to Return JSON with ASP.NET & Jquery
Posting JSON to Url via Webclient in C#
How to Test for the Presence of an Action Filter with Constructor Arguments