Is There an Equivalent to the Scanner Class in C# for Strings

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:

Sample Image



Related Topics



Leave a reply



Submit